-
Notifications
You must be signed in to change notification settings - Fork 0
/
WebSocketEJBTest.java
237 lines (181 loc) · 8.68 KB
/
WebSocketEJBTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package io.gitbooks.abhirockzz.jwah.websocketejb;
import java.io.IOException;
import java.net.URI;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import javax.websocket.CloseReason;
import javax.websocket.ContainerProvider;
import javax.websocket.DeploymentException;
import javax.websocket.Endpoint;
import javax.websocket.EndpointConfig;
import javax.websocket.MessageHandler;
import javax.websocket.Session;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import static org.junit.Assert.*;
public class WebSocketEJBTest {
public WebSocketEJBTest() {
}
static final int NUM_OF_THREADS = 5;
ExecutorService es = null;
CountDownLatch msgReceivedLatch = null;
CountDownLatch startLatch = null;
CountDownLatch closeConnLatch = null;
@Before
public void setUp() {
es = Executors.newFixedThreadPool(NUM_OF_THREADS);
msgReceivedLatch = new CountDownLatch(NUM_OF_THREADS);
startLatch = new CountDownLatch(1);
closeConnLatch = new CountDownLatch(1);
}
@After
public void tearDown() {
msgReceivedLatch = null;
startLatch = null;
closeConnLatch = null;
es.shutdown();
es = null;
}
final static String BASE_URL = "ws://localhost:8080/websocket-ejb/";
/**
* Used to assert that a SLSB instance is obtained from the EJB pool (if
* needed) for a WebSocket client. The server endpoint returns the hashCode
* of the EJB as a response message and the same is stored (in a
* Set<String>) by the client. For a successful scenario, it's sufficient to
* confirm that the Set contains more than 1 entries (at least 2 SLSBs were
* created)
*
* @throws DeploymentException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void aSLSBPickedFromPoolForEachClient() throws DeploymentException, IOException, InterruptedException {
final ProgrammaticWebsocketClient client = new ProgrammaticWebsocketClient(msgReceivedLatch);
ConnectToServer connectToServer = new ConnectToServer(client, BASE_URL + "stateless/", startLatch, closeConnLatch);
for (int i = 1; i <= NUM_OF_THREADS; i++) {
es.execute(connectToServer);
}
startLatch.countDown();
System.out.println("All threads started");
assertTrue("Please try test with more than ONE thread", NUM_OF_THREADS > 1);
assertTrue(msgReceivedLatch.await(NUM_OF_THREADS, TimeUnit.SECONDS)); //1 sec per thread
System.out.println(client.getResponses().size() + " SLSB instances were created");
assertTrue("at least two different SLSB should have been created by the container", client.getResponses().size() > 1);
closeConnLatch.countDown(); // initiate session close
}
/**
* Used to assert the same SLSB instance is used throughout the lifecycle of
* a WebSocket session i.e. connect, send/receive messages, disconnect (The
* instance is obtained from the EJB pool)
*
* The server endpoint returns the hashCode of the EJB as a response message
* via the onOpen callback and a hello (appended by the hashCode) in the
* onMessage callback, and the same is stored (in a Set<String>) by the
* client. For a successful scenario, it's sufficient to confirm that the
* Set contains 2 entries - 1 response each the onOpen callback and one
* response (only unique entries in a Set) from onMessage callback
*
* @throws DeploymentException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void theSameSLSBInstanceIsUsedForMultipleInteractionsWithinASession() throws DeploymentException, IOException, InterruptedException {
ProgrammaticWebsocketClient client = new ProgrammaticWebsocketClient(msgReceivedLatch);
Session session = ContainerProvider.getWebSocketContainer()
.connectToServer(client,
URI.create(BASE_URL + "stateless/"));
SendMessageToServerEndpoint sendMsg = new SendMessageToServerEndpoint(session, startLatch, closeConnLatch);
for (int n = 1; n <= NUM_OF_THREADS; n++) {
es.execute(sendMsg);
}
startLatch.countDown();
System.out.println("All threads started");
msgReceivedLatch.await(); //wait for acitivity to finish
assertEquals(2, client.getResponses().size()); //
closeConnLatch.countDown(); // initiate session close
}
/**
* Used to assert that the SAME singleton EJB instance is invoked for
* establishing a connection with different clients (threads). The server
* endpoint returns the hashCode of the EJB as a response message and the
* same is stored (in a Set<String>) by the client. For a successful
* scenario, it's sufficient to confirm that the Set contains ONLY 1 entry
* (only unique entries in a Set)
*
* @throws DeploymentException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void theSameSingletonInstanceIsUsedForAllSessions() throws DeploymentException, IOException, InterruptedException {
final ProgrammaticWebsocketClient client = new ProgrammaticWebsocketClient(msgReceivedLatch);
ConnectToServer connectToServer = new ConnectToServer(client, BASE_URL + "singleton/", startLatch, closeConnLatch);
for (int i = 1; i <= NUM_OF_THREADS; i++) {
//new Thread(connectToServer).start();
es.execute(connectToServer);
}
startLatch.countDown();
System.out.println("All threads started");
assertTrue(msgReceivedLatch.await(NUM_OF_THREADS, TimeUnit.SECONDS)); //1 sec per thread
assertEquals(1, client.getResponses().size());
closeConnLatch.countDown(); // initiate session close
}
/**
* Used to assert that a unique Stateful bean is obtained from the EJB pool (if
* needed) for each WebSocket client. The server endpoint returns the hashCode
* of the EJB as a response message and the same is stored (in a
* Set<String>) by the client. For a successful scenario, it's sufficient to
* confirm that the Set contains as many entries as the number of clients
*
* @throws DeploymentException
* @throws IOException
* @throws InterruptedException
*/
@Test
public void uniqueStatefulBeanIsAssignedToEachClient() throws DeploymentException, IOException, InterruptedException {
final ProgrammaticWebsocketClient client1 = new ProgrammaticWebsocketClient(msgReceivedLatch);
ConnectToServer connectToServer = new ConnectToServer(client1, BASE_URL + "stateful/", startLatch, closeConnLatch);
for (int i = 1; i <= NUM_OF_THREADS; i++) {
es.execute(connectToServer);
}
startLatch.countDown();
System.out.println("All threads started");
int timeout = NUM_OF_THREADS == 1 ? 5 : NUM_OF_THREADS; //if no. of threads is just one, then need to be slightly more liberal about the timout
assertTrue(msgReceivedLatch.await(timeout, TimeUnit.SECONDS)); //1 sec per thread
assertEquals(NUM_OF_THREADS, client1.getResponses().size());
closeConnLatch.countDown(); // initiate session close
}
public static class ProgrammaticWebsocketClient extends Endpoint {
CountDownLatch msgReceivedLatch;
public ProgrammaticWebsocketClient(CountDownLatch msgReceivedLatch) {
this.msgReceivedLatch = msgReceivedLatch;
}
public Set<String> getResponses() {
return Collections.unmodifiableSet(responses);
}
Set<String> responses = new HashSet<>();
@Override
public void onOpen(Session sn, EndpointConfig ec) {
sn.addMessageHandler(new MessageHandler.Whole<String>() {
@Override
public void onMessage(String t) {
System.out.println("Got msg from server -- " + t);
responses.add(t);
msgReceivedLatch.countDown();
}
});
}
@Override
public void onClose(Session session, CloseReason closeReason) {
System.out.println("closed " + session.getId() + " due to " + closeReason.getCloseCode());
}
}
}