33import static org .junit .Assert .*;
44
55import java .util .Hashtable ;
6+ import java .util .Random ;
67import java .util .concurrent .CountDownLatch ;
78import java .util .concurrent .TimeUnit ;
89
@@ -748,7 +749,7 @@ public void successCallback(String channel, Object message) {
748749 }
749750 });
750751 pubnub .subscribe (args );
751- latch .await (30 , TimeUnit .SECONDS );
752+ latch .await (60 , TimeUnit .SECONDS );
752753 assertEquals (20 , subscribeCb .getResponse ());
753754
754755
@@ -797,7 +798,7 @@ public void successCallback(String channel, Object message) {
797798 }
798799 });
799800 pubnub .subscribe (args );
800- latch .await (30 , TimeUnit .SECONDS );
801+ latch .await (60 , TimeUnit .SECONDS );
801802 assertNotEquals (10 , subscribeCb .getResponse ());
802803
803804
@@ -845,7 +846,7 @@ public void successCallback(String channel, Object message) {
845846
846847 });
847848 pubnub .subscribe (args );
848- latch .await (30 , TimeUnit .SECONDS );
849+ latch .await (60 , TimeUnit .SECONDS );
849850 assertNotEquals (10 , subscribeCb .getResponse ());
850851
851852
@@ -898,7 +899,7 @@ public void successCallback(String channel, Object message) {
898899 }
899900 });
900901 pubnub .subscribe (args );
901- latch .await (30 , TimeUnit .SECONDS );
902+ latch .await (60 , TimeUnit .SECONDS );
902903 assertNotEquals (20 , subscribeCb .getResponse ());
903904
904905
@@ -950,7 +951,7 @@ public void successCallback(String channel, Object message) {
950951 }
951952 });
952953 pubnub .subscribe (args );
953- latch .await (30 , TimeUnit .SECONDS );
954+ latch .await (60 , TimeUnit .SECONDS );
954955 assertNotEquals (20 , subscribeCb .getResponse ());
955956
956957
@@ -961,4 +962,108 @@ public void successCallback(String channel, Object message) {
961962 }
962963 }
963964
965+
966+
967+ @ Test
968+ public void testSubscribeInMultipleThreads () {
969+ final String channel = "java-unittest-" + Math .random ();
970+ final CountDownLatch latch = new CountDownLatch (100 );
971+ final Hashtable results = new Hashtable ();
972+ final Hashtable inputs = new Hashtable ();
973+ final int count = 100 ;
974+ final Random rand = new Random ();
975+ results .put ("connects" , 0 );
976+ results .put ("count" , 0 );
977+
978+ class SubscribeThread implements Runnable {
979+
980+
981+ class PublishThread implements Runnable {
982+ private String ch ;
983+ private int message ;
984+ PublishThread (String ch , int message ) {
985+ this .ch = ch ;
986+ this .message = message ;
987+ }
988+
989+ public void run () {
990+ try {
991+ Thread .sleep (rand .nextInt (20000 ));
992+ } catch (InterruptedException e1 ) {
993+ e1 .printStackTrace ();
994+ }
995+ Hashtable args = new Hashtable ();
996+ args .put ("channel" , ch );
997+ args .put ("message" , message );
998+ args .put ("callback" , new Callback (){
999+
1000+ @ Override
1001+ public void successCallback (String channel , Object message ) {
1002+ }
1003+ @ Override
1004+ public void errorCallback (String channel , PubnubError error ) {
1005+
1006+ }
1007+ });
1008+ pubnub .publish (args );
1009+ }
1010+ }
1011+
1012+ private String ch ;
1013+ SubscribeThread (String ch ) {
1014+ this .ch = ch ;
1015+ }
1016+ private void startPublish () {
1017+ for (int i = 1 ; i <= count ; i ++) {
1018+ inputs .put (channel + "-" + i , i );
1019+ new Thread (new PublishThread (channel + "-" + i , i )).start ();
1020+ }
1021+ }
1022+ public void run () {
1023+
1024+ Hashtable args = new Hashtable ();
1025+ args .put ("channel" , ch );
1026+ args .put ("callback" , new Callback (){
1027+
1028+ @ Override
1029+ public void connectCallback (String channel , Object message ) {
1030+
1031+ results .put ("connects" , (Integer )(results .get ("connects" )) + 1 );
1032+ if ((Integer )results .get ("connects" ) >= count )
1033+ startPublish ();
1034+ }
1035+ @ Override
1036+ public void successCallback (String channel , Object message ) {
1037+ int c = (results .get (channel ) == null )?0 :(Integer ) results .get (channel );
1038+ int d = (inputs .get (channel ) == null )?0 :(Integer ) inputs .get (channel );
1039+ assertNotEquals ((Integer )c , (Integer )message );
1040+ assertEquals ((Integer )d , (Integer )message );
1041+ results .put (channel , message );
1042+ results .put ("count" , (Integer )results .get ("count" ) + 1 );
1043+ }
1044+ @ Override
1045+ public void errorCallback (String channel , PubnubError error ) {
1046+ }
1047+ });
1048+ try {
1049+ pubnub .subscribe (args );
1050+ } catch (PubnubException e ) {
1051+ e .printStackTrace ();
1052+ }
1053+ }
1054+ }
1055+
1056+ for (int i = 1 ; i <= count ; i ++) {
1057+ inputs .put (channel + "-" + i , i );
1058+ new Thread (new SubscribeThread (channel + "-" + i )).start ();
1059+ }
1060+
1061+ try {
1062+ latch .await (90 , TimeUnit .SECONDS );
1063+ } catch (InterruptedException e ) {
1064+ e .printStackTrace ();
1065+ }
1066+ assertEquals ((Integer )count ,(Integer )results .get ("count" ));
1067+
1068+ }
9641069}
0 commit comments