89
89
import org .json .JSONObject ;
90
90
import org .junit .After ;
91
91
import org .junit .AfterClass ;
92
+ import org .junit .Assert ;
92
93
import org .junit .Before ;
93
94
import org .junit .BeforeClass ;
94
95
import org .junit .Test ;
110
111
import java .util .Iterator ;
111
112
import java .util .List ;
112
113
import java .util .Map ;
114
+ import java .util .concurrent .ArrayBlockingQueue ;
115
+ import java .util .concurrent .BlockingQueue ;
113
116
import java .util .concurrent .atomic .AtomicBoolean ;
114
117
import java .util .regex .Pattern ;
115
118
@@ -3817,14 +3820,17 @@ public void sendsExternalIdOnEmailPlayers() throws Exception {
3817
3820
3818
3821
@ Test
3819
3822
public void testGetTagsQueuesCallbacks () throws Exception {
3823
+ final BlockingQueue <Boolean > queue = new ArrayBlockingQueue <>(2 );
3820
3824
3821
3825
// Allows us to validate that both handlers get executed independently
3822
3826
class DebugGetTagsHandler implements OneSignal .GetTagsHandler {
3823
- boolean executed = false ;
3824
-
3825
3827
@ Override
3826
3828
public void tagsAvailable (JSONObject tags ) {
3827
- executed = true ;
3829
+ try {
3830
+ queue .put (true );
3831
+ } catch (InterruptedException e ) {
3832
+ Assert .fail ("Throw unexpected" );
3833
+ }
3828
3834
}
3829
3835
}
3830
3836
@@ -3840,50 +3846,46 @@ public void tagsAvailable(JSONObject tags) {
3840
3846
OneSignal .getTags (first );
3841
3847
OneSignal .getTags (second );
3842
3848
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 ) {} }
3846
3849
3847
- assertTrue (first . executed );
3848
- assertTrue (second . executed );
3850
+ assertTrue (queue . take () );
3851
+ assertTrue (queue . take () );
3849
3852
}
3850
3853
3851
3854
@ Test
3852
3855
public void testNestedGetTags () throws Exception {
3856
+ final BlockingQueue <Boolean > queue = new ArrayBlockingQueue <>(2 );
3853
3857
3854
3858
// Validates that nested getTags calls won't throw a ConcurrentModificationException
3855
3859
class DebugGetTagsHandler implements OneSignal .GetTagsHandler {
3856
- boolean executed = false ;
3857
-
3858
3860
@ Override
3859
3861
public void tagsAvailable (JSONObject tags ) {
3860
3862
OneSignal .getTags (new OneSignal .GetTagsHandler () {
3861
3863
@ Override
3862
3864
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" );
3866
3869
}
3867
3870
}
3868
3871
});
3869
3872
}
3870
3873
}
3871
3874
3872
- DebugGetTagsHandler first = new DebugGetTagsHandler ();
3873
- DebugGetTagsHandler second = new DebugGetTagsHandler ();
3874
-
3875
3875
OneSignalInit ();
3876
3876
threadAndTaskWait ();
3877
3877
3878
3878
OneSignal .sendTag ("test" , "value" );
3879
3879
threadAndTaskWait ();
3880
3880
3881
+ DebugGetTagsHandler first = new DebugGetTagsHandler ();
3882
+ DebugGetTagsHandler second = new DebugGetTagsHandler ();
3881
3883
OneSignal .getTags (first );
3882
3884
OneSignal .getTags (second );
3883
3885
threadAndTaskWait ();
3884
3886
3885
- assertTrue (first . executed );
3886
- assertTrue (second . executed );
3887
+ assertTrue (queue . take () );
3888
+ assertTrue (queue . take () );
3887
3889
}
3888
3890
3889
3891
/**
0 commit comments