Skip to content

Commit 61c52d6

Browse files
committed
Polish ConcurrentWebSocketSessionDecoratorTests
1 parent 282a4ad commit 61c52d6

File tree

2 files changed

+145
-92
lines changed

2 files changed

+145
-92
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.web.reactive.function.client;
17+
18+
import java.io.IOException;
19+
import java.time.Duration;
20+
import java.util.function.Consumer;
21+
22+
import io.netty.channel.group.ChannelGroup;
23+
import io.netty.channel.group.DefaultChannelGroup;
24+
import io.netty.util.concurrent.ImmediateEventExecutor;
25+
import okhttp3.mockwebserver.MockResponse;
26+
import okhttp3.mockwebserver.MockWebServer;
27+
import okhttp3.mockwebserver.RecordedRequest;
28+
import reactor.core.publisher.Mono;
29+
import reactor.netty.FutureMono;
30+
import reactor.netty.http.client.HttpClient;
31+
import reactor.netty.resources.ConnectionProvider;
32+
import reactor.netty.resources.LoopResources;
33+
import reactor.netty.tcp.TcpClient;
34+
import reactor.test.StepVerifier;
35+
36+
import org.springframework.http.HttpHeaders;
37+
import org.springframework.http.client.reactive.ClientHttpConnector;
38+
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
39+
import org.springframework.mock.web.test.server.MockWebSession;
40+
41+
import static org.junit.Assert.*;
42+
43+
/**
44+
*
45+
* @author Rossen Stoyanchev
46+
*/
47+
public class MyTest {
48+
49+
public static void main(String[] args) throws IOException {
50+
51+
LoopResources resources = LoopResources.create("test-loop");
52+
ConnectionProvider provider = ConnectionProvider.elastic("test-pool");
53+
TcpClient tcpClient = TcpClient.create(provider).runOn(resources, false);
54+
HttpClient httpClient = HttpClient.from(tcpClient);
55+
56+
WebClient webClient = WebClient.builder()
57+
.clientConnector(new ReactorClientHttpConnector(httpClient))
58+
.build();
59+
60+
makeCalls(webClient);
61+
62+
provider.dispose();
63+
resources.dispose();
64+
65+
//Mono<Void> result1 = FutureMono.from(channelGroup.close());
66+
//Mono<Void> result2 = connProvider.disposeLater();
67+
//Mono<Void> result3 = loopResources.disposeLater();
68+
//Mono.whenDelayError(result1, result2, result3).block(Duration.ofSeconds(5));
69+
70+
System.in.read();
71+
System.exit(0);
72+
73+
}
74+
75+
private static void makeCalls(WebClient webClient) {
76+
webClient.get().uri("http://httpbin.org/ip")
77+
.retrieve()
78+
.bodyToMono(String.class)
79+
.block(Duration.ofSeconds(5));
80+
}
81+
82+
}

spring-websocket/src/test/java/org/springframework/web/socket/handler/ConcurrentWebSocketSessionDecoratorTests.java

Lines changed: 63 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -32,103 +32,74 @@
3232
import static org.junit.Assert.*;
3333

3434
/**
35-
* Unit tests for
36-
* {@link org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator}.
37-
*
35+
* Unit tests for {@link ConcurrentWebSocketSessionDecorator}.
3836
* @author Rossen Stoyanchev
3937
*/
4038
@SuppressWarnings("resource")
4139
public class ConcurrentWebSocketSessionDecoratorTests {
4240

43-
4441
@Test
4542
public void send() throws IOException {
4643

4744
TestWebSocketSession session = new TestWebSocketSession();
4845
session.setOpen(true);
4946

50-
ConcurrentWebSocketSessionDecorator concurrentSession =
47+
ConcurrentWebSocketSessionDecorator decorator =
5148
new ConcurrentWebSocketSessionDecorator(session, 1000, 1024);
5249

5350
TextMessage textMessage = new TextMessage("payload");
54-
concurrentSession.sendMessage(textMessage);
51+
decorator.sendMessage(textMessage);
5552

5653
assertEquals(1, session.getSentMessages().size());
5754
assertEquals(textMessage, session.getSentMessages().get(0));
5855

59-
assertEquals(0, concurrentSession.getBufferSize());
60-
assertEquals(0, concurrentSession.getTimeSinceSendStarted());
56+
assertEquals(0, decorator.getBufferSize());
57+
assertEquals(0, decorator.getTimeSinceSendStarted());
6158
assertTrue(session.isOpen());
6259
}
6360

6461
@Test
6562
public void sendAfterBlockedSend() throws IOException, InterruptedException {
6663

67-
BlockingSession blockingSession = new BlockingSession();
68-
blockingSession.setOpen(true);
69-
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
64+
BlockingSession session = new BlockingSession();
65+
session.setOpen(true);
7066

71-
final ConcurrentWebSocketSessionDecorator concurrentSession =
72-
new ConcurrentWebSocketSessionDecorator(blockingSession, 10 * 1000, 1024);
67+
final ConcurrentWebSocketSessionDecorator decorator =
68+
new ConcurrentWebSocketSessionDecorator(session, 10 * 1000, 1024);
7369

74-
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
75-
TextMessage message = new TextMessage("slow message");
76-
try {
77-
concurrentSession.sendMessage(message);
78-
}
79-
catch (IOException e) {
80-
e.printStackTrace();
81-
}
82-
});
70+
sendBlockingMessage(decorator);
8371

84-
assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS));
85-
86-
// ensure some send time elapses
87-
Thread.sleep(100);
88-
assertTrue(concurrentSession.getTimeSinceSendStarted() > 0);
72+
Thread.sleep(50);
73+
assertTrue(decorator.getTimeSinceSendStarted() > 0);
8974

9075
TextMessage payload = new TextMessage("payload");
9176
for (int i = 0; i < 5; i++) {
92-
concurrentSession.sendMessage(payload);
77+
decorator.sendMessage(payload);
9378
}
9479

95-
assertTrue(concurrentSession.getTimeSinceSendStarted() > 0);
96-
assertEquals(5 * payload.getPayloadLength(), concurrentSession.getBufferSize());
97-
assertTrue(blockingSession.isOpen());
80+
assertTrue(decorator.getTimeSinceSendStarted() > 0);
81+
assertEquals(5 * payload.getPayloadLength(), decorator.getBufferSize());
82+
assertTrue(session.isOpen());
9883
}
9984

10085
@Test
10186
public void sendTimeLimitExceeded() throws IOException, InterruptedException {
10287

103-
BlockingSession blockingSession = new BlockingSession();
104-
blockingSession.setId("123");
105-
blockingSession.setOpen(true);
106-
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
107-
108-
int sendTimeLimit = 100;
109-
int bufferSizeLimit = 1024;
88+
BlockingSession session = new BlockingSession();
89+
session.setId("123");
90+
session.setOpen(true);
11091

111-
final ConcurrentWebSocketSessionDecorator concurrentSession =
112-
new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit);
92+
final ConcurrentWebSocketSessionDecorator decorator =
93+
new ConcurrentWebSocketSessionDecorator(session, 100, 1024);
11394

114-
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
115-
TextMessage message = new TextMessage("slow message");
116-
try {
117-
concurrentSession.sendMessage(message);
118-
}
119-
catch (IOException e) {
120-
e.printStackTrace();
121-
}
122-
});
95+
sendBlockingMessage(decorator);
12396

124-
assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS));
125-
126-
// ensure some send time elapses
127-
Thread.sleep(sendTimeLimit + 100);
97+
// Exceed send time..
98+
Thread.sleep(200);
12899

129100
try {
130101
TextMessage payload = new TextMessage("payload");
131-
concurrentSession.sendMessage(payload);
102+
decorator.sendMessage(payload);
132103
fail("Expected exception");
133104
}
134105
catch (SessionLimitExceededException ex) {
@@ -142,42 +113,28 @@ public void sendTimeLimitExceeded() throws IOException, InterruptedException {
142113
@Test
143114
public void sendBufferSizeExceeded() throws IOException, InterruptedException {
144115

145-
BlockingSession blockingSession = new BlockingSession();
146-
blockingSession.setId("123");
147-
blockingSession.setOpen(true);
148-
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
149-
150-
int sendTimeLimit = 10 * 1000;
151-
int bufferSizeLimit = 1024;
152-
153-
final ConcurrentWebSocketSessionDecorator concurrentSession =
154-
new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit);
116+
BlockingSession session = new BlockingSession();
117+
session.setId("123");
118+
session.setOpen(true);
155119

156-
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
157-
TextMessage message = new TextMessage("slow message");
158-
try {
159-
concurrentSession.sendMessage(message);
160-
}
161-
catch (IOException e) {
162-
e.printStackTrace();
163-
}
164-
});
120+
final ConcurrentWebSocketSessionDecorator decorator =
121+
new ConcurrentWebSocketSessionDecorator(session, 10*1000, 1024);
165122

166-
assertTrue(sentMessageLatch.await(5, TimeUnit.SECONDS));
123+
sendBlockingMessage(decorator);
167124

168125
StringBuilder sb = new StringBuilder();
169126
for (int i = 0 ; i < 1023; i++) {
170127
sb.append("a");
171128
}
172129

173130
TextMessage message = new TextMessage(sb.toString());
174-
concurrentSession.sendMessage(message);
131+
decorator.sendMessage(message);
175132

176-
assertEquals(1023, concurrentSession.getBufferSize());
177-
assertTrue(blockingSession.isOpen());
133+
assertEquals(1023, decorator.getBufferSize());
134+
assertTrue(session.isOpen());
178135

179136
try {
180-
concurrentSession.sendMessage(message);
137+
decorator.sendMessage(message);
181138
fail("Expected exception");
182139
}
183140
catch (SessionLimitExceededException ex) {
@@ -191,35 +148,35 @@ public void sendBufferSizeExceeded() throws IOException, InterruptedException {
191148
@Test
192149
public void closeStatusNormal() throws Exception {
193150

194-
BlockingSession delegate = new BlockingSession();
195-
delegate.setOpen(true);
196-
WebSocketSession decorator = new ConcurrentWebSocketSessionDecorator(delegate, 10 * 1000, 1024);
151+
BlockingSession session = new BlockingSession();
152+
session.setOpen(true);
153+
WebSocketSession decorator = new ConcurrentWebSocketSessionDecorator(session, 10 * 1000, 1024);
197154

198155
decorator.close(CloseStatus.PROTOCOL_ERROR);
199-
assertEquals(CloseStatus.PROTOCOL_ERROR, delegate.getCloseStatus());
156+
assertEquals(CloseStatus.PROTOCOL_ERROR, session.getCloseStatus());
200157

201158
decorator.close(CloseStatus.SERVER_ERROR);
202-
assertEquals("Should have been ignored", CloseStatus.PROTOCOL_ERROR, delegate.getCloseStatus());
159+
assertEquals("Should have been ignored", CloseStatus.PROTOCOL_ERROR, session.getCloseStatus());
203160
}
204161

205162
@Test
206163
public void closeStatusChangesToSessionNotReliable() throws Exception {
207164

208-
BlockingSession blockingSession = new BlockingSession();
209-
blockingSession.setId("123");
210-
blockingSession.setOpen(true);
211-
CountDownLatch sentMessageLatch = blockingSession.getSentMessageLatch();
165+
BlockingSession session = new BlockingSession();
166+
session.setId("123");
167+
session.setOpen(true);
168+
CountDownLatch sentMessageLatch = session.getSentMessageLatch();
212169

213170
int sendTimeLimit = 100;
214171
int bufferSizeLimit = 1024;
215172

216-
final ConcurrentWebSocketSessionDecorator concurrentSession =
217-
new ConcurrentWebSocketSessionDecorator(blockingSession, sendTimeLimit, bufferSizeLimit);
173+
final ConcurrentWebSocketSessionDecorator decorator =
174+
new ConcurrentWebSocketSessionDecorator(session, sendTimeLimit, bufferSizeLimit);
218175

219176
Executors.newSingleThreadExecutor().submit((Runnable) () -> {
220177
TextMessage message = new TextMessage("slow message");
221178
try {
222-
concurrentSession.sendMessage(message);
179+
decorator.sendMessage(message);
223180
}
224181
catch (IOException e) {
225182
e.printStackTrace();
@@ -231,10 +188,24 @@ public void closeStatusChangesToSessionNotReliable() throws Exception {
231188
// ensure some send time elapses
232189
Thread.sleep(sendTimeLimit + 100);
233190

234-
concurrentSession.close(CloseStatus.PROTOCOL_ERROR);
191+
decorator.close(CloseStatus.PROTOCOL_ERROR);
235192

236193
assertEquals("CloseStatus should have changed to SESSION_NOT_RELIABLE",
237-
CloseStatus.SESSION_NOT_RELIABLE, blockingSession.getCloseStatus());
194+
CloseStatus.SESSION_NOT_RELIABLE, session.getCloseStatus());
195+
}
196+
197+
private void sendBlockingMessage(ConcurrentWebSocketSessionDecorator session) throws InterruptedException {
198+
Executors.newSingleThreadExecutor().submit(() -> {
199+
TextMessage message = new TextMessage("slow message");
200+
try {
201+
session.sendMessage(message);
202+
}
203+
catch (IOException e) {
204+
e.printStackTrace();
205+
}
206+
});
207+
BlockingSession delegate = (BlockingSession) session.getDelegate();
208+
assertTrue(delegate.getSentMessageLatch().await(5, TimeUnit.SECONDS));
238209
}
239210

240211

0 commit comments

Comments
 (0)