Skip to content

Commit

Permalink
Make ReceiveReceiptWorker run with no delay for unit tests
Browse files Browse the repository at this point in the history
- unit tests fail when ReceiveReceiptWorker runs with a delay so make no delay for unit tests

- a few unit tests related to sending receive receipts  were failing due to "Main looper has queued unexecuted runnables. This might be the cause of the test failure." even after awaiting a long time (flaky pass/fail)

- removed test `testNotificationReceivedNoSendReceivedRequest_Delay` because no longer testing the delay in unit tests and other receive receipt tests are sufficient
  • Loading branch information
nan-li committed Nov 12, 2021
1 parent 8b36886 commit d4458ef
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;

import org.robolectric.annotation.Implementation;
import org.robolectric.annotation.Implements;

@Implements(OSUtils.class)
Expand Down Expand Up @@ -93,4 +94,10 @@ public String getCarrierName() {
int initializationChecker(Context context, String oneSignalAppId) {
return subscribableStatus;
}
}

@Implementation
public static int getRandomDelay(int minDelay, int maxDelay) {
// unit tests fail when ReceiveReceiptWorker runs with a delay so make no delay for unit tests
return 0;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1226,40 +1226,6 @@ public void testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDis
assertNotEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl);
}

@Test
@Config(shadows = { ShadowGenerateNotification.class })
public void testNotificationReceivedNoSendReceivedRequest_Delay() throws Exception {
int delay = 2;
MockDelayTaskController mockDelayTaskController = new MockDelayTaskController(new MockOSLog());
mockDelayTaskController.setMockedRandomValue(delay);
mockDelayTaskController.setRunOnSameThread(false);
OneSignal_setDelayTaskController(mockDelayTaskController);

ShadowOneSignalRestClient.setRemoteParamsReceiveReceiptsEnable(true);
// First init run for appId to be saved
// At least OneSignal was init once for user to be subscribed
// If this doesn't' happen, notifications will not arrive
OneSignal.setAppId(ONESIGNAL_APP_ID);
OneSignal.initWithContext(blankActivity);
threadAndTaskWait();

long calledTime = System.currentTimeMillis();

// 1. Receive a notification in background
FCMBroadcastReceiver_processBundle(blankActivity, getBaseNotifBundle());
threadAndTaskWait();

// 2. Check that report_received where sent
Awaitility.await()
.atMost(new Duration(3, TimeUnit.SECONDS))
.pollInterval(new Duration(1, TimeUnit.SECONDS))
.untilAsserted(() -> {
assertEquals(3, ShadowOneSignalRestClient.requests.size());
assertEquals("notifications/UUID/report_received", ShadowOneSignalRestClient.lastUrl);
assertTrue(System.currentTimeMillis() - calledTime >= delay * 1000);
});
}

/**
* @see #testNotificationReceivedNoSendReceivedRequest_WhenNotificationNotDisplayed
*/
Expand Down

0 comments on commit d4458ef

Please sign in to comment.