Skip to content

Commit 03a54be

Browse files
committed
Auth Error is allowed to be set before being connected
1 parent 06699ee commit 03a54be

File tree

8 files changed

+149
-124
lines changed

8 files changed

+149
-124
lines changed

build.gradle

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,15 @@ repositories {
3737
maven { url "https://repo1.maven.org/maven2/" }
3838
maven { url "https://central.sonatype.com/repository/maven-snapshots/" }
3939
maven { url "https://oss.sonatype.org/content/repositories/releases/" }
40+
mavenLocal()
4041
}
4142

4243
dependencies {
4344
implementation 'org.bouncycastle:bcprov-lts8on:2.73.8'
4445
implementation 'org.jspecify:jspecify:1.0.0'
4546

4647
testImplementation 'org.junit.jupiter:junit-jupiter:5.9.0'
47-
testImplementation 'io.nats:jnats-server-runner:2.0.0'
48+
testImplementation 'io.nats:jnats-server-runner:3.0.1'
4849
testImplementation 'nl.jqno.equalsverifier:equalsverifier:3.12.3'
4950
}
5051

src/main/java/io/nats/client/impl/NatsConnection.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,8 +1920,8 @@ void processError(String errorText) {
19201920
this.lastError.set(errorText);
19211921
this.connectError.set(errorText); // even if this isn't during connection, save it just in case
19221922

1923-
// If we are connected && we get an authentication error, save it
1924-
if (this.isConnected() && this.isAuthenticationError(errorText) && currentServer != null) {
1923+
// If we get an authentication error, save it
1924+
if (this.isAuthenticationError(errorText) && currentServer != null) {
19251925
this.serverAuthErrors.put(currentServer, errorText);
19261926
}
19271927

src/test/java/io/nats/client/AuthTests.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -375,8 +375,12 @@ public void testBadToken() {
375375
String[] customArgs = { "--auth", "colin" };
376376
try (NatsTestServer ts = new NatsTestServer(customArgs, false)) {
377377
// See config file for user/pass
378-
Options options = new Options.Builder().server(ts.getURI()).maxReconnects(0)
379-
.token("notthetoken".toCharArray()).build();
378+
Options options = new Options.Builder()
379+
.server(ts.getURI())
380+
.maxReconnects(0)
381+
.errorListener(new ErrorListener() {})
382+
.token("notthetoken".toCharArray())
383+
.build();
380384
Nats.connect(options); // expected to fail
381385
}
382386
});
Lines changed: 111 additions & 108 deletions
Original file line numberDiff line numberDiff line change
@@ -1,108 +1,111 @@
1-
// Copyright 2015-2022 The NATS Authors
2-
// Licensed under the Apache License, Version 2.0 (the "License");
3-
// you may not use this file except in compliance with the License.
4-
// You may obtain a copy of the License at:
5-
//
6-
// http://www.apache.org/licenses/LICENSE-2.0
7-
//
8-
// Unless required by applicable law or agreed to in writing, software
9-
// distributed under the License is distributed on an "AS IS" BASIS,
10-
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11-
// See the License for the specific language governing permissions and
12-
// limitations under the License.
13-
14-
package io.nats.client;
15-
16-
import io.nats.ConsoleOutput;
17-
import io.nats.NatsRunnerUtils;
18-
import io.nats.NatsServerRunner;
19-
20-
import java.io.IOException;
21-
import java.util.logging.Level;
22-
23-
public class NatsTestServer extends NatsServerRunner {
24-
static {
25-
NatsServerRunner.setDefaultOutputSupplier(ConsoleOutput::new);
26-
quiet();
27-
}
28-
29-
public static void quiet() {
30-
NatsServerRunner.setDefaultOutputLevel(Level.WARNING);
31-
}
32-
33-
public static void verbose() {
34-
NatsServerRunner.setDefaultOutputLevel(Level.ALL);
35-
}
36-
37-
public NatsTestServer() throws IOException {
38-
super();
39-
}
40-
41-
public NatsTestServer(boolean debug) throws IOException {
42-
super(debug);
43-
}
44-
45-
public NatsTestServer(boolean debug, boolean jetstream) throws IOException {
46-
super(debug, jetstream);
47-
}
48-
49-
public NatsTestServer(int port, boolean debug) throws IOException {
50-
super(port, debug);
51-
}
52-
53-
public NatsTestServer(int port, boolean debug, boolean jetstream) throws IOException {
54-
super(port, debug, jetstream);
55-
}
56-
57-
public NatsTestServer(String configFilePath, boolean debug) throws IOException {
58-
super(configFilePath, debug);
59-
}
60-
61-
public NatsTestServer(String configFilePath, boolean debug, boolean jetstream) throws IOException {
62-
super(configFilePath, debug, jetstream);
63-
}
64-
65-
public NatsTestServer(String configFilePath, String[] configInserts, int port, boolean debug) throws IOException {
66-
super(configFilePath, configInserts, port, debug);
67-
}
68-
69-
public NatsTestServer(String configFilePath, int port, boolean debug) throws IOException {
70-
super(configFilePath, port, debug);
71-
}
72-
73-
public NatsTestServer(String[] customArgs, boolean debug) throws IOException {
74-
super(customArgs, debug);
75-
}
76-
77-
public NatsTestServer(String[] customArgs, int port, boolean debug) throws IOException {
78-
super(customArgs, port, debug);
79-
}
80-
81-
public NatsTestServer(int port, boolean debug, boolean jetstream, String configFilePath, String[] configInserts, String[] customArgs) throws IOException {
82-
super(port, debug, jetstream, configFilePath, configInserts, customArgs);
83-
}
84-
85-
public NatsTestServer(Builder b) throws IOException {
86-
super(b);
87-
}
88-
89-
public static int nextPort() throws IOException {
90-
return NatsRunnerUtils.nextPort();
91-
}
92-
93-
public String getLocalhostUri(String schema) {
94-
return NatsRunnerUtils.getLocalhostUri(schema, getPort());
95-
}
96-
97-
public String getNatsLocalhostUri() {
98-
return NatsRunnerUtils.getNatsLocalhostUri(getPort());
99-
}
100-
101-
public static String getNatsLocalhostUri(int port) {
102-
return NatsRunnerUtils.getNatsLocalhostUri(port);
103-
}
104-
105-
public static String getLocalhostUri(String schema, int port) {
106-
return NatsRunnerUtils.getLocalhostUri(schema, port);
107-
}
108-
}
1+
// Copyright 2015-2022 The NATS Authors
2+
// Licensed under the Apache License, Version 2.0 (the "License");
3+
// you may not use this file except in compliance with the License.
4+
// You may obtain a copy of the License at:
5+
//
6+
// http://www.apache.org/licenses/LICENSE-2.0
7+
//
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
14+
package io.nats.client;
15+
16+
import io.nats.ConsoleOutput;
17+
import io.nats.NatsRunnerUtils;
18+
import io.nats.NatsServerRunner;
19+
20+
import java.io.IOException;
21+
import java.util.logging.Level;
22+
23+
public class NatsTestServer extends NatsServerRunner {
24+
static {
25+
NatsTestServer.quiet();
26+
NatsServerRunner.setDefaultOutputSupplier(ConsoleOutput::new);
27+
NatsServerRunner.setDefaultValidateTries(5);
28+
NatsServerRunner.setDefaultInitialValidateDelay(100);
29+
NatsServerRunner.setDefaultSubsequentValidateDelay(50);
30+
}
31+
32+
public static void quiet() {
33+
NatsServerRunner.setDefaultOutputLevel(Level.WARNING);
34+
}
35+
36+
public static void verbose() {
37+
NatsServerRunner.setDefaultOutputLevel(Level.ALL);
38+
}
39+
40+
public NatsTestServer() throws IOException {
41+
super();
42+
}
43+
44+
public NatsTestServer(boolean debug) throws IOException {
45+
super(debug);
46+
}
47+
48+
public NatsTestServer(boolean debug, boolean jetstream) throws IOException {
49+
super(debug, jetstream);
50+
}
51+
52+
public NatsTestServer(int port, boolean debug) throws IOException {
53+
super(port, debug);
54+
}
55+
56+
public NatsTestServer(int port, boolean debug, boolean jetstream) throws IOException {
57+
super(port, debug, jetstream);
58+
}
59+
60+
public NatsTestServer(String configFilePath, boolean debug) throws IOException {
61+
super(configFilePath, debug);
62+
}
63+
64+
public NatsTestServer(String configFilePath, boolean debug, boolean jetstream) throws IOException {
65+
super(configFilePath, debug, jetstream);
66+
}
67+
68+
public NatsTestServer(String configFilePath, String[] configInserts, int port, boolean debug) throws IOException {
69+
super(configFilePath, configInserts, port, debug);
70+
}
71+
72+
public NatsTestServer(String configFilePath, int port, boolean debug) throws IOException {
73+
super(configFilePath, port, debug);
74+
}
75+
76+
public NatsTestServer(String[] customArgs, boolean debug) throws IOException {
77+
super(customArgs, debug);
78+
}
79+
80+
public NatsTestServer(String[] customArgs, int port, boolean debug) throws IOException {
81+
super(customArgs, port, debug);
82+
}
83+
84+
public NatsTestServer(int port, boolean debug, boolean jetstream, String configFilePath, String[] configInserts, String[] customArgs) throws IOException {
85+
super(port, debug, jetstream, configFilePath, configInserts, customArgs);
86+
}
87+
88+
public NatsTestServer(Builder b) throws IOException {
89+
super(b);
90+
}
91+
92+
public static int nextPort() throws IOException {
93+
return NatsRunnerUtils.nextPort();
94+
}
95+
96+
public String getLocalhostUri(String schema) {
97+
return NatsRunnerUtils.getLocalhostUri(schema, getPort());
98+
}
99+
100+
public String getNatsLocalhostUri() {
101+
return NatsRunnerUtils.getNatsLocalhostUri(getPort());
102+
}
103+
104+
public static String getNatsLocalhostUri(int port) {
105+
return NatsRunnerUtils.getNatsLocalhostUri(port);
106+
}
107+
108+
public static String getLocalhostUri(String schema, int port) {
109+
return NatsRunnerUtils.getLocalhostUri(schema, port);
110+
}
111+
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,9 @@ public static void beforeAll() throws IOException, InterruptedException {
5252

5353
@AfterAll
5454
public static void afterAll() throws Exception {
55-
jsServer.close();
55+
if (jsServer != null) {
56+
jsServer.close();
57+
}
5658
}
5759

5860
public static final Duration DEFAULT_TIMEOUT = Duration.ofMillis(1000);

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

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ public void messageDiscarded(Connection conn, Message msg) {
235235
}
236236
}
237237

238-
public void connectionEvent(Connection conn, Events type) {
238+
@Override
239+
public void connectionEvent(Connection conn, Events type) {} // DEPRECATED
240+
241+
@Override
242+
public void connectionEvent(Connection conn, Events type, Long time, String uriDetails) {
239243
lastEventConnection = conn;
240244
connectionEvents.add(type);
241245
count.incrementAndGet();

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

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,11 @@ public void testSimpleTLSConnection() throws Exception {
9696
@Test
9797
public void testSimpleTlsFirstConnection() throws Exception {
9898
if (TestBase.atLeast2_10_3(ensureRunServerInfo())) {
99-
try (NatsTestServer ts = new NatsTestServer("src/test/resources/tls_first.conf", false)) {
99+
try (NatsTestServer ts = new NatsTestServer(
100+
NatsTestServer.builder()
101+
.configFilePath("src/test/resources/tls_first.conf")
102+
.connectValidateTlsFirstMode())
103+
) {
100104
String servers = ts.getURI();
101105
Options options = new Options.Builder()
102106
.server(servers)
@@ -444,11 +448,15 @@ void handleInfo(String infoJson) {
444448
5. client regular secure | secure proxy | server tls required -> connects
445449
6. client regular secure | secure proxy | server tls available -> connects
446450
*/
447-
448451
@Test
449452
public void testProxyTlsFirst() throws Exception {
450453
if (TestBase.atLeast2_10_3(ensureRunServerInfo())) {
451-
try (NatsTestServer ts = new NatsTestServer("src/test/resources/tls_first.conf", false)) {
454+
// cannot check connect b/c tls first
455+
try (NatsTestServer ts = new NatsTestServer(
456+
NatsTestServer.builder()
457+
.configFilePath("src/test/resources/tls_first.conf")
458+
.connectValidateTlsFirstMode())
459+
) {
452460
// 1. client tls first | secure proxy | server insecure -> connects
453461
ProxyConnection connTI = new ProxyConnection(ts.getURI(), true, null, SERVER_INSECURE);
454462
connTI.connect(false);

src/test/java/io/nats/client/utils/TestBase.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,6 @@ public class TestBase {
8484
HAS_SPACE, HAS_CR, HAS_LF, HAS_TAB, STARTS_SPACE, ENDS_SPACE, null, EMPTY
8585
};
8686

87-
static {
88-
NatsTestServer.quiet();
89-
}
90-
9187
// ----------------------------------------------------------------------------------------------------
9288
// runners
9389
// ----------------------------------------------------------------------------------------------------
@@ -109,6 +105,7 @@ default void append(int index, Options.Builder builder) {}
109105
default boolean includeAllServers() { return false; }
110106
}
111107

108+
@SuppressWarnings("BooleanMethodIsAlwaysInverted")
112109
public interface VersionCheck {
113110
boolean runTest(ServerInfo si);
114111
}
@@ -269,8 +266,14 @@ public static class LongRunningNatsTestServer extends NatsTestServer {
269266
public final Options.Builder builder;
270267
public final ListenerForTesting listenerForTesting;
271268

272-
public LongRunningNatsTestServer(boolean debug, boolean jetstream, Options.Builder builder) throws IOException, InterruptedException {
273-
super(debug, jetstream);
269+
public LongRunningNatsTestServer(boolean debug, boolean jetstream, Options.Builder builder) throws IOException {
270+
super(builder()
271+
.debug(debug)
272+
.jetstream(jetstream)
273+
.connectValidateInitialDelay(100L)
274+
.connectValidateSubsequentDelay(25L)
275+
.connectValidateTries(15)
276+
);
274277
this.jetstream = jetstream;
275278
if (builder == null) {
276279
this.builder = new Options.Builder();

0 commit comments

Comments
 (0)