Skip to content

Commit 639abf3

Browse files
committed
fix: tests
1 parent bc1946d commit 639abf3

File tree

5 files changed

+106
-54
lines changed

5 files changed

+106
-54
lines changed

src/test/java/io/socket/client/Connection.java

Lines changed: 73 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -18,61 +18,88 @@ public abstract class Connection {
1818

1919
final static int TIMEOUT = 7_000;
2020
final static int PORT = 3000;
21+
final static int NO_RECOVERY_PORT = 3001;
2122

2223
private Process serverProcess;
24+
private Process noRecoveryServerProcess;
2325
private ExecutorService serverService;
24-
private Future serverOutput;
25-
private Future serverError;
26+
private Future<?> serverOutput;
27+
private Future<?> serverError;
28+
private Future<?> noRecoveryServerOutput;
29+
private Future<?> noRecoveryServerError;
2630

2731
@Before
2832
public void startServer() throws IOException, InterruptedException {
29-
logger.fine("Starting server ...");
33+
logger.fine("Starting servers...");
3034

35+
// Start main server
3136
final CountDownLatch latch = new CountDownLatch(1);
32-
serverProcess = Runtime.getRuntime().exec(
33-
String.format("node src/test/resources/server.js %s", nsp()), createEnv());
37+
serverProcess = startServerProcess("node src/test/resources/server.js %s", PORT);
3438
serverService = Executors.newCachedThreadPool();
35-
serverOutput = serverService.submit(new Runnable() {
36-
@Override
37-
public void run() {
38-
BufferedReader reader = new BufferedReader(
39-
new InputStreamReader(serverProcess.getInputStream()));
40-
String line;
41-
try {
42-
line = reader.readLine();
43-
latch.countDown();
44-
do {
45-
logger.fine("SERVER OUT: " + line);
46-
} while ((line = reader.readLine()) != null);
47-
} catch (IOException e) {
48-
logger.warning(e.getMessage());
49-
}
39+
serverOutput = startServerOutput(serverProcess, "MAIN", latch);
40+
serverError = startServerError(serverProcess, "MAIN");
41+
42+
// Start no-recovery server
43+
final CountDownLatch noRecoveryLatch = new CountDownLatch(1);
44+
noRecoveryServerProcess = startServerProcess("node src/test/resources/server_no_recovery.js %s", NO_RECOVERY_PORT);
45+
noRecoveryServerOutput = startServerOutput(noRecoveryServerProcess, "NO_RECOVERY", noRecoveryLatch);
46+
noRecoveryServerError = startServerError(noRecoveryServerProcess, "NO_RECOVERY");
47+
48+
// Wait for both servers to start
49+
latch.await(3000, TimeUnit.MILLISECONDS);
50+
noRecoveryLatch.await(3000, TimeUnit.MILLISECONDS);
51+
}
52+
53+
private Process startServerProcess(String script, int port) throws IOException {
54+
return Runtime.getRuntime().exec(String.format(script, nsp()), createEnv(port));
55+
}
56+
57+
private Future<?> startServerOutput(Process process, String serverName, CountDownLatch latch) {
58+
return serverService.submit(() -> {
59+
BufferedReader reader = new BufferedReader(
60+
new InputStreamReader(process.getInputStream()));
61+
String line;
62+
try {
63+
line = reader.readLine();
64+
latch.countDown();
65+
do {
66+
logger.fine(serverName + " SERVER OUT: " + line);
67+
} while ((line = reader.readLine()) != null);
68+
} catch (IOException e) {
69+
logger.warning(e.getMessage());
5070
}
5171
});
52-
serverError = serverService.submit(new Runnable() {
53-
@Override
54-
public void run() {
55-
BufferedReader reader = new BufferedReader(
56-
new InputStreamReader(serverProcess.getErrorStream()));
57-
String line;
58-
try {
59-
while ((line = reader.readLine()) != null) {
60-
logger.fine("SERVER ERR: " + line);
61-
}
62-
} catch (IOException e) {
63-
logger.warning(e.getMessage());
72+
}
73+
74+
private Future<?> startServerError(Process process, String serverName) {
75+
return serverService.submit(() -> {
76+
BufferedReader reader = new BufferedReader(
77+
new InputStreamReader(process.getErrorStream()));
78+
String line;
79+
try {
80+
while ((line = reader.readLine()) != null) {
81+
logger.fine(serverName + " SERVER ERR: " + line);
6482
}
83+
} catch (IOException e) {
84+
logger.warning(e.getMessage());
6585
}
6686
});
67-
latch.await(3000, TimeUnit.MILLISECONDS);
6887
}
6988

7089
@After
7190
public void stopServer() throws InterruptedException {
72-
logger.fine("Stopping server ...");
91+
logger.fine("Stopping servers...");
92+
93+
// Stop main server
7394
serverProcess.destroy();
7495
serverOutput.cancel(false);
7596
serverError.cancel(false);
97+
98+
// Stop no-recovery server
99+
noRecoveryServerProcess.destroy();
100+
noRecoveryServerOutput.cancel(false);
101+
noRecoveryServerError.cancel(false);
102+
76103
serverService.shutdown();
77104
serverService.awaitTermination(3000, TimeUnit.MILLISECONDS);
78105
}
@@ -90,11 +117,16 @@ Socket client(IO.Options opts) {
90117
}
91118

92119
Socket client(String path, IO.Options opts) {
93-
return IO.socket(URI.create(uri() + path), opts);
120+
int port = opts.port != -1 ? opts.port : PORT;
121+
return IO.socket(URI.create(uri(port) + path), opts);
94122
}
95123

96124
URI uri() {
97-
return URI.create("http://localhost:" + PORT);
125+
return uri(PORT);
126+
}
127+
128+
URI uri(int port) {
129+
return URI.create("http://localhost:" + port);
98130
}
99131

100132
String nsp() {
@@ -108,16 +140,19 @@ IO.Options createOptions() {
108140
}
109141

110142
String[] createEnv() {
143+
return createEnv(PORT);
144+
}
145+
146+
String[] createEnv(int port) {
111147
Map<String, String> env = new HashMap<>(System.getenv());
112148
env.put("DEBUG", "socket.io:*");
113-
env.put("PORT", String.valueOf(PORT));
149+
env.put("PORT", String.valueOf(port));
114150
String[] _env = new String[env.size()];
115151
int i = 0;
116152
for (String key : env.keySet()) {
117153
_env[i] = key + "=" + env.get(key);
118154
i++;
119155
}
120156
return _env;
121-
122157
}
123158
}

src/test/java/io/socket/client/SocketTest.java

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -117,12 +117,7 @@ public void shouldChangeSocketIdUponReconnection() throws InterruptedException {
117117

118118
IO.Options opts = createOptions();
119119
opts.forceNew = true;
120-
try {
121-
JSONObject auth = new JSONObject();
122-
auth.put("noRecovery", true);
123-
opts.auth = auth;
124-
} catch (JSONException ignored) {
125-
}
120+
opts.port = Connection.NO_RECOVERY_PORT;
126121

127122
socket = client(opts);
128123
socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() {

src/test/java/io/socket/client/executions/ConnectionFailure.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@
1010
public class ConnectionFailure {
1111

1212
public static void main(String[] args) throws URISyntaxException {
13-
int port = Integer.parseInt(System.getenv("PORT"));
14-
port++;
13+
int port = 60_000;
1514
IO.Options options = new IO.Options();
1615
options.forceNew = true;
1716
options.reconnection = false;

src/test/resources/server.js

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,14 +22,6 @@ var port = process.env.PORT || 3000;
2222
var nsp = process.argv[2] || '/';
2323
var slice = Array.prototype.slice;
2424

25-
// Disable recovery on demand
26-
io.use((socket, next) => {
27-
if (socket.handshake.auth?.noRecovery === true) {
28-
socket.handshake.auth._pid = 'invalid-' + Date.now();
29-
}
30-
next();
31-
});
32-
3325
const fooNsp = io.of('/foo');
3426

3527
fooNsp.on('connection', (socket) => {
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
var fs = require('fs');
2+
3+
var server;
4+
if (process.env.SSL) {
5+
server = require('https').createServer({
6+
key: fs.readFileSync(__dirname + '/key.pem'),
7+
cert: fs.readFileSync(__dirname + '/cert.pem')
8+
});
9+
} else {
10+
server = require('http').createServer();
11+
}
12+
13+
// Create server without connection state recovery
14+
var io = require('socket.io')(server, {
15+
pingInterval: 2000
16+
});
17+
18+
var port = process.env.PORT || 3001; // Different port to avoid conflicts
19+
var nsp = process.argv[2] || '/';
20+
21+
server.listen(port, () => {
22+
console.log(`Test server without recovery running on port ${port}`);
23+
});
24+
25+
io.of(nsp).on('connection', (socket) => {
26+
console.log(`New connection: ${socket.id}`);
27+
28+
socket.on('disconnect', () => {
29+
console.log(`Client disconnected: ${socket.id}`);
30+
});
31+
});

0 commit comments

Comments
 (0)