nc = Nats.connect(options); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(ts.getURI(), nc.getConnectedUrl()); handler.prepForStatusChange(Events.DISCONNECTED); assertNull(nc.getConnectedUrl()); assertEquals(ts.getURI(), nc.getConnectedUrl()); assertNull(nc.getConnectedUrl());
@Test public void testCloseCount() throws Exception { try (NatsTestServer ts = new NatsTestServer(false)) { TestHandler handler = new TestHandler(); Options options = new Options.Builder(). server(ts.getURI()). connectionListener(handler). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); assertEquals(ts.getURI(), nc.getConnectedUrl()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); assertNull(nc.getConnectedUrl()); } assertEquals(1, handler.getEventCount(Events.CLOSED)); } }
@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 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 testTokenInURLWithFallback() 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://localhost:"+ts2.getPort()). token("token_two"). 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://localhost:"+ts2.getPort()); nc.close(); } }
@Test public void testUserPassInURLWithFallback() 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://localhost:"+ts2.getPort()). userInfo("alberto", "casadecampo"). 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://localhost:"+ts2.getPort()); nc.close(); } }