-
Notifications
You must be signed in to change notification settings - Fork 62
Expand file tree
/
Copy pathEventLoopRestartTest.java
More file actions
127 lines (108 loc) · 4.38 KB
/
EventLoopRestartTest.java
File metadata and controls
127 lines (108 loc) · 4.38 KB
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
package org.microhttp;
import java.io.Closeable;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
public class EventLoopRestartTest {
@Test
public void stoppingShouldReleasePort() throws IOException, InterruptedException {
Options options = OptionsBuilder.newBuilder()
.withPort(0)
.build();
TestLogger logger = new TestLogger();
Handler handler = (request, callback) -> callback.accept(new Response(200, "OK", List.of(), "".getBytes(StandardCharsets.UTF_8)));
EventLoop eventLoop = null;
EventLoop secondEventLoop = null;
try {
eventLoop = new EventLoop(options, (Logger) logger, (org.microhttp.Handler) handler);
eventLoop.start();
int port = eventLoop.getPort();
eventLoop.stop();
eventLoop.join();
options = OptionsBuilder.newBuilder()
.withPort(port)
.build();
secondEventLoop = new EventLoop(options, (Logger) logger, (org.microhttp.Handler) handler);
secondEventLoop.start();
} finally {
if (eventLoop != null) {
eventLoop.stop();
eventLoop.join();
}
if (secondEventLoop != null) {
secondEventLoop.stop();
secondEventLoop.join();
}
}
}
@Test
public void stoppingShouldReleasePortAfterHandledResponse() throws IOException, InterruptedException {
Options options = OptionsBuilder.newBuilder()
.withPort(0)
.build();
TestLogger logger = new TestLogger();
String responseBody = "test";
Handler handler = (request, callback) -> callback.accept(new Response(
200,
"OK",
List.of(new Header("Content-Type", "text/plain")),
responseBody.getBytes(StandardCharsets.UTF_8)
));
EventLoop eventLoop = null;
EventLoop secondEventLoop = null;
Collection<Closeable> resourcesToClose = new ArrayList<>();
try {
eventLoop = new EventLoop(options, (Logger) logger, (org.microhttp.Handler) handler);
eventLoop.start();
int port = eventLoop.getPort();
Socket socket = new Socket("localhost", port);
socket.setSoTimeout(5_000);
resourcesToClose.add(socket);
InputStream inputStream = socket.getInputStream();
OutputStream outputStream = socket.getOutputStream();
outputStream.write(EventLoopTest.HTTP10_REQUEST.getBytes());
byte[] received = inputStream.readAllBytes();
Assertions.assertEquals(
EventLoopTest.HTTP10_RESPONSE.formatted(responseBody.length(), responseBody),
new String(received, StandardCharsets.UTF_8)
);
eventLoop.stop();
eventLoop.join();
socket.close();
options = OptionsBuilder.newBuilder()
.withPort(port)
.build();
secondEventLoop = new EventLoop(options, (Logger) logger, (org.microhttp.Handler) handler);
secondEventLoop.start();
socket = new Socket("localhost", port);
resourcesToClose.add(socket);
inputStream = socket.getInputStream();
outputStream = socket.getOutputStream();
outputStream.write(EventLoopTest.HTTP10_REQUEST.getBytes());
received = inputStream.readAllBytes();
Assertions.assertEquals(
EventLoopTest.HTTP10_RESPONSE.formatted(responseBody.length(), responseBody),
new String(received, StandardCharsets.UTF_8)
);
} finally {
if (eventLoop != null) {
eventLoop.stop();
eventLoop.join();
}
if (secondEventLoop != null) {
secondEventLoop.stop();
secondEventLoop.join();
}
for (Closeable closeable : resourcesToClose) {
closeable.close();
}
}
}
}