Skip to content

Commit bc1946d

Browse files
committed
fix(test): disable recovery on demand for a specific test
1 parent 102ab09 commit bc1946d

File tree

3 files changed

+28
-11
lines changed

3 files changed

+28
-11
lines changed

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -942,7 +942,6 @@ public void run() {
942942
if (args[0] instanceof JSONObject) {
943943
JSONObject data = (JSONObject) args[0];
944944
String text = data.optString("text");
945-
System.out.println("Received message: " + text);
946945
events.offer(text);
947946
}
948947
});

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

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,17 @@ public void run() {
114114
@Test(timeout = TIMEOUT)
115115
public void shouldChangeSocketIdUponReconnection() throws InterruptedException {
116116
final BlockingQueue<Optional> values = new LinkedBlockingQueue<>();
117-
socket = client();
117+
118+
IO.Options opts = createOptions();
119+
opts.forceNew = true;
120+
try {
121+
JSONObject auth = new JSONObject();
122+
auth.put("noRecovery", true);
123+
opts.auth = auth;
124+
} catch (JSONException ignored) {
125+
}
126+
127+
socket = client(opts);
118128
socket.once(Socket.EVENT_CONNECT, new Emitter.Listener() {
119129
@Override
120130
public void call(Object... objects) {
@@ -160,7 +170,7 @@ public void shouldAcceptAQueryStringOnDefaultNamespace() throws InterruptedExcep
160170
socket.emit("getHandshake", new Ack() {
161171
@Override
162172
public void call(Object... args) {
163-
JSONObject handshake = (JSONObject)args[0];
173+
JSONObject handshake = (JSONObject) args[0];
164174
values.offer(Optional.ofNullable(handshake));
165175
}
166176
});
@@ -181,7 +191,7 @@ public void shouldAcceptAQueryString() throws InterruptedException, JSONExceptio
181191
socket.on("handshake", new Emitter.Listener() {
182192
@Override
183193
public void call(Object... args) {
184-
JSONObject handshake = (JSONObject)args[0];
194+
JSONObject handshake = (JSONObject) args[0];
185195
values.offer(Optional.ofNullable(handshake));
186196
}
187197
});
@@ -208,7 +218,7 @@ public void shouldAcceptAnAuthOption() throws InterruptedException, JSONExceptio
208218
socket.on("handshake", new Emitter.Listener() {
209219
@Override
210220
public void call(Object... args) {
211-
JSONObject handshake = (JSONObject)args[0];
221+
JSONObject handshake = (JSONObject) args[0];
212222
values.offer(Optional.ofNullable(handshake));
213223
}
214224
});
@@ -375,7 +385,7 @@ public void shouldNotTimeoutWhenTheServerDoesAcknowledgeTheEvent() throws Interr
375385
socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
376386
@Override
377387
public void call(Object... args) {
378-
socket.emit("ack", 1, "2", new byte[] { 3 }, new AckWithTimeout(200) {
388+
socket.emit("ack", 1, "2", new byte[]{3}, new AckWithTimeout(200) {
379389
@Override
380390
public void onTimeout() {
381391
fail();
@@ -395,7 +405,7 @@ public void onSuccess(Object... args) {
395405

396406
assertThat((Integer) values.take(), is(1));
397407
assertThat((String) values.take(), is("2"));
398-
assertThat((byte[]) values.take(), is(new byte[] { 3 }));
408+
assertThat((byte[]) values.take(), is(new byte[]{3}));
399409
}
400410

401411
@Test(timeout = TIMEOUT)
@@ -405,7 +415,7 @@ public void shouldCallCatchAllListenerForIncomingPackets() throws InterruptedExc
405415
socket = client();
406416

407417
socket.on("message", args -> {
408-
socket.emit("echo", 1, "2", new byte[] { 3 });
418+
socket.emit("echo", 1, "2", new byte[]{3});
409419

410420
socket.onAnyIncoming(args1 -> {
411421
for (Object arg : args1) {
@@ -419,7 +429,7 @@ public void shouldCallCatchAllListenerForIncomingPackets() throws InterruptedExc
419429
assertThat((String) values.take(), is("echoBack"));
420430
assertThat((Integer) values.take(), is(1));
421431
assertThat((String) values.take(), is("2"));
422-
assertThat((byte[]) values.take(), is(new byte[] { 3 }));
432+
assertThat((byte[]) values.take(), is(new byte[]{3}));
423433
}
424434

425435
@Test(timeout = TIMEOUT)
@@ -428,7 +438,7 @@ public void shouldCallCatchAllListenerForOutgoingPackets() throws InterruptedExc
428438

429439
socket = client();
430440

431-
socket.emit("echo", 1, "2", new byte[] { 3 });
441+
socket.emit("echo", 1, "2", new byte[]{3});
432442

433443
socket.onAnyOutgoing(args -> {
434444
for (Object arg : args) {
@@ -441,6 +451,6 @@ public void shouldCallCatchAllListenerForOutgoingPackets() throws InterruptedExc
441451
assertThat((String) values.take(), is("echo"));
442452
assertThat((Integer) values.take(), is(1));
443453
assertThat((String) values.take(), is("2"));
444-
assertThat((byte[]) values.take(), is(new byte[] { 3 }));
454+
assertThat((byte[]) values.take(), is(new byte[]{3}));
445455
}
446456
}

src/test/resources/server.js

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,14 @@ 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+
2533
const fooNsp = io.of('/foo');
2634

2735
fooNsp.on('connection', (socket) => {

0 commit comments

Comments
 (0)