Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Testing Cleanup Only - No Functionality Changes #1110

Merged
merged 1 commit into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
import java.io.IOException;
import java.security.GeneralSecurityException;

public class TestAuthHandler implements AuthHandler {
private NKey nkey;
public class AuthHandlerForTesting implements AuthHandler {
private final NKey nkey;

public TestAuthHandler(NKey nkey) {
public AuthHandlerForTesting(NKey nkey) {
this.nkey = nkey;
}

public TestAuthHandler() throws Exception {
public AuthHandlerForTesting() throws Exception {
this.nkey = NKey.createUser(null);
}

Expand Down
104 changes: 51 additions & 53 deletions src/test/java/io/nats/client/AuthTests.java

Large diffs are not rendered by default.

58 changes: 29 additions & 29 deletions src/test/java/io/nats/client/ConnectTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@

import io.nats.client.ConnectionListener.Events;
import io.nats.client.NatsServerProtocolMock.ExitAt;
import io.nats.client.impl.ListenerForTesting;
import io.nats.client.impl.SimulateSocketDataPortException;
import io.nats.client.impl.TestHandler;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -35,7 +35,7 @@
public class ConnectTests {
@Test
public void testDefaultConnection() throws Exception {
try (NatsTestServer ts = new NatsTestServer(Options.DEFAULT_PORT, false)) {
try (NatsTestServer ignored = new NatsTestServer(Options.DEFAULT_PORT, false)) {
Connection nc = standardConnection();
assertEquals(Options.DEFAULT_PORT, nc.getServerInfo().getPort());
standardCloseConnection(nc);
Expand Down Expand Up @@ -235,7 +235,6 @@ public void testFailWrongInitialInfoOP() {
@Test
public void testIncompleteInitialInfo() {
assertThrows(IOException.class, () -> {
Connection nc = null;
String badInfo = "{\"server_id\"\r\n";
try (NatsServerProtocolMock ts = new NatsServerProtocolMock(null, badInfo)) {
Options options = new Options.Builder().server(ts.getURI()).reconnectWait(Duration.ofDays(1)).build();
Expand All @@ -246,18 +245,18 @@ public void testIncompleteInitialInfo() {

@Test
public void testAsyncConnection() throws Exception {
TestHandler handler = new TestHandler();
ListenerForTesting listener = new ListenerForTesting();
Connection nc = null;

try (NatsTestServer ts = new NatsTestServer(false)) {
Options options = new Options.Builder().server(ts.getURI()).connectionListener(handler).build();
handler.prepForStatusChange(Events.CONNECTED);
Options options = new Options.Builder().server(ts.getURI()).connectionListener(listener).build();
listener.prepForStatusChange(Events.CONNECTED);

Nats.connectAsynchronously(options, false);

handler.waitForStatusChange(1, TimeUnit.SECONDS);
listener.waitForStatusChange(1, TimeUnit.SECONDS);

nc = handler.getLastEventConnection();
nc = listener.getLastEventConnection();
assertNotNull(nc);
assertConnected(nc);
standardCloseConnection(nc);
Expand All @@ -266,21 +265,21 @@ public void testAsyncConnection() throws Exception {

@Test
public void testAsyncConnectionWithReconnect() throws Exception {
TestHandler handler = new TestHandler();
ListenerForTesting listener = new ListenerForTesting();
int port = NatsTestServer.nextPort();
Options options = new Options.Builder().server("nats://localhost:" + port).maxReconnects(-1)
.reconnectWait(Duration.ofMillis(100)).connectionListener(handler).build();
.reconnectWait(Duration.ofMillis(100)).connectionListener(listener).build();

Nats.connectAsynchronously(options, true);

sleep(5000); // No server at this point, let it fail and try to start over

Connection nc = handler.getLastEventConnection(); // will be disconnected, but should be there
Connection nc = listener.getLastEventConnection(); // will be disconnected, but should be there
assertNotNull(nc);

handler.prepForStatusChange(Events.RECONNECTED);
try (NatsTestServer ts = new NatsTestServer(port, false)) {
standardConnectionWait(nc, handler);
listener.prepForStatusChange(Events.RECONNECTED);
try (NatsTestServer ignored = new NatsTestServer(port, false)) {
standardConnectionWait(nc, listener);
standardCloseConnection(nc);
}
}
Expand All @@ -297,15 +296,15 @@ public void testThrowOnAsyncWithoutListener() {

@Test
public void testErrorOnAsync() throws Exception {
TestHandler handler = new TestHandler();
ListenerForTesting listener = new ListenerForTesting();
Options options = new Options.Builder().server("nats://localhost:" + NatsTestServer.nextPort())
.connectionListener(handler).errorListener(handler).noReconnect().build();
handler.prepForStatusChange(Events.CLOSED);
.connectionListener(listener).errorListener(listener).noReconnect().build();
listener.prepForStatusChange(Events.CLOSED);
Nats.connectAsynchronously(options, false);
handler.waitForStatusChange(10, TimeUnit.SECONDS);
listener.waitForStatusChange(10, TimeUnit.SECONDS);

assertTrue(handler.getExceptionCount() > 0);
assertTrue(handler.getEventCount(Events.CLOSED) > 0);
assertTrue(listener.getExceptionCount() > 0);
assertTrue(listener.getEventCount(Events.CLOSED) > 0);
}

@Test
Expand Down Expand Up @@ -439,6 +438,7 @@ public void testFlushBufferThreadSafety() throws Exception {
CountDownLatch completedLatch = new CountDownLatch(1);

Thread t = new Thread("publisher") {
@SuppressWarnings("ResultOfMethodCallIgnored")
public void run() {
byte[] payload = new byte[5];
pubLatch.countDown();
Expand Down Expand Up @@ -489,13 +489,13 @@ public void run() {
}
}

@SuppressWarnings({"unused", "UnusedAssignment", "resource"})
@SuppressWarnings({"unused", "UnusedAssignment"})
@Test
public void testSocketLevelException() throws Exception {
int port = NatsTestServer.nextPort();

AtomicBoolean simExReceived = new AtomicBoolean();
TestHandler th = new TestHandler();
ListenerForTesting listener = new ListenerForTesting();
ErrorListener el = new ErrorListener() {
@Override
public void exceptionOccurred(Connection conn, Exception exp) {
Expand All @@ -508,7 +508,7 @@ public void exceptionOccurred(Connection conn, Exception exp) {
Options options = new Options.Builder()
.server(NatsTestServer.getNatsLocalhostUri(port))
.dataPortType("io.nats.client.impl.SimulateSocketDataPortException")
.connectionListener(th)
.connectionListener(listener)
.errorListener(el)
.reconnectDelayHandler(l -> Duration.ofSeconds(1))
.build();
Expand All @@ -534,25 +534,25 @@ public void exceptionOccurred(Connection conn, Exception exp) {
try (NatsTestServer ts = new NatsTestServer(port, false)) {
try {
SimulateSocketDataPortException.THROW_ON_CONNECT.set(true);
th.prepForStatusChange(Events.RECONNECTED);
listener.prepForStatusChange(Events.RECONNECTED);
connection = Nats.connectReconnectOnConnect(options);
assertTrue(th.waitForStatusChange(5, TimeUnit.SECONDS));
th.prepForStatusChange(Events.DISCONNECTED);
assertTrue(listener.waitForStatusChange(5, TimeUnit.SECONDS));
listener.prepForStatusChange(Events.DISCONNECTED);
}
catch (Exception e) {
fail("should have connected " + e);
}
}
assertTrue(th.waitForStatusChange(5, TimeUnit.SECONDS));
assertTrue(listener.waitForStatusChange(5, TimeUnit.SECONDS));
assertTrue(simExReceived.get());
simExReceived.set(false);

// 2. NORMAL RECONNECT
th.prepForStatusChange(Events.RECONNECTED);
listener.prepForStatusChange(Events.RECONNECTED);
try (NatsTestServer ts = new NatsTestServer(port, false)) {
SimulateSocketDataPortException.THROW_ON_CONNECT.set(true);
try {
assertTrue(th.waitForStatusChange(5, TimeUnit.SECONDS));
assertTrue(listener.waitForStatusChange(5, TimeUnit.SECONDS));
}
catch (Exception e) {
fail("should have reconnected " + e);
Expand Down
48 changes: 24 additions & 24 deletions src/test/java/io/nats/client/OptionsTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,8 @@ private static void _testDefaultOptions(Options o) {
assertEquals(Options.DEFAULT_REQUEST_CLEANUP_INTERVAL, o.getRequestCleanupInterval(),
"default cleanup interval");

assertTrue(o.getErrorListener() instanceof ErrorListenerLoggerImpl, "error handler");
assertNull(o.getConnectionListener(), "disconnect handler");
assertInstanceOf(ErrorListenerLoggerImpl.class, o.getErrorListener(), "error listener");
assertNull(o.getConnectionListener(), "disconnect listener");
assertNull(o.getStatisticsCollector(), "statistics collector");
assertFalse(o.isOldRequestStyle(), "default oldstyle");
}
Expand Down Expand Up @@ -152,7 +152,7 @@ private static void _testChainedStringOptions(Options o) {

@Test
public void testChainedSecure() throws Exception {
SSLContext ctx = TestSSLUtils.createTestSSLContext();
SSLContext ctx = SslTestingHelper.createTestSSLContext();
SSLContext.setDefault(ctx);
Options o = new Options.Builder().secure().build();
_testChainedSecure(ctx, o);
Expand All @@ -165,7 +165,7 @@ private static void _testChainedSecure(SSLContext ctx, Options o) {

@Test
public void testChainedSSLOptions() throws Exception {
SSLContext ctx = TestSSLUtils.createTestSSLContext();
SSLContext ctx = SslTestingHelper.createTestSSLContext();
Options o = new Options.Builder().sslContext(ctx).build();
_testChainedSSLOptions(ctx, o);
_testChainedSSLOptions(ctx, new Options.Builder(o).build());
Expand Down Expand Up @@ -239,15 +239,15 @@ public void testHttpRequestInterceptors() {

@Test
public void testChainedErrorHandler() {
TestHandler handler = new TestHandler();
Options o = new Options.Builder().errorListener(handler).build();
_testChainedErrorHandler(handler, o);
_testChainedErrorHandler(handler, new Options.Builder(o).build());
ListenerForTesting listener = new ListenerForTesting();
Options o = new Options.Builder().errorListener(listener).build();
_testChainedErrorListener(listener, o);
_testChainedErrorListener(listener, new Options.Builder(o).build());
}

private static void _testChainedErrorHandler(TestHandler handler, Options o) {
private static void _testChainedErrorListener(ListenerForTesting listener, Options o) {
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
assertEquals(handler, o.getErrorListener(), "chained error handler");
assertEquals(listener, o.getErrorListener(), "chained error listener");
}

@Test
Expand All @@ -260,8 +260,8 @@ public void testChainedConnectionListener() {

private static void _testChainedConnectionListener(ConnectionListener cHandler, Options o) {
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
assertTrue(o.getErrorListener() instanceof ErrorListenerLoggerImpl, "error handler");
assertSame(cHandler, o.getConnectionListener(), "chained connection handler");
assertInstanceOf(ErrorListenerLoggerImpl.class, o.getErrorListener(), "error listener");
assertSame(cHandler, o.getConnectionListener(), "chained connection listener");
}

@Test
Expand All @@ -274,7 +274,7 @@ public void testChainedStatisticsCollector() {

private static void _testChainedStatisticsCollector(StatisticsCollector cHandler, Options o) {
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
assertTrue(o.getStatisticsCollector() instanceof TestStatisticsCollector, "statistics collector");
assertInstanceOf(TestStatisticsCollector.class, o.getStatisticsCollector(), "statistics collector");
assertSame(cHandler, o.getStatisticsCollector(), "chained statistics collector");
}

Expand Down Expand Up @@ -334,7 +334,7 @@ private static void _testPropertiesStringOptions(Options o) {
@Test
public void testPropertiesSSLOptions() throws Exception {
// don't use default for tests, issues with forcing algorithm exception in other tests break it
SSLContext.setDefault(TestSSLUtils.createTestSSLContext());
SSLContext.setDefault(SslTestingHelper.createTestSSLContext());
Properties props = new Properties();
props.setProperty(Options.PROP_SECURE, "true");

Expand Down Expand Up @@ -476,7 +476,7 @@ private static void _testProperties(Options o) {
assertEquals(1000, o.getPingInterval().toMillis());
assertNotNull(o.getAuthHandler());
assertNotNull(o.getServerPool());
assertTrue(o.getServerPool() instanceof CoverageServerPool);
assertInstanceOf(CoverageServerPool.class, o.getServerPool());
}

@Test
Expand Down Expand Up @@ -583,32 +583,32 @@ private static void _testPropertyDurationOptions(Options o) {
}

@Test
public void testPropertyErrorHandler() {
public void testPropertyErrorListener() {
Properties props = new Properties();
props.setProperty(Options.PROP_ERROR_LISTENER, TestHandler.class.getCanonicalName());
props.setProperty(Options.PROP_ERROR_LISTENER, ListenerForTesting.class.getCanonicalName());

Options o = new Options.Builder(props).build();
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
assertNotNull(o.getErrorListener(), "property error handler");
assertNotNull(o.getErrorListener(), "property error listener");

o.getErrorListener().errorOccurred(null, "bad subject");
assertEquals(((TestHandler) o.getErrorListener()).getCount(), 1, "property error handler class");
assertEquals(((ListenerForTesting) o.getErrorListener()).getCount(), 1, "property error listener class");
}

@Test
public void testPropertyConnectionListeners() {
Properties props = new Properties();
props.setProperty(Options.PROP_CONNECTION_CB, TestHandler.class.getCanonicalName());
props.setProperty(Options.PROP_CONNECTION_CB, ListenerForTesting.class.getCanonicalName());

Options o = new Options.Builder(props).build();
assertFalse(o.isVerbose(), "default verbose"); // One from a different type
assertNotNull(o.getConnectionListener(), "property connection handler");
assertNotNull(o.getConnectionListener(), "property connection listener");

o.getConnectionListener().connectionEvent(null, Events.DISCONNECTED);
o.getConnectionListener().connectionEvent(null, Events.RECONNECTED);
o.getConnectionListener().connectionEvent(null, Events.CLOSED);

assertEquals(((TestHandler) o.getConnectionListener()).getCount(), 3, "property connect handler class");
assertEquals(((ListenerForTesting) o.getConnectionListener()).getCount(), 3, "property connect listener class");
}

@Test
Expand Down Expand Up @@ -654,7 +654,7 @@ public void testNonDefaultConnectOptions() {

@Test
public void testConnectOptionsWithNameAndContext() throws Exception {
SSLContext ctx = TestSSLUtils.createTestSSLContext();
SSLContext ctx = SslTestingHelper.createTestSSLContext();
Options o = new Options.Builder().sslContext(ctx).connectionName("c1").build();
String expected = "{\"lang\":\"java\",\"version\":\"" + Nats.CLIENT_VERSION + "\",\"name\":\"c1\""
+ ",\"protocol\":1,\"verbose\":false,\"pedantic\":false,\"tls_required\":true,\"echo\":true,\"headers\":true,\"no_responders\":true}";
Expand All @@ -679,7 +679,7 @@ public void testAuthConnectOptions() {

@Test
public void testNKeyConnectOptions() throws Exception {
TestAuthHandler th = new TestAuthHandler();
AuthHandlerForTesting th = new AuthHandlerForTesting();
byte[] nonce = "abcdefg".getBytes(StandardCharsets.UTF_8);
String sig = Base64.getUrlEncoder().withoutPadding().encodeToString(th.sign(nonce));

Expand Down
6 changes: 3 additions & 3 deletions src/test/java/io/nats/client/RequestTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

import io.nats.client.api.StorageType;
import io.nats.client.api.StreamConfiguration;
import io.nats.client.impl.TestHandler;
import io.nats.client.impl.ListenerForTesting;
import org.junit.jupiter.api.Test;

import java.io.IOException;
Expand All @@ -32,8 +32,8 @@ public class RequestTests {
@Test
public void testRequestNoResponder() throws Exception {
try (NatsTestServer ts = new NatsTestServer(false, true)) {
Options optCancel = Options.builder().server(ts.getURI()).errorListener(new TestHandler()).build();
Options optReport = Options.builder().server(ts.getURI()).reportNoResponders().errorListener(new TestHandler()).build();
Options optCancel = Options.builder().server(ts.getURI()).errorListener(new ListenerForTesting()).build();
Options optReport = Options.builder().server(ts.getURI()).reportNoResponders().errorListener(new ListenerForTesting()).build();
try (Connection ncCancel = standardConnection(optCancel);
Connection ncReport = standardConnection(optReport);
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import java.security.SecureRandom;
import java.util.Properties;

public class TestSSLUtils {
public class SslTestingHelper {
public static String KEYSTORE_PATH = "src/test/resources/keystore.jks";
public static String TRUSTSTORE_PATH = "src/test/resources/truststore.jks";
public static String PASSWORD = "password";
Expand All @@ -34,10 +34,10 @@ public static KeyStore loadKeystore(String path) throws Exception {

public static Properties createTestSSLProperties() {
Properties props = new Properties();
props.setProperty(Options.PROP_KEYSTORE, TestSSLUtils.KEYSTORE_PATH);
props.setProperty(Options.PROP_KEYSTORE_PASSWORD, TestSSLUtils.PASSWORD);
props.setProperty(Options.PROP_TRUSTSTORE, TestSSLUtils.TRUSTSTORE_PATH);
props.setProperty(Options.PROP_TRUSTSTORE_PASSWORD, TestSSLUtils.PASSWORD);
props.setProperty(Options.PROP_KEYSTORE, KEYSTORE_PATH);
props.setProperty(Options.PROP_KEYSTORE_PASSWORD, PASSWORD);
props.setProperty(Options.PROP_TRUSTSTORE, TRUSTSTORE_PATH);
props.setProperty(Options.PROP_TRUSTSTORE_PASSWORD, PASSWORD);
return props;
}

Expand Down
Loading
Loading