Skip to content

Commit b9f64f0

Browse files
committed
Switched testNestedGetTags to use a BlockingQueue
1 parent 13aecf6 commit b9f64f0

File tree

1 file changed

+20
-18
lines changed

1 file changed

+20
-18
lines changed

OneSignalSDK/unittest/src/test/java/com/test/onesignal/MainOneSignalClassRunner.java

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@
8989
import org.json.JSONObject;
9090
import org.junit.After;
9191
import org.junit.AfterClass;
92+
import org.junit.Assert;
9293
import org.junit.Before;
9394
import org.junit.BeforeClass;
9495
import org.junit.Test;
@@ -110,6 +111,8 @@
110111
import java.util.Iterator;
111112
import java.util.List;
112113
import java.util.Map;
114+
import java.util.concurrent.ArrayBlockingQueue;
115+
import java.util.concurrent.BlockingQueue;
113116
import java.util.concurrent.atomic.AtomicBoolean;
114117
import java.util.regex.Pattern;
115118

@@ -3817,14 +3820,17 @@ public void sendsExternalIdOnEmailPlayers() throws Exception {
38173820

38183821
@Test
38193822
public void testGetTagsQueuesCallbacks() throws Exception {
3823+
final BlockingQueue<Boolean> queue = new ArrayBlockingQueue<>(2);
38203824

38213825
// Allows us to validate that both handlers get executed independently
38223826
class DebugGetTagsHandler implements OneSignal.GetTagsHandler {
3823-
boolean executed = false;
3824-
38253827
@Override
38263828
public void tagsAvailable(JSONObject tags) {
3827-
executed = true;
3829+
try {
3830+
queue.put(true);
3831+
} catch (InterruptedException e) {
3832+
Assert.fail("Throw unexpected");
3833+
}
38283834
}
38293835
}
38303836

@@ -3840,50 +3846,46 @@ public void tagsAvailable(JSONObject tags) {
38403846
OneSignal.getTags(first);
38413847
OneSignal.getTags(second);
38423848
threadAndTaskWait();
3843-
// TODO: Need a clean up if this is stable
3844-
synchronized (first) { try { if (!first.executed) first.wait(); } catch (InterruptedException e) {} }
3845-
synchronized (second) { try { if (!second.executed) second.wait(); } catch (InterruptedException e) {} }
38463849

3847-
assertTrue(first.executed);
3848-
assertTrue(second.executed);
3850+
assertTrue(queue.take());
3851+
assertTrue(queue.take());
38493852
}
38503853

38513854
@Test
38523855
public void testNestedGetTags() throws Exception {
3856+
final BlockingQueue<Boolean> queue = new ArrayBlockingQueue<>(2);
38533857

38543858
// Validates that nested getTags calls won't throw a ConcurrentModificationException
38553859
class DebugGetTagsHandler implements OneSignal.GetTagsHandler {
3856-
boolean executed = false;
3857-
38583860
@Override
38593861
public void tagsAvailable(JSONObject tags) {
38603862
OneSignal.getTags(new OneSignal.GetTagsHandler() {
38613863
@Override
38623864
public void tagsAvailable(JSONObject tags) {
3863-
synchronized (this) {
3864-
executed = true;
3865-
this.notifyAll();
3865+
try {
3866+
queue.put(true);
3867+
} catch (InterruptedException e) {
3868+
Assert.fail("Throw unexpected");
38663869
}
38673870
}
38683871
});
38693872
}
38703873
}
38713874

3872-
DebugGetTagsHandler first = new DebugGetTagsHandler();
3873-
DebugGetTagsHandler second = new DebugGetTagsHandler();
3874-
38753875
OneSignalInit();
38763876
threadAndTaskWait();
38773877

38783878
OneSignal.sendTag("test", "value");
38793879
threadAndTaskWait();
38803880

3881+
DebugGetTagsHandler first = new DebugGetTagsHandler();
3882+
DebugGetTagsHandler second = new DebugGetTagsHandler();
38813883
OneSignal.getTags(first);
38823884
OneSignal.getTags(second);
38833885
threadAndTaskWait();
38843886

3885-
assertTrue(first.executed);
3886-
assertTrue(second.executed);
3887+
assertTrue(queue.take());
3888+
assertTrue(queue.take());
38873889
}
38883890

38893891
/**

0 commit comments

Comments
 (0)