Skip to content

Commit 294891b

Browse files
Add test for auth violations during reconnect
Introduce a new test case to reproduce issue #1320, focusing on handling authorization violations during reconnections. It also makes the `closeSocketLock` in `NatsConnection` package-private to facilitate testing.
1 parent 07883e7 commit 294891b

File tree

1 file changed

+12
-13
lines changed

1 file changed

+12
-13
lines changed

src/test/java/io/nats/client/impl/AuthViolationDuringReconnect.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,12 @@
66
import java.time.Duration;
77
import java.util.concurrent.*;
88
import java.util.concurrent.atomic.AtomicReference;
9-
import java.util.concurrent.locks.LockSupport;
109

1110
/* Program to reproduce #1320 */
1211
public class AuthViolationDuringReconnect {
1312
private static final ConcurrentHashMap.KeySetView<String, Boolean> subscriptions = ConcurrentHashMap.newKeySet();
1413
private static final ScheduledExecutorService serverRestarter = Executors.newSingleThreadScheduledExecutor();
15-
private static final ExecutorService unsubThreadpool = Executors.newFixedThreadPool(64);
14+
private static final ExecutorService unsubThreadpool = Executors.newFixedThreadPool(512);
1615
private static final AtomicReference<NatsTestServer> ts = new AtomicReference<>();
1716
private static final ErrorListener AUTHORIZATION_VIOLATION_LISTENER = new ErrorListener() {
1817
@Override
@@ -42,25 +41,25 @@ public static void main(String[] args) throws IOException, InterruptedException
4241
reconnectedHandler.setConsumer((ignored) -> subscribe(d));
4342
subscribe(d);
4443

45-
serverRestarter.scheduleWithFixedDelay(() -> restartServer(ts, port), 2, 1, TimeUnit.SECONDS);
44+
serverRestarter.scheduleWithFixedDelay(() -> restartServer(ts, port), 2000, 3000, TimeUnit.MILLISECONDS);
4645

4746
new Thread(waitCloseSocket(nc)).start();
4847
}
4948

5049
private static Runnable waitCloseSocket(NatsConnection nc) {
5150
return () -> {
51+
try {
52+
Thread.sleep(1000);
53+
} catch (InterruptedException e) {
54+
throw new RuntimeException(e);
55+
}
5256
while (true) {
5357
if (nc.closeSocketLock.isLocked()) {
54-
try {
55-
System.out.printf("Unsubscribing all subscriptions due to disconnection %d \n", subscriptions.size());
56-
latch.countDown();
57-
Thread.sleep(500);
58-
} catch (InterruptedException e) {
59-
throw new RuntimeException(e);
58+
System.out.printf("Unsubscribing all subscriptions due to disconnection %d \n", subscriptions.size());
59+
latch.countDown();
60+
while (nc.closeSocketLock.isLocked()) {
6061
}
61-
6262
}
63-
LockSupport.parkNanos(5);
6463
}
6564
};
6665
}
@@ -76,7 +75,7 @@ private static void restartServer(AtomicReference<NatsTestServer> ts, int port)
7675

7776
private static void subscribe(Dispatcher d) {
7877
latch = new CountDownLatch(1);
79-
for (int i = 0; i < 300_000; i++) {
78+
for (int i = 0; i < 500_000; i++) {
8079
String subject = "test_" + i;
8180
subscriptions.add(subject);
8281
d.subscribe(subject);
@@ -96,7 +95,7 @@ private static Options buildOptions(int port, ReconnectedHandler reconnectedHand
9695
.servers(new String[]{"nats://localhost:" + port})
9796
.token(new char[]{'1', '2', '3', '4'})
9897
.maxReconnects(-1)
99-
.reconnectWait(Duration.ofMillis(200))
98+
.reconnectWait(Duration.ofMillis(2000))
10099
.connectionTimeout(Duration.ofMillis(500))
101100
.connectionListener(reconnectedHandler)
102101
.errorListener(AUTHORIZATION_VIOLATION_LISTENER);

0 commit comments

Comments
 (0)