Skip to content

Commit 9cc5919

Browse files
committed
Add a test for push registration with userId, fix threading issue
1 parent 75fa9bb commit 9cc5919

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

iterableapi/src/main/java/com/iterable/iterableapi/IterableApi.java

Lines changed: 19 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -525,25 +525,27 @@ public void registerDeviceToken(final String applicationName, final String token
525525
}
526526

527527
if (token != null) {
528-
new Thread(new Runnable() {
528+
final Thread registrationThread = new Thread(new Runnable() {
529529
public void run() {
530-
if (getUserId() != null) {
531-
createUserForUserId(new IterableHelper.SuccessHandler() {
532-
@Override
533-
public void onSuccess(JSONObject data) {
534-
registerDeviceToken(applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
535-
}
536-
}, new IterableHelper.FailureHandler() {
537-
@Override
538-
public void onFailure(String reason, JSONObject data) {
539-
IterableLogger.e(TAG, "Could not create user: " + reason);
540-
}
541-
});
542-
} else {
543-
registerDeviceToken(applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
544-
}
530+
registerDeviceToken(applicationName, token, IterableConstants.MESSAGING_PLATFORM_FIREBASE, null);
545531
}
546-
}).start();
532+
});
533+
534+
if (getUserId() != null) {
535+
createUserForUserId(new IterableHelper.SuccessHandler() {
536+
@Override
537+
public void onSuccess(JSONObject data) {
538+
registrationThread.start();
539+
}
540+
}, new IterableHelper.FailureHandler() {
541+
@Override
542+
public void onFailure(String reason, JSONObject data) {
543+
IterableLogger.e(TAG, "Could not create user: " + reason);
544+
}
545+
});
546+
} else {
547+
registrationThread.start();
548+
}
547549
}
548550
}
549551

iterableapi/src/test/java/com/iterable/iterableapi/IterableApiTest.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -240,4 +240,23 @@ public void testPushRegistrationDeviceFields() throws Exception {
240240
assertEquals(IterableConstants.ITBL_KEY_SDK_VERSION_NUMBER, dataFields.getString("iterableSdkVersion"));
241241
}
242242

243+
@Test
244+
public void testPushRegistrationWithUserId() throws Exception {
245+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
246+
server.enqueue(new MockResponse().setResponseCode(200).setBody("{}"));
247+
248+
IterableApi.initialize(RuntimeEnvironment.application, "apiKey", new IterableConfig.Builder().setAutoPushRegistration(false).build());
249+
IterableApi.getInstance().setUserId("testUserId");
250+
IterableApi.getInstance().registerDeviceToken("pushIntegration", "token", IterableConstants.MESSAGING_PLATFORM_FIREBASE);
251+
Thread.sleep(1000); // Since the network request is queued from a background thread, we need to wait
252+
Robolectric.flushBackgroundThreadScheduler();
253+
RecordedRequest createUserRequest = server.takeRequest(1, TimeUnit.SECONDS);
254+
assertNotNull(createUserRequest);
255+
assertEquals("/" + IterableConstants.ENDPOINT_CREATE_USERID, createUserRequest.getPath());
256+
257+
RecordedRequest registerDeviceRequest = server.takeRequest(1, TimeUnit.SECONDS);
258+
assertNotNull(registerDeviceRequest);
259+
assertEquals("/" + IterableConstants.ENDPOINT_REGISTER_DEVICE_TOKEN, registerDeviceRequest.getPath());
260+
}
261+
243262
}

0 commit comments

Comments
 (0)