Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ First release as `com.spreedly:composer-sdk-server`. If you're upgrading from `c
- `CheckpointResponseOptions.timeout` is an `Integer` and `sync` is a `Boolean`. A new `CheckpointRequest` has `options` `null`.
- `checkpoint()` throws `ComposerMissingParameterException` (an `IllegalArgumentException`) before returning the future when the request, `checkpointName`, `event`, or both `sessionId` and `sourceToken` are missing. Blank strings count as missing. This happens even when the SDK is disabled.
- `event.ip` is optional on checkpoints.
- `ApiEvent.eventTime` is a `Long` (milliseconds since the Unix epoch) instead of a `Date`.
- When `eventTime` is `null`, `event()` sends the current time. It doesn't set `eventTime` on your `ApiEvent`.
- `event()` sends only `type`, `data` and `eventTime`. Fields you add in an `ApiEvent` subclass aren't sent; put custom values in `data`.
- `event()` doesn't throw for invalid input or failed requests. It logs the problem and returns a response with `success` `false`.
- `sync` defaults to `false` and is sent as given. With `sync` `true`, the first request waits for the workflow to finish, so set `setRequestTimeoutMs` longer than your slowest workflow.
- `timeout` is now only how long the SDK polls, and defaults to 10 minutes. It's no longer sent to the API.
Expand Down
1 change: 0 additions & 1 deletion src/main/java/com/spreedly/composer/api/ApiEvent.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ public ApiEvent() {
public ApiEvent(String type, Map<String, Object> data) {
this.type = type;
this.data = data;
this.eventTime = System.currentTimeMillis();
}

/**
Expand Down
12 changes: 8 additions & 4 deletions src/main/java/com/spreedly/composer/api/ComposerServices.java
Original file line number Diff line number Diff line change
Expand Up @@ -340,13 +340,17 @@ public NotificationResponse call() {
OkHttpClient client = buildClient(requestTimeoutMs);

try {
// Ensure event has timestamp
if (request.event != null && request.event.eventTime == null) {
request.event.eventTime = System.currentTimeMillis();
// Send a copy with only the fields the track endpoint takes. Gson would otherwise
// serialize a subclass's extra fields, and defaulting the time on the copy gives a
// reused event a fresh time on each call.
ApiEvent event = request.event;
if (event != null) {
Long eventTime = event.eventTime != null ? event.eventTime : System.currentTimeMillis();
event = new ApiEvent(event.type, event.data, eventTime);
}

// Prepare request body
String jsonBody = gson.toJson(request.event);
String jsonBody = gson.toJson(event);
RequestBody body = RequestBody.create(jsonBody, JSON);

// Build request
Expand Down
72 changes: 72 additions & 0 deletions src/test/java/com/spreedly/composer/ComposerIntegrationTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1238,6 +1238,78 @@ public void testEvent_withNullTimestamp() throws Exception {
assertThat(recordedRequest.getBody().readUtf8()).contains("eventTime");
}

@Test
public void testEvent_reusedEventGetsFreshDefaultTime() throws Exception {
NotificationResponse mockResponse = new NotificationResponse(true, new ComposerApiError[]{});
mockServer.enqueue(new MockResponse()
.setBody(gson.toJson(mockResponse))
.addHeader("Content-Type", "application/json"));
mockServer.enqueue(new MockResponse()
.setBody(gson.toJson(mockResponse))
.addHeader("Content-Type", "application/json"));

ApiEvent event = new ApiEvent("TEST_EVENT", new HashMap<>());
NotificationRequest request = new NotificationRequest();
request.event = event;
request.sessionId = "sess-123";

composer.event(request).get(5, TimeUnit.SECONDS);
Thread.sleep(5);
composer.event(request).get(5, TimeUnit.SECONDS);

long firstTime = gson.fromJson(mockServer.takeRequest().getBody().readUtf8(), com.google.gson.JsonObject.class)
.get("eventTime").getAsLong();
long secondTime = gson.fromJson(mockServer.takeRequest().getBody().readUtf8(), com.google.gson.JsonObject.class)
.get("eventTime").getAsLong();
assertThat(secondTime).isGreaterThan(firstTime);
assertThat(event.eventTime).isNull();
}

@Test
public void testEvent_keepsCallerEventTime() throws Exception {
NotificationResponse mockResponse = new NotificationResponse(true, new ComposerApiError[]{});
mockServer.enqueue(new MockResponse()
.setBody(gson.toJson(mockResponse))
.addHeader("Content-Type", "application/json"));

NotificationRequest request = new NotificationRequest();
request.event = new ApiEvent("TEST_EVENT", new HashMap<>(), 1700000000000L);
request.sessionId = "sess-123";

composer.event(request).get(5, TimeUnit.SECONDS);

long sentTime = gson.fromJson(mockServer.takeRequest().getBody().readUtf8(), com.google.gson.JsonObject.class)
.get("eventTime").getAsLong();
assertThat(sentTime).isEqualTo(1700000000000L);
}

static class EventWithExtraField extends ApiEvent {
public String region = "us-east";

EventWithExtraField(Long eventTime) {
super("TEST_EVENT", new HashMap<>(), eventTime);
}
}

@Test
public void testEvent_sendsOnlyTrackFields() throws Exception {
NotificationResponse mockResponse = new NotificationResponse(true, new ComposerApiError[]{});
for (Long eventTime : new Long[]{1700000000000L, null}) {
mockServer.enqueue(new MockResponse()
.setBody(gson.toJson(mockResponse))
.addHeader("Content-Type", "application/json"));

NotificationRequest request = new NotificationRequest();
request.event = new EventWithExtraField(eventTime);
request.sessionId = "sess-123";

composer.event(request).get(5, TimeUnit.SECONDS);

com.google.gson.JsonObject body = gson.fromJson(mockServer.takeRequest().getBody().readUtf8(), com.google.gson.JsonObject.class);
assertThat(body.keySet()).containsExactlyInAnyOrder("type", "data", "eventTime");
}
}

@Test
public void testEvent_nullEvent() throws Exception {
Composer errorLoggingComposer = Composer.builder()
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/spreedly/composer/api/ApiTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@ public void testApiEvent_constructorWithTypeAndData() {

assertThat(event.type).isEqualTo("TEST_EVENT");
assertThat(event.data).isEqualTo(data);
assertThat(event.eventTime).isNotNull();
assertThat(event.eventTime).isGreaterThan(0L);
assertThat(event.eventTime).isNull();
}

@Test
Expand Down
Loading