forked from fabric8io/kubernetes-client
-
Notifications
You must be signed in to change notification settings - Fork 0
/
JettyWebSocketTest.java
265 lines (243 loc) · 8.97 KB
/
JettyWebSocketTest.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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/**
* Copyright (C) 2015 Red Hat, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.fabric8.kubernetes.client.jetty;
import io.fabric8.kubernetes.client.http.WebSocket;
import org.assertj.core.api.InstanceOfAssertFactories;
import org.eclipse.jetty.util.BufferUtil;
import org.eclipse.jetty.websocket.api.Session;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.util.LinkedHashMap;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.RETURNS_DEEP_STUBS;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
class JettyWebSocketTest {
@Test
@DisplayName("Remote WebSocket binary message, notifies first onMessage with no back pressure")
void webSocketBinaryNotifiesOnMessage() {
// Given
final var listener = new Listener();
// When
new JettyWebSocket(listener).onWebSocketBinary(new byte[] { 1, 3, 3, 7 }, 0, 4);
// Then
assertThat(listener.events)
.containsOnlyKeys("onMessage")
.extracting("onMessage", InstanceOfAssertFactories.type(Object[].class))
.extracting(o -> o[0], InstanceOfAssertFactories.type(ByteBuffer.class))
.extracting(BufferUtil::toArray, InstanceOfAssertFactories.type(byte[].class))
.isEqualTo(new byte[] { 1, 3, 3, 7 });
}
@Test
@DisplayName("Remote WebSocket text message, notifies first onMessage with no back pressure")
void webSocketTextNotifiesOnMessage() {
// Given
final var listener = new Listener();
// When
new JettyWebSocket(listener).onWebSocketText("the message");
// Then
assertThat(listener.events)
.containsOnlyKeys("onMessage")
.extracting("onMessage", InstanceOfAssertFactories.type(Object[].class))
.extracting(o -> o[0], InstanceOfAssertFactories.type(String.class))
.isEqualTo("the message");
}
@Test
@DisplayName("Remote WebSocket close, notifies onClose")
void webSocketCloseNotifiesOnClose() {
// Given
final var listener = new Listener();
// When
new JettyWebSocket(listener).onWebSocketClose(1337, "closed");
// Then
assertThat(listener.events)
.containsOnly(entry("onClose", new Object[] { 1337, "closed" }));
}
@Test
@DisplayName("Remote WebSocket connect, notifies onOpen")
void webSocketConnectNotifiesOnOpen() {
// Given
final var listener = new Listener();
// When
new JettyWebSocket(listener).onWebSocketConnect(null);
// Then
assertThat(listener.events).containsOnlyKeys("onOpen");
}
@Test
@DisplayName("Remote WebSocket error, notifies onError")
void webSocketErrorNotifiesOnError() {
// Given
final var listener = new Listener();
// When
new JettyWebSocket(listener).onWebSocketError(new Exception("WebSocket Error!"));
// Then
assertThat(listener.events)
.containsOnlyKeys("onError")
.extracting("onError", InstanceOfAssertFactories.type(Object[].class))
.extracting(o -> o[0], InstanceOfAssertFactories.throwable(Exception.class))
.hasMessage("WebSocket Error!");
}
@Test
@DisplayName("Remote WebSocket error, ignored if connection is already closed and is ClosedChannelException")
void webSocketErrorIgnoredWhenClosed() {
// Given
final var listener = new Listener();
final var jws = new JettyWebSocket(listener);
jws.onWebSocketClose(1000, "closed");
// When
jws.onWebSocketError(new ClosedChannelException());
// Then
assertThat(listener.events)
.containsOnlyKeys("onClose");
}
@Test
@DisplayName("Remote WebSocket error, notifies onClose if connection is already closed and is NOT ClosedChannelException")
void webSocketErrorNotifiesOnErrorWhenClosedAndNotClosedChannelException() {
// Given
final var listener = new Listener();
final var jws = new JettyWebSocket(listener);
jws.onWebSocketClose(1000, "closed");
// When
jws.onWebSocketError(new Exception("NOT ClosedChannelException"));
// Then
assertThat(listener.events)
.containsOnlyKeys("onClose", "onError")
.extracting("onError", InstanceOfAssertFactories.type(Object[].class))
.extracting(o -> o[0], InstanceOfAssertFactories.throwable(Exception.class))
.hasMessage("NOT ClosedChannelException");
}
@Test
@DisplayName("backPressure, onWebSocketText processes first frame and waits for request() call")
void backPressure() throws Exception {
final var executor = Executors.newSingleThreadExecutor();
try {
final var buffer = new StringBuffer();
final var messages = new String[] { "Hell", "o ", "World!" };
final BlockingQueue<String> lock = new ArrayBlockingQueue<>(3);
final var ws = new JettyWebSocket(new WebSocket.Listener() {
@Override
public void onMessage(WebSocket webSocket, String text) {
buffer.append(text);
}
});
executor.execute(() -> {
for (var m : messages) {
ws.onWebSocketText(m);
assertTrue(lock.offer(m));
}
});
lock.poll(1, TimeUnit.SECONDS);
assertThat(buffer).hasToString("Hell");
ws.request();
lock.poll(1, TimeUnit.SECONDS);
assertThat(buffer).hasToString("Hello ");
ws.request();
lock.poll(1, TimeUnit.SECONDS);
assertThat(buffer).hasToString("Hello World!");
} finally {
executor.shutdownNow();
}
}
@Test
@DisplayName("sendClose, sends close message if connection is open")
void sendCloseWhenConnectionIsOpen() {
// Given
final var jws = new JettyWebSocket(new Listener());
final var session = mock(Session.class);
jws.onWebSocketConnect(session);
when(session.isOpen()).thenReturn(true);
// When
jws.sendClose(1000, "Closing");
// Then
verify(session).close(1000, "Closing");
}
@Test
@DisplayName("sendClose, ignored if connection is closed")
void sendCloseIgnoredWhenConnectionIsClosed() {
// Given
final var jws = new JettyWebSocket(new Listener());
final var session = mock(Session.class);
jws.onWebSocketConnect(session);
when(session.isOpen()).thenReturn(false);
// When
jws.sendClose(1000, "Closing");
// Then
verify(session, times(0)).close(anyInt(), anyString());
}
@Test
@DisplayName("sendClose, ignored if connection is already closed")
void sendCloseIgnoredWhenAlreadyClosed() {
// Given
final var jws = new JettyWebSocket(new Listener());
final var session = mock(Session.class);
jws.onWebSocketConnect(session);
when(session.isOpen()).thenReturn(true);
jws.sendClose(1000, "Closing");
// When
jws.sendClose(1000, "Closing twice");
// Then
verify(session, times(1)).close(anyInt(), anyString());
verify(session).close(1000, "Closing");
}
@Test
@DisplayName("send increases queueSize")
void sendIncreasesQueueSize() {
// Given
final var jws = new JettyWebSocket(new Listener());
final var session = mock(Session.class, RETURNS_DEEP_STUBS);
jws.onWebSocketConnect(session);
when(session.isOpen()).thenReturn(true);
// When
jws.send(ByteBuffer.wrap(new byte[] { 1, 3, 3, 7 }));
// Then
assertThat(jws.queueSize()).isEqualTo(4L);
}
private static final class Listener implements WebSocket.Listener {
private final LinkedHashMap<String, Object[]> events = new LinkedHashMap<>();
@Override
public void onOpen(WebSocket webSocket) {
events.put("onOpen", null);
}
@Override
public void onMessage(WebSocket webSocket, String text) {
events.put("onMessage", new Object[] { text });
}
@Override
public void onMessage(WebSocket webSocket, ByteBuffer bytes) {
events.put("onMessage", new Object[] { bytes });
}
@Override
public void onClose(WebSocket webSocket, int code, String reason) {
events.put("onClose", new Object[] { code, reason });
}
@Override
public void onError(WebSocket webSocket, Throwable error) {
events.put("onError", new Object[] { error });
}
}
}