@Test(expected = IllegalArgumentException.class) public void testThrowsWithoutSubject() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.publish(null, null); assertFalse(true); } }
public String getURI() { return getURIForPort(this.port); }
@Test public void testTokenInURLClusteredWithDifferentUser() throws Exception { String[] customArgs1 = {"--auth","token_one"}; String[] customArgs2 = {"--auth","token_two"}; TestHandler handler = new TestHandler(); try (NatsTestServer ts1 = new NatsTestServer(customArgs1, false); NatsTestServer ts2 = new NatsTestServer(customArgs2, false)) { // See config file for user/pass Options options = new Options.Builder(). server("nats://token_one@localhost:"+ts1.getPort()). server("nats://token_two@localhost:"+ts2.getPort()). maxReconnects(4). noRandomize(). connectionListener(handler). pingInterval(Duration.ofMillis(100)). build(); Connection nc = Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(nc.getConnectedUrl(), "nats://token_one@localhost:"+ts1.getPort()); handler.prepForStatusChange(Events.RESUBSCRIBED); ts1.close(); handler.waitForStatusChange(2, TimeUnit.SECONDS); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(nc.getConnectedUrl(), "nats://token_two@localhost:"+ts2.getPort()); nc.close(); } }
@Test public void testURISchemeOpenTLSConnection() throws Exception { try (NatsTestServer ts = new NatsTestServer("src/test/resources/tls.conf", false)) { Options options = new Options.Builder(). server("opentls://localhost:"+ts.getPort()). maxReconnects(0). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
int tsPort = NatsTestServer.nextPort(); int ts2Port = NatsTestServer.nextPort(); int tsCPort = NatsTestServer.nextPort(); int ts2CPort = NatsTestServer.nextPort(); ts = new NatsTestServer("src/test/resources/tls_noip.conf", tsInserts, tsPort, false); ts2 = new NatsTestServer("src/test/resources/tls_noip.conf", ts2Inserts, ts2Port, false); server(ts.getURI()). secure(). connectionListener(handler). nc = (NatsConnection) Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(nc.getConnectedUrl(), ts.getURI()); ts.close(); flushAndWait(nc, handler); assertEquals(ts2.getPort(), uri.getPort()); // full uri will have some ip address, just check port } finally { if (nc != null) { ts.close(); ts2.close();
@Test(expected=TimeoutException.class) public void testThrowIfCantFlush() throws Exception { TestHandler handler = new TestHandler(); try (NatsTestServer ts = new NatsTestServer(false); Connection subCon = Nats.connect(new Options.Builder().connectionListener(handler).server(ts.getURI()).build())) { assertTrue("Connected Status", Connection.Status.CONNECTED == subCon.getStatus()); subCon.flush(Duration.ofSeconds(1)); // Get the sub to the server handler.prepForStatusChange(Events.DISCONNECTED); ts.close(); // make the drain flush fail handler.waitForStatusChange(2, TimeUnit.SECONDS); // make sure the connection is down subCon.drain(Duration.ofSeconds(1)); //should throw } }
@Test public void testJWTAuthWithChainFile() throws Exception { NKey theKey = NKey.createUser(null); assertNotNull(theKey); String version = NatsTestServer.generateGnatsdVersionString(); if (!version.contains("version 2")) { // Server version doesn't support this test return; } try (NatsTestServer ts = new NatsTestServer("src/test/resources/operator.conf", true)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). authHandler(Nats.credentials("src/test/resources/jwt_nkey/user.chain")). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
Connection nc = null; TestHandler handler = new TestHandler(); int port = NatsTestServer.nextPort(); int newPort = NatsTestServer.nextPort(); try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", port, false)) { SSLContext ctx = TestSSLUtils.createTestSSLContext(); Options options = new Options.Builder(). server(ts.getURI()). server(NatsTestServer.getURIForPort(newPort)). maxReconnects(-1). sslContext(ctx). handler.prepForStatusChange(Events.RESUBSCRIBED); try (NatsTestServer ts = new NatsTestServer("src/test/resources/tlsverify.conf", newPort, false)) { handler.waitForStatusChange(10, TimeUnit.SECONDS); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus());
@Test public void testMaxReconnects() throws Exception { Connection nc = null; TestHandler handler = new TestHandler(); int port = NatsTestServer.nextPort(); try { try (NatsTestServer ts = new NatsTestServer(port, false)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(1). connectionListener(handler). reconnectWait(Duration.ofMillis(10)). build(); nc = Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); handler.prepForStatusChange(Events.CLOSED); } flushAndWait(nc, handler); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
try { int port; try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder(). server(ts.getURI()). reconnectWait(Duration.ofMillis(100)). maxReconnects(-1). connectionListener(handler). build(); port = ts.getPort(); nc = Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(ts.getURI(), nc.getConnectedUrl()); handler.prepForStatusChange(Events.DISCONNECTED); try (NatsTestServer ts = new NatsTestServer(port, false)) { try { Thread.sleep(200); assertEquals(ts.getURI(), nc.getConnectedUrl());
@Test public void testUserPassInURLOnReconnect() throws Exception { TestHandler handler = new TestHandler(); int port = NatsTestServer.nextPort(); Connection nc = null; Subscription sub = null; String[] customArgs = {"--user","stephen","--pass","password"}; try (NatsTestServer ts = new NatsTestServer(customArgs, port, false)) { server("nats://stephen:password@localhost:"+ts.getPort()). maxReconnects(-1). connectionListener(handler). try (NatsTestServer ts = new NatsTestServer(customArgs, port, false)) { handler.waitForStatusChange(5, TimeUnit.SECONDS); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus());
NatsConnection nc = null; TestHandler handler = new TestHandler(); int port = NatsTestServer.nextPort(); Duration reconnectWait = Duration.ofMillis(100); // thrash int thrashCount = 5; try (NatsTestServer ts = new NatsTestServer(port, false)) { handler.waitForStatusChange(5000, TimeUnit.MILLISECONDS); assertEquals("Connected Status", Connection.Status.CONNECTED, nc.getStatus());
public NatsServerProtocolMock(ExitAt exitAt) { this(NatsTestServer.nextPort(), exitAt); }
public NatsTestServer(String[] customArgs, int port, boolean debug) { this.port = port; this.debug = debug; this.customArgs = customArgs; start(); }
/** * Synonomous with shutdown. */ public void close() { shutdown(); } }
@Test public void testDefaultConnection() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(Options.DEFAULT_PORT, false)) { Connection nc = Nats.connect(); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
@Test public void testUserPassInURLClusteredWithDifferentUser() throws Exception { String[] customArgs1 = {"--user","stephen","--pass","password"}; String[] customArgs2 = {"--user","alberto","--pass","casadecampo"}; TestHandler handler = new TestHandler(); try (NatsTestServer ts1 = new NatsTestServer(customArgs1, false); NatsTestServer ts2 = new NatsTestServer(customArgs2, false)) { // See config file for user/pass Options options = new Options.Builder(). server("nats://stephen:password@localhost:"+ts1.getPort()). server("nats://alberto:casadecampo@localhost:"+ts2.getPort()). maxReconnects(4). noRandomize(). connectionListener(handler). pingInterval(Duration.ofMillis(100)). build(); Connection nc = Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(nc.getConnectedUrl(), "nats://stephen:password@localhost:"+ts1.getPort()); handler.prepForStatusChange(Events.RESUBSCRIBED); ts1.close(); handler.waitForStatusChange(2, TimeUnit.SECONDS); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(nc.getConnectedUrl(), "nats://alberto:casadecampo@localhost:"+ts2.getPort()); nc.close(); } }
@Test public void testUserPassInURL() throws Exception { String[] customArgs = {"--user","stephen","--pass","password"}; try (NatsTestServer ts = new NatsTestServer(customArgs, false)) { // See config file for user/pass Options options = new Options.Builder(). server("nats://stephen:password@localhost:"+ts.getPort()). maxReconnects(0). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }
String[] customArgs = {"--user","stephen","--pass","password"}; try (NatsTestServer ts = new NatsTestServer(); NatsTestServer ts2 = new NatsTestServer(customArgs, false); //ts2 requires auth NatsTestServer ts3 = new NatsTestServer()) { Options options = new Options.Builder(). server(ts.getURI()). server(ts2.getURI()). server(ts3.getURI()). noRandomize(). connectionListener(handler). nc = (NatsConnection) Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(ts.getURI(), nc.getConnectedUrl()); handler.prepForStatusChange(Events.DISCONNECTED); ts.close(); try { nc.flush(Duration.ofSeconds(1)); assertTrue(nc.getLastError().indexOf("Authorization Violation") >= 0); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(ts3.getURI(), nc.getConnectedUrl()); } finally { if (nc != null) {