1+ package com .clusterws ;
2+
3+ import com .alibaba .fastjson .JSONArray ;
4+ import com .alibaba .fastjson .JSONObject ;
5+ import org .junit .Before ;
6+ import org .junit .Test ;
7+
8+ import java .util .ArrayList ;
9+
10+ import static org .junit .Assert .*;
11+
12+ public class ChannelServerTest {
13+ private ClusterWS mClusterWS ;
14+ private boolean mGotTheData ;
15+ private Object mReceivedData ;
16+
17+ @ Before
18+ public void init () throws Exception {
19+ mClusterWS = new ClusterWS ("ws://localhost:80" );
20+ mGotTheData = false ;
21+ mReceivedData = null ;
22+ }
23+
24+ @ Test
25+ public void testChannelPublishAndWatchInt () throws Exception {
26+ mClusterWS .connect ();
27+ Thread .sleep (1000 );
28+ mClusterWS .subscribe ("testChannel" )
29+ .watch (new Channel .IChannelListener () {
30+ @ Override
31+ public void onDataReceived (String channelName , Object data ) {
32+ mGotTheData = true ;
33+ mReceivedData = data ;
34+ }
35+ })
36+ .publish (24 );
37+ Thread .sleep (1000 );
38+
39+ assertTrue ("Did not get the data" , mGotTheData );
40+ assertEquals ("Data send and data received are not the same" , 24 , mReceivedData );
41+ }
42+
43+ @ Test
44+ public void testChannelPublishAndWatchString () throws Exception {
45+ mClusterWS .connect ();
46+ Thread .sleep (1000 );
47+ mClusterWS .subscribe ("testChannel" )
48+ .watch (new Channel .IChannelListener () {
49+ @ Override
50+ public void onDataReceived (String channelName , Object data ) {
51+ mGotTheData = true ;
52+ mReceivedData = data ;
53+ }
54+ })
55+ .publish ("test string" );
56+ Thread .sleep (1000 );
57+
58+ assertTrue ("Did not get the data" , mGotTheData );
59+ assertEquals ("Data send and data received are not the same" , "test string" , mReceivedData );
60+ }
61+
62+ @ Test
63+ public void testChannelPublishAndWatchObject () throws Exception {
64+ JSONObject jsonObject = new JSONObject ();
65+ jsonObject .put ("int" , 30 );
66+ jsonObject .put ("bool" , true );
67+ jsonObject .put ("string" , "CHLEN" );
68+ JSONArray jsonArray = new JSONArray ();
69+ jsonArray .add (30 );
70+ jsonArray .add (true );
71+ jsonArray .add ("CHLEN" );
72+ jsonObject .put ("array" , jsonArray );
73+
74+ mClusterWS .connect ();
75+ Thread .sleep (1000 );
76+ mClusterWS .subscribe ("testChannel" )
77+ .watch (new Channel .IChannelListener () {
78+ @ Override
79+ public void onDataReceived (String channelName , Object data ) {
80+ mGotTheData = true ;
81+ mReceivedData = data ;
82+ }
83+ })
84+ .publish (jsonObject );
85+ Thread .sleep (1000 );
86+
87+ assertTrue ("Did not get the data" , mGotTheData );
88+ assertEquals ("Data send and data received are not the same" , jsonObject , mReceivedData );
89+ }
90+
91+ @ Test
92+ public void testChannelPublishAndWatchNull () throws Exception {
93+ mClusterWS .connect ();
94+ Thread .sleep (1000 );
95+ mClusterWS .subscribe ("testChannel" )
96+ .watch (new Channel .IChannelListener () {
97+ @ Override
98+ public void onDataReceived (String channelName , Object data ) {
99+ mGotTheData = true ;
100+ mReceivedData = data ;
101+ }
102+ })
103+ .publish (null );
104+ Thread .sleep (1000 );
105+
106+ assertTrue ("Did not get the data" , mGotTheData );
107+ assertEquals ("Data send and data received are not the same" , null , mReceivedData );
108+ }
109+
110+ @ Test
111+ public void testChannelPublishAndWatchArray () throws Exception {
112+ ArrayList <Object > mObjectArrayList ;
113+ mObjectArrayList = new ArrayList <>();
114+ mObjectArrayList .add (30 );
115+ mObjectArrayList .add (false );
116+ mObjectArrayList .add ("Test message" );
117+
118+ JSONObject jsonObject = new JSONObject ();
119+ jsonObject .put ("int" , 30 );
120+ jsonObject .put ("bool" , true );
121+ jsonObject .put ("string" , "CHLEN" );
122+ JSONArray jsonArray = new JSONArray ();
123+ jsonArray .add (30 );
124+ jsonArray .add (true );
125+ jsonArray .add ("CHLEN" );
126+ jsonObject .put ("array" , jsonArray );
127+
128+ mObjectArrayList .add (jsonObject );
129+
130+ mClusterWS .connect ();
131+ Thread .sleep (1000 );
132+ mClusterWS .subscribe ("testChannel" )
133+ .watch (new Channel .IChannelListener () {
134+ @ Override
135+ public void onDataReceived (String channelName , Object data ) {
136+ mGotTheData = true ;
137+ mReceivedData = data ;
138+ }
139+ })
140+ .publish (mObjectArrayList );
141+ Thread .sleep (1000 );
142+
143+ assertTrue ("Did not get the data" , mGotTheData );
144+ assertEquals ("Data send and data received are not the same" , mObjectArrayList , mReceivedData );
145+ }
146+
147+ @ Test
148+ public void testGetChannelByName () throws Exception {
149+ mClusterWS .connect ();
150+ Thread .sleep (1000 );
151+ mClusterWS .subscribe ("testChannel" );
152+ assertNotNull (mClusterWS .getChannelByName ("testChannel" ));
153+ }
154+
155+ @ Test
156+ public void testGetChannelList () throws Exception {
157+ mClusterWS .connect ();
158+ Thread .sleep (1000 );
159+ mClusterWS .subscribe ("testChannel1" );
160+ mClusterWS .subscribe ("testChannel2" );
161+ mClusterWS .subscribe ("testChannel3" );
162+
163+ assertEquals (mClusterWS .getChannels ().size (), 3 );
164+ assertEquals (mClusterWS .getChannels ().get (0 ).getChannelName (), "testChannel1" );
165+ assertEquals (mClusterWS .getChannels ().get (1 ).getChannelName (), "testChannel2" );
166+ assertEquals (mClusterWS .getChannels ().get (2 ).getChannelName (), "testChannel3" );
167+ }
168+
169+ @ Test
170+ public void testTryGetChannelByNameAfterUnsubscribing () throws Exception {
171+ mClusterWS .connect ();
172+ Thread .sleep (1000 );
173+ Channel channel = mClusterWS .subscribe ("testChannel1" );
174+ channel .unsubscribe ();
175+ assertNull (mClusterWS .getChannelByName ("testChannel1" ));
176+ }
177+
178+ @ Test
179+ public void testResubscribeOnAllChannelsAfterReconnection () throws Exception {
180+ mClusterWS .setReconnection (true , 1000 , 2000 , null );
181+ mClusterWS .connect ();
182+ Thread .sleep (1000 );
183+ Channel channel = mClusterWS .subscribe ("testChannel" )
184+ .watch (new Channel .IChannelListener () {
185+ @ Override
186+ public void onDataReceived (String channelName , Object data ) {
187+ mGotTheData = true ;
188+ mReceivedData = data ;
189+ }
190+ });
191+ mClusterWS .disconnect (4001 , "hui" );
192+ Thread .sleep (1000 );
193+ channel .publish ("testData" );
194+ Thread .sleep (1000 );
195+ assertTrue ("Did not get the data" , mGotTheData );
196+ assertEquals ("Data send and data received are not the same" , "testData" , mReceivedData );
197+ }
198+
199+ }
0 commit comments