-
Notifications
You must be signed in to change notification settings - Fork 159
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
fix test failing because of timeout that aren't testing timing #375
Changes from all commits
b88c57a
b205278
5bd649b
874a560
3c9bb72
af728fc
0374297
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,31 +13,26 @@ | |
|
||
package io.nats.client; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
import static org.junit.jupiter.api.Assertions.assertFalse; | ||
import static org.junit.jupiter.api.Assertions.assertNotNull; | ||
import static org.junit.jupiter.api.Assertions.assertThrows; | ||
import static org.junit.jupiter.api.Assertions.assertTrue; | ||
import io.nats.client.ConnectionListener.Events; | ||
import io.nats.client.NatsServerProtocolMock.ExitAt; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.io.IOException; | ||
import java.time.Duration; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import org.junit.jupiter.api.Test; | ||
|
||
import io.nats.client.ConnectionListener.Events; | ||
import io.nats.client.NatsServerProtocolMock.ExitAt; | ||
import static io.nats.client.impl.TestMacros.*; | ||
import static org.junit.jupiter.api.Assertions.*; | ||
|
||
public class ConnectTests { | ||
@Test | ||
public void testDefaultConnection() throws IOException, InterruptedException { | ||
try (NatsTestServer ts = new NatsTestServer(Options.DEFAULT_PORT, false)) { | ||
Connection nc = Nats.connect(); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -47,10 +42,9 @@ public void testConnection() throws IOException, InterruptedException { | |
try (NatsTestServer ts = new NatsTestServer(false)) { | ||
Connection nc = Nats.connect(ts.getURI()); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -61,10 +55,9 @@ public void testConnectionWithOptions() throws IOException, InterruptedException | |
Options options = new Options.Builder().server(ts.getURI()).build(); | ||
Connection nc = Nats.connect(options); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -74,10 +67,9 @@ public void testFullFakeConnect() throws IOException, InterruptedException { | |
try (NatsServerProtocolMock ts = new NatsServerProtocolMock(ExitAt.NO_EXIT)) { | ||
Connection nc = Nats.connect(ts.getURI()); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -88,10 +80,9 @@ public void testFullFakeConnectWithTabs() throws IOException, InterruptedExcepti | |
ts.useTabs(); | ||
Connection nc = Nats.connect(ts.getURI()); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -105,10 +96,7 @@ public void testConnectExitBeforeInfo() { | |
try { | ||
nc = Nats.connect(opt); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -123,10 +111,7 @@ public void testConnectExitAfterInfo() { | |
try { | ||
nc = Nats.connect(opt); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -141,10 +126,7 @@ public void testConnectExitAfterConnect() { | |
try { | ||
nc = Nats.connect(opt); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -159,10 +141,7 @@ public void testConnectExitAfterPing() { | |
try { | ||
nc = Nats.connect(opt); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -176,10 +155,9 @@ public void testConnectionFailureWithFallback() throws IOException, InterruptedE | |
Options options = new Options.Builder().connectionTimeout(Duration.ofSeconds(5)).server(fake.getURI()).server(ts.getURI()).build(); | ||
Connection nc = Nats.connect(options); | ||
try { | ||
assertEquals(Connection.Status.CONNECTED, nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -190,10 +168,9 @@ public void testConnectWithConfig() throws IOException, InterruptedException { | |
try (NatsTestServer ts = new NatsTestServer("src/test/resources/simple.conf", false)) { | ||
Connection nc = Nats.connect(ts.getURI()); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -204,10 +181,9 @@ public void testConnectWithCommas() throws IOException, InterruptedException { | |
try (NatsTestServer ts2 = new NatsTestServer(false)) { | ||
Connection nc = Nats.connect(ts1.getURI() + "," + ts2.getURI()); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -224,16 +200,15 @@ public void testConnectRandomize() throws IOException, InterruptedException { | |
for (int i=0; i < 10; i++) { | ||
Connection nc = Nats.connect(ts1.getURI() + "," + ts2.getURI()); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
|
||
if (nc.getConnectedUrl().equals(ts1.getURI())) { | ||
one++; | ||
} else { | ||
two++; | ||
} | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
|
||
|
@@ -256,21 +231,20 @@ public void testConnectNoRandomize() throws IOException, InterruptedException { | |
Options options = new Options.Builder().noRandomize().servers(servers).build(); | ||
Connection nc = Nats.connect(options); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
|
||
if (nc.getConnectedUrl().equals(ts1.getURI())) { | ||
one++; | ||
} else { | ||
two++; | ||
} | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
|
||
assertTrue(one == 10, "always got one"); | ||
assertTrue(two == 0, "never got two"); | ||
assertEquals(one, 10, "always got one"); | ||
assertEquals(two, 0, "never got two"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nitty for sure, but again how the assertions were designed. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see linter complaints about these... thanks. |
||
} | ||
} | ||
} | ||
|
@@ -285,10 +259,7 @@ public void testFailWithMissingLineFeedAfterInfo() { | |
try { | ||
nc = Nats.connect(options); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -304,10 +275,7 @@ public void testFailWithStuffAfterInitialInfo() { | |
try { | ||
nc = Nats.connect(options); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -324,10 +292,7 @@ public void testFailWrongInitialInfoOP() { | |
try { | ||
nc = Nats.connect(options); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -343,10 +308,7 @@ public void testIncompleteInitialInfo() { | |
try { | ||
nc = Nats.connect(options); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
}); | ||
|
@@ -371,12 +333,9 @@ public void testAsyncConnection() throws IOException, InterruptedException { | |
try { | ||
nc = handler.getConnection(); | ||
assertNotNull(nc); | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
} | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -396,26 +355,17 @@ public void testAsyncConnectionWithReconnect() throws IOException, InterruptedEx | |
try { | ||
Nats.connectAsynchronously(options, true); | ||
|
||
// No server at this point, let it fail and try to start over | ||
try { | ||
Thread.sleep(1000); | ||
} catch (Exception exp) { | ||
|
||
} | ||
sleep(5000); // No server at this point, let it fail and try to start over | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sleep macro just cleaner. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. could be a bit more deterministic and use |
||
|
||
nc = handler.getConnection(); // will be disconnected, but should be there | ||
assertNotNull(nc); | ||
|
||
handler.prepForStatusChange(Events.RECONNECTED); | ||
try (NatsTestServer ts = new NatsTestServer(port, false)) { | ||
handler.waitForStatusChange(5, TimeUnit.SECONDS); | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
waitThenAssertConnected(nc, handler); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This macro fixed the flappers that didn't allow enough time for the connection. With the macro it's consistent. Only used in tests where they are not testing the connection itself, but using the connection to test something. This version of the macro waits up to 5 seconds. There is another version that you can specify the wait. |
||
} | ||
} finally { | ||
if (nc != null) { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
} | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
|
||
|
@@ -459,7 +409,7 @@ public void testConnectionTimeout() { | |
connectionTimeout(Duration.ofSeconds(2)). // 2 is also the default but explicit for test | ||
build(); | ||
Connection nc = Nats.connect(options); | ||
assertFalse(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertNotSame(Connection.Status.CONNECTED, nc.getStatus(), "Connected Status"); | ||
} | ||
}); | ||
} | ||
|
@@ -474,10 +424,9 @@ public void testSlowConnectionNoTimeout() throws IOException, InterruptedExcepti | |
build(); | ||
Connection nc = Nats.connect(options); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
@@ -491,16 +440,15 @@ public void testTimeCheckCoverage() throws IOException, InterruptedException { | |
build(); | ||
Connection nc = Nats.connect(options); | ||
try { | ||
assertTrue(Connection.Status.CONNECTED == nc.getStatus(), "Connected Status"); | ||
assertConnected(nc); | ||
} finally { | ||
nc.close(); | ||
assertTrue(Connection.Status.CLOSED == nc.getStatus(), "Closed Status"); | ||
closeThenAssertClosed(nc); | ||
} | ||
} | ||
} | ||
|
||
@Test | ||
public void testConnectExceptionHasURLS() throws IOException, InterruptedException { | ||
public void testConnectExceptionHasURLS() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. exception was never thrown |
||
try { | ||
Nats.connect("nats://testserver.notnats:4222, nats://testserver.alsonotnats:4223"); | ||
} catch (Exception e) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I noticed it was the same thing over and over with no variation. "Macros" aren't really a java thing, just something I like. I think they add description to the tests and simplify routine scaffolding code.