Skip to content

Commit

Permalink
Pre track events (#460)
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergio Alonso Fernández authored May 23, 2017
1 parent e5beaf4 commit 8250889
Show file tree
Hide file tree
Showing 20 changed files with 1,143 additions and 251 deletions.
6 changes: 6 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ env:
# install timeout in minutes (2 minutes by default)
- ADB_INSTALL_TIMEOUT=8

before_install:
- adb logcat > logcat.log &

# Emulator Management: Create, Start and Wait
before_script:
- echo no | android create avd --force -n mp --target android-19 --abi armeabi-v7a
Expand All @@ -30,3 +33,6 @@ script:
- ./gradlew lint
- ./gradlew --info connectedAndroidTest
- ./gradlew --info androidJavadocs

after_failure:
- cat logcat.log

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -27,27 +27,27 @@ public void setUp() {
mPoster = new MockPoster();
mEventBinder = new MockUpdatesFromMixpanel();
mEventBinder.startUpdates();
mDecideMessages1 = new DecideMessages("TOKEN 1", null, mEventBinder);
mDecideMessages1 = new DecideMessages(getContext(), "TOKEN 1", null, mEventBinder);
mDecideMessages1.setDistinctId("DISTINCT ID 1");
mDecideMessages2 = new DecideMessages("TOKEN 2", null, mEventBinder);
mDecideMessages2 = new DecideMessages(getContext(), "TOKEN 2", null, mEventBinder);
mDecideMessages2.setDistinctId("DISTINCT ID 2");
mDecideMessages3 = new DecideMessages("TOKEN 3", null, mEventBinder);
mDecideMessages3 = new DecideMessages(getContext(), "TOKEN 3", null, mEventBinder);
mDecideMessages3.setDistinctId("DISTINCT ID 3");
}

public void testReadEmptyLists() throws RemoteService.ServiceUnavailableException {
mDecideChecker.addDecideCheck(mDecideMessages1);

mPoster.response = bytes("{}");
mDecideChecker.runDecideChecks(mPoster);
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertNull(mDecideMessages1.getNotification(false));
assertUpdatesSeen(new JSONArray[] {
new JSONArray()
});
mEventBinder.bindingsSeen.clear();

mPoster.response = bytes("{\"notifications\":[]}");
mDecideChecker.runDecideChecks(mPoster);
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertNull(mDecideMessages1.getNotification(false));
assertUpdatesSeen(new JSONArray[] {
new JSONArray()
Expand All @@ -59,21 +59,21 @@ public void testBadDecideResponses() throws RemoteService.ServiceUnavailableExce

// Corrupted or crazy responses.
mPoster.response = bytes("{ WONT PARSE");
mDecideChecker.runDecideChecks(mPoster);
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertNull(mDecideMessages1.getNotification(false));
assertUpdatesSeen(new JSONArray[] {}); // No updates at all on parsing failure
mEventBinder.bindingsSeen.clear();

// Just pure (but legal) JSON craziness
mPoster.response = bytes("null");
mDecideChecker.runDecideChecks(mPoster);
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertNull(mDecideMessages1.getNotification(false));
assertUpdatesSeen(new JSONArray[]{});
mEventBinder.bindingsSeen.clear();

// Valid JSON that isn't relevant
mPoster.response = bytes("{\"Ziggy Startdust and the Spiders from Mars\":\"The Best Ever Number One\"}");
mDecideChecker.runDecideChecks(mPoster);
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertNull(mDecideMessages1.getNotification(false));
assertUpdatesSeen(new JSONArray[]{
new JSONArray()
Expand All @@ -87,7 +87,7 @@ public void testDecideHonorsFallbackEnabled() throws RemoteService.ServiceUnavai
mPoster.response = null;
mPoster.exception = new IOException("Bang!");
mDecideChecker.addDecideCheck(mDecideMessages1);
mDecideChecker.runDecideChecks(mPoster);
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertEquals(2, mPoster.requestedUrls.size());
}

Expand All @@ -97,7 +97,7 @@ public void testDecideHonorsFallbackDisabled() throws RemoteService.ServiceUnava
mPoster.response = null;
mPoster.exception = new IOException("Bang!");
mDecideChecker.addDecideCheck(mDecideMessages1);
mDecideChecker.runDecideChecks(mPoster);
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertEquals(1, mPoster.requestedUrls.size());
}

Expand Down Expand Up @@ -184,6 +184,32 @@ public void testDecideResponses() throws DecideChecker.UnintelligibleMessageExce
}
}

public void testAutomaticResponse() throws DecideChecker.UnintelligibleMessageException, RemoteService.ServiceUnavailableException {
final String automaticEventsTrue = "{\"notifications\": null, \"automatic_events\": true}";
DecideChecker.Result parseElements;
parseElements = DecideChecker.parseDecideResponse(automaticEventsTrue);
assertTrue(parseElements.automaticEvents);

final String automaticEventsFalse = "{\"notifications\": null, \"automatic_events\": false}";
parseElements = DecideChecker.parseDecideResponse(automaticEventsFalse);
assertFalse(parseElements.automaticEvents);

mDecideChecker.addDecideCheck(mDecideMessages1);

assertNull(mDecideMessages1.isAutomaticEventsEnabled());
assertTrue(mDecideMessages1.shouldTrackAutomaticEvent());

mPoster.response = bytes("{\"notifications\": null, \"automatic_events\": true}");
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertTrue(mDecideMessages1.isAutomaticEventsEnabled());
assertTrue(mDecideMessages1.shouldTrackAutomaticEvent());

mPoster.response = bytes("{\"notifications\": null, \"automatic_events\": false}");
mDecideChecker.runDecideCheck(mDecideMessages1.getToken(), mPoster);
assertFalse(mDecideMessages1.isAutomaticEventsEnabled());
assertFalse(mDecideMessages1.shouldTrackAutomaticEvent());
}

private void assertUpdatesSeen(JSONArray[] expected) {
assertEquals(expected.length, mEventBinder.bindingsSeen.size());
for (int bindingCallIx = 0; bindingCallIx < expected.length; bindingCallIx++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -334,12 +334,12 @@ public synchronized String toString() {

private class MockMessages extends DecideMessages {
public MockMessages(final String token, final OnNewResultsListener listener, final UpdatesFromMixpanel binder) {
super(token, listener, binder);
super(getContext(), token, listener, binder);
}

@Override
public void reportResults(List<InAppNotification> newNotifications, JSONArray newBindings, JSONArray variants) {
super.reportResults(newNotifications, newBindings, variants);
public void reportResults(List<InAppNotification> newNotifications, JSONArray newBindings, JSONArray variants, boolean isAutomaticEvents) {
super.reportResults(newNotifications, newBindings, variants, isAutomaticEvents);
mExpectations.resolve();
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public void removeOnMixpanelTweaksUpdatedListener(OnMixpanelTweaksUpdatedListene
}
};

mDecideMessages = new DecideMessages("TEST TOKEN", mMockListener, mMockUpdates);
mDecideMessages = new DecideMessages(getContext(), "TEST TOKEN", mMockListener, mMockUpdates);
mSomeNotifications = new ArrayList<>();

JSONObject notifsDesc1 = new JSONObject(
Expand All @@ -76,7 +76,7 @@ public void removeOnMixpanelTweaksUpdatedListener(OnMixpanelTweaksUpdatedListene
}

public void testDuplicateIds() throws JSONException, BadDecideObjectException {
mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants);
mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants, mIsAutomaticEventsEnabled);

final List<InAppNotification> fakeNotifications = new ArrayList<InAppNotification>(mSomeNotifications.size());
for (final InAppNotification real: mSomeNotifications) {
Expand All @@ -90,7 +90,7 @@ public void testDuplicateIds() throws JSONException, BadDecideObjectException {

assertNull(mDecideMessages.getNotification(false));

mDecideMessages.reportResults(fakeNotifications, mSomeBindings, mSomeVariants);
mDecideMessages.reportResults(fakeNotifications, mSomeBindings, mSomeVariants, mIsAutomaticEventsEnabled);

assertNull(mDecideMessages.getNotification(false));

Expand All @@ -100,7 +100,7 @@ public void testDuplicateIds() throws JSONException, BadDecideObjectException {
final InAppNotification unseenNotification = new MiniInAppNotification(notificationNewIdDesc);
fakeNotifications.add(unseenNotification);

mDecideMessages.reportResults(fakeNotifications, mSomeBindings, mSomeVariants);
mDecideMessages.reportResults(fakeNotifications, mSomeBindings, mSomeVariants, mIsAutomaticEventsEnabled);

assertEquals(mDecideMessages.getNotification(false), unseenNotification);

Expand All @@ -111,7 +111,7 @@ public void testPops() {
final InAppNotification nullBeforeNotification = mDecideMessages.getNotification(false);
assertNull(nullBeforeNotification);

mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants);
mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants, mIsAutomaticEventsEnabled);

final InAppNotification n1 = mDecideMessages.getNotification(false);
assertEquals(mSomeNotifications.get(0), n1);
Expand All @@ -125,12 +125,12 @@ public void testPops() {

public void testListenerCalls() throws JSONException, BadDecideObjectException {
assertNull(mListenerCalls.peek());
mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants);
mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants, mIsAutomaticEventsEnabled);
assertEquals(mListenerCalls.poll(), "CALLED");
assertNull(mListenerCalls.peek());

// No new info means no new calls
mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants);
mDecideMessages.reportResults(mSomeNotifications, mSomeBindings, mSomeVariants, mIsAutomaticEventsEnabled);
assertNull(mListenerCalls.peek());

// New info means new calls
Expand All @@ -141,7 +141,7 @@ public void testListenerCalls() throws JSONException, BadDecideObjectException {
final List<InAppNotification> newNotifications = new ArrayList<InAppNotification>();
newNotifications.add(unseenNotification);

mDecideMessages.reportResults(newNotifications, mSomeBindings, mSomeVariants);
mDecideMessages.reportResults(newNotifications, mSomeBindings, mSomeVariants, mIsAutomaticEventsEnabled);
assertEquals(mListenerCalls.poll(), "CALLED");
assertNull(mListenerCalls.peek());
}
Expand All @@ -153,4 +153,5 @@ public void testListenerCalls() throws JSONException, BadDecideObjectException {
private JSONArray mSomeBindings;
private JSONArray mSomeVariants;
private List<InAppNotification> mSomeNotifications;
private boolean mIsAutomaticEventsEnabled;
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,9 @@ public int getFlushInterval() {

final MPDbAdapter mockAdapter = new MPDbAdapter(getContext()) {
@Override
public void cleanupEvents(String last_id, Table table) {
public void cleanupEvents(String last_id, Table table, String token, boolean includeAutomaticEvents) {
mCleanupCalls.add("called");
super.cleanupEvents(last_id, table);
super.cleanupEvents(last_id, table, token, includeAutomaticEvents);
}

@Override
Expand Down Expand Up @@ -196,7 +196,7 @@ public void testHTTPFailures() {
public void runBasicSucceed() throws InterruptedException {
mCleanupCalls.clear();
mMetrics.track(SUCCEED_TEXT, null);
Thread.sleep(mFlushInterval + POLL_WAIT_MAX_MILLISECONDS);
waitForFlushInternval();
assertEquals(SUCCEED_TEXT, mPerformRequestCalls.poll(POLL_WAIT_MAX_MILLISECONDS, DEFAULT_TIMEUNIT));
assertEquals(null, mPerformRequestCalls.poll());
assertEquals(1, mCleanupCalls.size());
Expand Down Expand Up @@ -316,8 +316,6 @@ private void runDoubleServiceUnavailableException() throws InterruptedException

waitForBackOffTimeInterval();

Thread.sleep(5000);

assertEquals(3, mCleanupCalls.size());
assertEquals(SUCCEED_TEXT, mPerformRequestCalls.poll(POLL_WAIT_MAX_MILLISECONDS, DEFAULT_TIMEUNIT));
assertEquals(SUCCEED_TEXT, mPerformRequestCalls.poll(POLL_WAIT_MAX_MILLISECONDS, DEFAULT_TIMEUNIT));
Expand All @@ -343,10 +341,12 @@ private void runMemoryTest() throws InterruptedException {

private void waitForBackOffTimeInterval() throws InterruptedException {
long waitForMs = mMetrics.getAnalyticsMessages().getTrackEngageRetryAfter();
Thread.sleep(waitForMs + 500);
Thread.sleep(waitForMs);
Thread.sleep(1500);
}

private void waitForFlushInternval() throws InterruptedException {
Thread.sleep(mFlushInterval + 500);
Thread.sleep(mFlushInterval);
Thread.sleep(1500);
}
}
Loading

0 comments on commit 8250889

Please sign in to comment.