Skip to content
This repository has been archived by the owner on May 30, 2024. It is now read-only.

Commit

Permalink
Add X-LaunchDarkly-Payload-ID header on event post. (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
gwhelanLD authored Jan 14, 2020
1 parent 138fc89 commit 3063731
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.util.Date;
import java.util.List;
import java.util.Random;
import java.util.UUID;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
Expand Down Expand Up @@ -42,6 +43,7 @@ final class DefaultEventProcessor implements EventProcessor {
private static final Logger logger = LoggerFactory.getLogger(DefaultEventProcessor.class);
private static final String EVENT_SCHEMA_HEADER = "X-LaunchDarkly-Event-Schema";
private static final String EVENT_SCHEMA_VERSION = "3";
private static final String EVENT_PAYLOAD_ID_HEADER = "X-LaunchDarkly-Payload-ID";

private final BlockingQueue<EventProcessorMessage> inbox;
private final ScheduledExecutorService scheduler;
Expand Down Expand Up @@ -542,7 +544,8 @@ void stop() {

private void postEvents(String json, int outputEventCount) {
String uriStr = config.eventsURI.toString() + "/bulk";

String eventPayloadId = UUID.randomUUID().toString();

logger.debug("Posting {} event(s) to {} with payload: {}",
outputEventCount, uriStr, json);

Expand All @@ -558,6 +561,7 @@ private void postEvents(String json, int outputEventCount) {
.post(RequestBody.create(MediaType.parse("application/json; charset=utf-8"), json))
.addHeader("Content-Type", "application/json")
.addHeader(EVENT_SCHEMA_HEADER, EVENT_SCHEMA_VERSION)
.addHeader(EVENT_PAYLOAD_ID_HEADER, eventPayloadId)
.build();

long startTime = System.currentTimeMillis();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

import static com.launchdarkly.client.TestHttpUtil.httpsServerWithSelfSignedCert;
Expand All @@ -22,6 +23,7 @@
import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
Expand Down Expand Up @@ -485,6 +487,50 @@ public void eventSchemaIsSent() throws Exception {
}
}

@Test
public void eventPayloadIdIsSent() throws Exception {
Event e = EventFactory.DEFAULT.newIdentifyEvent(user);

try (MockWebServer server = makeStartedServer(eventsSuccessResponse())) {
try (DefaultEventProcessor ep = new DefaultEventProcessor(SDK_KEY, baseConfig(server).build())) {
ep.sendEvent(e);
}

RecordedRequest req = server.takeRequest();
String payloadHeaderValue = req.getHeader("X-LaunchDarkly-Payload-ID");
assertThat(payloadHeaderValue, notNullValue(String.class));
assertThat(UUID.fromString(payloadHeaderValue), notNullValue(UUID.class));
}
}

@Test
public void eventPayloadIdReusedOnRetry() throws Exception {
MockResponse errorResponse = new MockResponse().setResponseCode(429);
Event e = EventFactory.DEFAULT.newIdentifyEvent(user);

try (MockWebServer server = makeStartedServer(errorResponse, eventsSuccessResponse(), eventsSuccessResponse())) {
try (DefaultEventProcessor ep = new DefaultEventProcessor(SDK_KEY, baseConfig(server).build())) {
ep.sendEvent(e);
ep.flush();
// Necessary to ensure the retry occurs before the second request for test assertion ordering
ep.waitUntilInactive();
ep.sendEvent(e);
}

// Failed response request
RecordedRequest req = server.takeRequest(0, TimeUnit.SECONDS);
String payloadId = req.getHeader("X-LaunchDarkly-Payload-ID");
// Retry request has same payload ID as failed request
req = server.takeRequest(0, TimeUnit.SECONDS);
String retryId = req.getHeader("X-LaunchDarkly-Payload-ID");
assertThat(retryId, equalTo(payloadId));
// Second request has different payload ID from first request
req = server.takeRequest(0, TimeUnit.SECONDS);
payloadId = req.getHeader("X-LaunchDarkly-Payload-ID");
assertThat(retryId, not(equalTo(payloadId)));
}
}

@Test
public void http400ErrorIsRecoverable() throws Exception {
testRecoverableHttpError(400);
Expand Down

0 comments on commit 3063731

Please sign in to comment.