Skip to content

Commit 3538bbb

Browse files
Fix flaky PII masking tests with deterministic init state
The tests raced initializeInBackground() against setEmail/setUserId — if background init completed before the API calls, operations executed immediately instead of being queued, failing the size assertion. Fix: use simulateInitializingState() to hold init in progress, then simulateInitializationComplete() after assertions. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
1 parent 20b97b9 commit 3538bbb

1 file changed

Lines changed: 16 additions & 34 deletions

File tree

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

Lines changed: 16 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1036,15 +1036,9 @@ public void onSDKInitialized() {
10361036

10371037
@Test
10381038
public void testPIIMasking_EmailMasked() throws InterruptedException {
1039-
CountDownLatch initLatch = new CountDownLatch(1);
1040-
1041-
// Start background initialization
1042-
IterableApi.initializeInBackground(context, TEST_API_KEY, new IterableInitializationCallback() {
1043-
@Override
1044-
public void onSDKInitialized() {
1045-
initLatch.countDown();
1046-
}
1047-
});
1039+
// Hold initialization in progress so operations queue instead of executing
1040+
IterableBackgroundInitializer.simulateInitializingState();
1041+
IterableApi.initialize(context, TEST_API_KEY);
10481042

10491043
// Use sensitive PII data that should be masked
10501044
String sensitiveEmail = "sensitive.user@company.com";
@@ -1085,8 +1079,8 @@ public void onSDKInitialized() {
10851079
}
10861080
}
10871081

1088-
// Wait for initialization to complete
1089-
assertTrue("Initialization should complete", waitForAsyncInitialization(initLatch, 3));
1082+
// Complete initialization
1083+
IterableBackgroundInitializer.simulateInitializationComplete();
10901084
}
10911085

10921086
@Test
@@ -1130,21 +1124,15 @@ public void testPIIMasking_SingleCharacterHandled() {
11301124

11311125
@Test
11321126
public void testPIIMasking_AuthTokenMasked() throws InterruptedException {
1133-
CountDownLatch initLatch = new CountDownLatch(1);
1134-
1135-
// Start background initialization
1136-
IterableApi.initializeInBackground(context, TEST_API_KEY, new IterableInitializationCallback() {
1137-
@Override
1138-
public void onSDKInitialized() {
1139-
initLatch.countDown();
1140-
}
1141-
});
1127+
// Hold initialization in progress so setEmail/setUserId queue instead of execute
1128+
IterableBackgroundInitializer.simulateInitializingState();
1129+
IterableApi.initialize(context, TEST_API_KEY);
11421130

11431131
// Use sensitive auth token
11441132
String sensitiveEmail = "test@example.com";
11451133
String sensitiveAuthToken = "SecretAuthToken12345";
11461134

1147-
// Make API calls with auth tokens
1135+
// Make API calls with auth tokens — these should queue since init is "in progress"
11481136
IterableApi.getInstance().setEmail(sensitiveEmail, sensitiveAuthToken);
11491137
IterableApi.getInstance().setUserId("testuser", sensitiveAuthToken);
11501138

@@ -1165,21 +1153,15 @@ public void onSDKInitialized() {
11651153
}
11661154
}
11671155

1168-
// Wait for initialization
1169-
assertTrue("Initialization should complete", waitForAsyncInitialization(initLatch, 3));
1156+
// Complete initialization
1157+
IterableBackgroundInitializer.simulateInitializationComplete();
11701158
}
11711159

11721160
@Test
11731161
public void testPIIMasking_VerifyExactFormat() throws InterruptedException {
1174-
CountDownLatch initLatch = new CountDownLatch(1);
1175-
1176-
// Start background initialization
1177-
IterableApi.initializeInBackground(context, TEST_API_KEY, new IterableInitializationCallback() {
1178-
@Override
1179-
public void onSDKInitialized() {
1180-
initLatch.countDown();
1181-
}
1182-
});
1162+
// Hold initialization in progress so operations queue instead of executing
1163+
IterableBackgroundInitializer.simulateInitializingState();
1164+
IterableApi.initialize(context, TEST_API_KEY);
11831165

11841166
// Test various PII formats
11851167
String email1 = "john.doe@example.com"; // Should mask to "j***"
@@ -1225,8 +1207,8 @@ public void onSDKInitialized() {
12251207
assertTrue("UserId 'user_123_abc' should be masked to 'u***'", foundUserId1Masked);
12261208
assertTrue("Email 'a@b.com' should be masked to 'a***'", foundEmail2Masked);
12271209

1228-
// Wait for initialization
1229-
assertTrue("Initialization should complete", waitForAsyncInitialization(initLatch, 3));
1210+
// Complete initialization
1211+
IterableBackgroundInitializer.simulateInitializationComplete();
12301212
}
12311213

12321214
// ========================================

0 commit comments

Comments
 (0)