Skip to content

Commit

Permalink
fix: fix failed unit tests due to async methods (#74)
Browse files Browse the repository at this point in the history
* fix tests

* Update HttpTransportTest.java

* Update HttpTransportTest.java

* Update AmplitudeTest.java
  • Loading branch information
bohan-amplitude authored Jul 19, 2022
1 parent 4fede17 commit 026c3cc
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 26 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
ARTIFACT_VERSION=1.9.1
ARTIFACT_VERSION=1.10.0
ARTIFACT_GROUP=com.amplitude
POM_PACKAGING=jar
POM_DESCRIPTION=Amplitude Java SDK
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/amplitude/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public interface Constants {

int NETWORK_TIMEOUT_MILLIS = 10000;
String SDK_LIBRARY = "amplitude-java";
String SDK_VERSION = "1.9.1";
String SDK_VERSION = "1.10.0";

int MAX_PROPERTY_KEYS = 1024;
int MAX_STRING_LENGTH = 1000;
Expand Down
35 changes: 26 additions & 9 deletions src/test/java/com/amplitude/AmplitudeTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@

import java.lang.reflect.Field;
import java.util.*;
import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;

Expand Down Expand Up @@ -219,6 +216,7 @@ public void testLogEventWithCallbacks()
HttpCall httpCall = getMockHttpCall(amplitude, false);
Response successResponse = ResponseUtil.getSuccessResponse();
CountDownLatch latch = new CountDownLatch(1);
CountDownLatch latch2 = new CountDownLatch(10);
when(httpCall.makeRequest(anyList()))
.thenAnswer(
invocation -> {
Expand All @@ -233,6 +231,7 @@ public void testLogEventWithCallbacks()
public void onLogEventServerResponse(Event event, int status, String message) {
assertEquals(200, status);
callbackCount1.incrementAndGet();
latch2.countDown();
}
};
AmplitudeCallbacks callbacks2 =
Expand All @@ -241,13 +240,15 @@ public void onLogEventServerResponse(Event event, int status, String message) {
public void onLogEventServerResponse(Event event, int status, String message) {
assertEquals(200, status);
callbackCount2.incrementAndGet();
latch2.countDown();
}
};
for (int i = 0; i < events.size(); i++) {
AmplitudeCallbacks callbackForEvent = i % 2 == 0 ? callbacks1 : callbacks2;
amplitude.logEvent(events.get(i), callbackForEvent);
}
assertTrue(latch.await(1L, TimeUnit.SECONDS));
assertTrue(latch2.await(1L, TimeUnit.SECONDS));
verify(httpCall, times(1)).makeRequest(anyList());
assertEquals(5, callbackCount1.get());
assertEquals(5, callbackCount2.get());
Expand Down Expand Up @@ -391,17 +392,18 @@ public void onLogEventServerResponse(Event event, int status, String message) {
amplitude.logEvent(event);
amplitude.logEvent(event2);
amplitude.flushEvents();
assertTrue(amplitude.shouldWait(event));
assertTrue(shouldWaitResultWithTimeout(amplitude, event, 1L, TimeUnit.SECONDS));
assertFalse(amplitude.shouldWait(event2));
barrier.await();
assertTrue(latch.await(1L, TimeUnit.SECONDS));
assertTrue(callbackLatch.await(1L, TimeUnit.SECONDS));
assertFalse(amplitude.shouldWait(event));
assertFalse(amplitude.shouldWait(event2));
assertFalse(shouldWaitResultWithTimeout(amplitude, event, 1L, TimeUnit.SECONDS));
}

@Test
public void testSetPlan() throws NoSuchFieldException, IllegalAccessException, AmplitudeInvalidAPIKeyException, InterruptedException {
public void testSetPlan()
throws NoSuchFieldException, IllegalAccessException, AmplitudeInvalidAPIKeyException,
InterruptedException {
Amplitude amplitude = Amplitude.getInstance("testSetPlan");
amplitude.init(apiKey);

Expand All @@ -410,7 +412,8 @@ public void testSetPlan() throws NoSuchFieldException, IllegalAccessException, A
String version = "1.0.0";
String versionId = "9ec23ba0-275f-468f-80d1-66b88bff9529";

Plan plan = new Plan().setBranch(branch).setSource(source).setVersion(version).setVersionId(versionId);
Plan plan =
new Plan().setBranch(branch).setSource(source).setVersion(version).setVersionId(versionId);
amplitude.setPlan(plan);

amplitude.useBatchMode(false);
Expand Down Expand Up @@ -466,4 +469,18 @@ private AmplitudeCallbacks getCallbacks(Amplitude amplitude)
callbackField.setAccessible(true);
return (AmplitudeCallbacks) callbackField.get(httpTransport);
}

private boolean shouldWaitResultWithTimeout(Amplitude client, Event event, long time, TimeUnit unit) {
CompletableFuture<Boolean> future = CompletableFuture.supplyAsync(() -> {
while (!client.shouldWait(event)) {
continue;
}
return true;
});
try {
return future.get(time, unit);
} catch (Exception e) {
return false;
}
}
}
30 changes: 15 additions & 15 deletions src/test/java/com/amplitude/HttpTransportTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -214,8 +214,8 @@ public void onLogEventServerResponse(Event event, int status, String message) {
httpTransport.setCallbacks(callbacks);
httpTransport.retryEvents(events, rateLimitResponse);
assertTrue(latch.await(1L, TimeUnit.SECONDS));
for (int i = 0; i < events.size(); i++) {
assertEquals(429, resultMap.get(events.get(i)));
for (Event event : events) {
assertEquals(429, resultMap.get(event));
}
}

Expand Down Expand Up @@ -250,8 +250,8 @@ public void onLogEventServerResponse(Event event, int status, String message) {
assertTrue(latch.await(1L, TimeUnit.SECONDS));
assertTrue(latch2.await(1L, TimeUnit.SECONDS));
verify(httpCall, times(1)).makeRequest(anyList());
for (int i = 0; i < events.size(); i++) {
assertEquals(429, resultMap.get(events.get(i)));
for (Event event : events) {
assertEquals(429, resultMap.get(event));
}
}

Expand Down Expand Up @@ -291,8 +291,8 @@ public void onLogEventServerResponse(Event event, int status, String message) {
assertTrue(latch.await(1L, TimeUnit.SECONDS));
assertTrue(latch2.await(1L, TimeUnit.SECONDS));
verify(httpCall, times(2)).makeRequest(anyList());
for (int i = 0; i < events.size(); i++) {
assertEquals(200, resultMap.get(events.get(i)));
for (Event event : events) {
assertEquals(200, resultMap.get(event));
}
}

Expand Down Expand Up @@ -325,8 +325,8 @@ public void onLogEventServerResponse(Event event, int status, String message) {
assertTrue(latch.await(1L, TimeUnit.SECONDS));
assertTrue(latch2.await(1L, TimeUnit.SECONDS));
verify(httpCall, times(1)).makeRequest(anyList());
for (int i = 0; i < events.size(); i++) {
assertEquals(500, resultMap.get(events.get(i)));
for (Event event : events) {
assertEquals(500, resultMap.get(event));
}
}

Expand Down Expand Up @@ -359,8 +359,8 @@ public void onLogEventServerResponse(Event event, int status, String message) {
assertTrue(latch.await(1L, TimeUnit.SECONDS));
assertTrue(latch2.await(1L, TimeUnit.SECONDS));
verify(httpCall, times(1)).makeRequest(anyList());
for (int i = 0; i < events.size(); i++) {
assertEquals(0, resultMap.get(events.get(i)));
for (Event event : events) {
assertEquals(0, resultMap.get(event));
}
}

Expand All @@ -384,16 +384,16 @@ public void testThreadTimeoutCallback()
@Override
public void onLogEventServerResponse(Event event, int status, String message) {
resultMap.put(event, status);
assertEquals("Error send events", message);
latch.countDown();
}
};
httpTransport.setHttpCall(httpCall);
httpTransport.setCallbacks(callbacks);
httpTransport.sendEventsWithRetry(events);
assertTrue(latch.await(1L, TimeUnit.SECONDS));
verify(httpCall, times(1)).makeRequest(anyList());
for (int i = 0; i < events.size(); i++) {
assertEquals(0, resultMap.get(events.get(i)));
for (Event event : events) {
assertEquals(0, resultMap.get(event));
}
}

Expand Down Expand Up @@ -426,8 +426,8 @@ public void onLogEventServerResponse(Event event, int status, String message) {
httpTransport.shutdown();

assertTrue(latch.await(5L, TimeUnit.SECONDS));
for (int i = 0; i < events.size(); i++) {
assertEquals(200, resultMap.get(events.get(i)));
for (Event event : events) {
assertEquals(200, resultMap.get(event));
}
}
}

0 comments on commit 026c3cc

Please sign in to comment.