11
11
public class AuthViolationDuringReconnect {
12
12
private static final ConcurrentHashMap .KeySetView <String , Boolean > subscriptions = ConcurrentHashMap .newKeySet ();
13
13
private static final ScheduledExecutorService serverRestarter = Executors .newSingleThreadScheduledExecutor ();
14
- private static final ExecutorService unsubThreadpool = Executors .newFixedThreadPool (512 );
14
+ private static final ExecutorService unsubThreadpool = Executors .newFixedThreadPool (2 );
15
15
private static final AtomicReference <NatsTestServer > ts = new AtomicReference <>();
16
16
private static final ErrorListener AUTHORIZATION_VIOLATION_LISTENER = new ErrorListener () {
17
17
@ Override
@@ -35,7 +35,8 @@ public static void main(String[] args) throws IOException, InterruptedException
35
35
ts .set (new NatsTestServer (new String []{"--auth" , "1234" , "-m" , "8222" }, port , false ));
36
36
37
37
ReconnectedHandler reconnectedHandler = new ReconnectedHandler ();
38
- NatsConnection nc = (NatsConnection ) Nats .connect (buildOptions (port , reconnectedHandler ));
38
+ NatsConnection nc = new MockPausingNatsConnection (buildOptions (port , reconnectedHandler ));
39
+ nc .connect (true );
39
40
Dispatcher d = nc .createDispatcher ();
40
41
41
42
reconnectedHandler .setConsumer ((ignored ) -> subscribe (d ));
@@ -49,7 +50,7 @@ public static void main(String[] args) throws IOException, InterruptedException
49
50
private static Runnable waitCloseSocket (NatsConnection nc ) {
50
51
return () -> {
51
52
try {
52
- Thread .sleep (1000 );
53
+ Thread .sleep (200 );
53
54
} catch (InterruptedException e ) {
54
55
throw new RuntimeException (e );
55
56
}
@@ -76,7 +77,7 @@ private static void restartServer(AtomicReference<NatsTestServer> ts, int port)
76
77
77
78
private static void subscribe (Dispatcher d ) {
78
79
latch = new CountDownLatch (1 );
79
- for (int i = 0 ; i < 10_000 ; i ++) {
80
+ for (int i = 0 ; i < 1_000 ; i ++) {
80
81
String subject = "test_" + i ;
81
82
subscriptions .add (subject );
82
83
d .subscribe (subject );
@@ -93,7 +94,8 @@ private static void subscribe(Dispatcher d) {
93
94
94
95
private static Options buildOptions (int port , ReconnectedHandler reconnectedHandler ) {
95
96
Options .Builder natsOptions = new Options .Builder ()
96
- .servers (new String []{"nats://localhost:" + port })
97
+ .servers (new String []{"nats://incorrect:1111" , "nats://localhost:" + port })
98
+ .noRandomize ()
97
99
.token (new char []{'1' , '2' , '3' , '4' })
98
100
.maxReconnects (-1 )
99
101
.reconnectWait (Duration .ofMillis (2000 ))
@@ -119,4 +121,30 @@ public void connectionEvent(Connection conn, Events type) {
119
121
}
120
122
}
121
123
}
124
+
125
+ static class MockPausingNatsConnection extends NatsConnection {
126
+ MockPausingNatsConnection (Options options ) {
127
+ super (options );
128
+ }
129
+
130
+ @ Override
131
+ void closeSocketImpl (boolean forceClose ) {
132
+ try {
133
+ Thread .sleep (500 );
134
+ } catch (InterruptedException e ) {
135
+ throw new RuntimeException (e );
136
+ }
137
+ super .closeSocketImpl (forceClose );
138
+ }
139
+
140
+ @ Override
141
+ void sendUnsub (NatsSubscription sub , int after ) {
142
+ try {
143
+ Thread .sleep (1000 );
144
+ } catch (InterruptedException e ) {
145
+ throw new RuntimeException (e );
146
+ }
147
+ super .sendUnsub (sub , after );
148
+ }
149
+ }
122
150
}
0 commit comments