try (NatsTestServer ts = new NatsTestServer(false); NatsConnection nc = (NatsConnection) Nats.connect(new Options.Builder(). server(ts.getURI()).errorListener(handler).build())) {
TestHandler handler = new TestHandler(); try (NatsTestServer ts = new NatsTestServer(false); Connection subCon = Nats.connect(new Options.Builder().server(ts.getURI()).errorListener(handler).maxReconnects(0).build()); Connection pubCon = Nats.connect(new Options.Builder().server(ts.getURI()).maxReconnects(0).build())) { assertTrue("Connected Status", Connection.Status.CONNECTED == subCon.getStatus());
server(ts.getURI()). maxReconnects(0). errorListener(handler). build(); Connection nc = Nats.connect(options);
server(ts.getURI()). maxReconnects(0). errorListener(handler). build(); Connection nc = Nats.connect(options);
@Test public void testExceptionInErrorHandler() throws Exception { String[] customArgs = {"--user","stephen","--pass","password"}; BadHandler handler = new BadHandler(); Connection nc = null; try (NatsTestServer ts = new NatsTestServer(customArgs, false)) { // See config file for user/pass Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). errorListener(handler). // skip this so we get an error userInfo("stephen", "password"). build(); try { nc = Nats.connect(options); assertFalse(true); } catch(IOException e) { assertTrue(true); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
@Test public void testErrorOnNoAuth() throws Exception { String[] customArgs = {"--user","stephen","--pass","password"}; TestHandler handler = new TestHandler(); Connection nc = null; try (NatsTestServer ts = new NatsTestServer(customArgs, false)) { // See config file for user/pass Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). errorListener(handler). // skip this so we get an error userInfo("stephen", "password"). build(); try { nc = Nats.connect(options); } catch(IOException e) { assertTrue(handler.getCount() > 0); assertEquals(1, handler.getErrorCount("Authorization Violation")); } finally { if (nc != null) { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
void runBadContentTest(NatsServerProtocolMock.Customizer badServer, CompletableFuture<Boolean> ready) throws Exception { TestHandler handler = new TestHandler(); try (NatsServerProtocolMock ts = new NatsServerProtocolMock(badServer, null)) { Options options = new Options.Builder(). server(ts.getURI()). maxReconnects(0). errorListener(handler). connectionListener(handler). build(); Connection nc = Nats.connect(options); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); handler.prepForStatusChange(Events.DISCONNECTED); ready.complete(Boolean.TRUE); handler.waitForStatusChange(200, TimeUnit.MILLISECONDS); assertTrue(handler.getExceptionCount() > 0); assertTrue("Disconnected Status", Connection.Status.DISCONNECTED == nc.getStatus() || Connection.Status.CLOSED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } } }
@Test public void testExceptionInSlowConsumerHandler() throws Exception { BadHandler handler = new BadHandler(); try (NatsTestServer ts = new NatsTestServer(false); NatsConnection nc = (NatsConnection) Nats.connect(new Options.Builder(). server(ts.getURI()). errorListener(handler). build())) { Subscription sub = nc.subscribe("subject"); sub.setPendingLimits(1, -1); nc.publish("subject", null); nc.publish("subject", null); nc.publish("subject", null); nc.publish("subject", null); nc.flush(Duration.ofMillis(5000)); assertEquals(3, sub.getDroppedCount()); nc.close(); // should force the exception handler through assertTrue(((NatsConnection)nc).getNatsStatistics().getExceptions()>0); } }
io.nats.client.Connection createNatsConnection() throws IOException, InterruptedException { io.nats.client.Connection nc = null; if (getNatsConnection() == null) { if (opts.getNatsUrl() != null) { io.nats.client.Options natsOpts = new io.nats.client.Options.Builder(). connectionName(clientId). errorListener(opts.getErrorListener()). connectionListener(opts.getConnectionListener()). server(opts.getNatsUrl()). build(); nc = Nats.connect(natsOpts); } else { nc = Nats.connect(); } ncOwned = true; } return nc; }
io.nats.client.Connection createNatsConnection() throws IOException, InterruptedException { io.nats.client.Connection nc = null; if (getNatsConnection() == null) { if (opts.getNatsUrl() != null) { io.nats.client.Options natsOpts = new io.nats.client.Options.Builder(). connectionName(clientId). errorListener(opts.getErrorListener()). connectionListener(opts.getConnectionListener()). server(opts.getNatsUrl()). build(); nc = Nats.connect(natsOpts); } else { nc = Nats.connect(); } ncOwned = true; } return nc; }
@Test public void testErrorOnAsync() throws IOException, InterruptedException { TestHandler handler = new TestHandler(); Options options = new Options.Builder(). server("nats://localhost:"+NatsTestServer.nextPort()). connectionListener(handler). errorListener(handler). noReconnect(). build(); handler.prepForStatusChange(Events.CLOSED); Nats.connectAsynchronously(options, false); handler.waitForStatusChange(10, TimeUnit.SECONDS); assertTrue(1 <= handler.getExceptionCount()); assertTrue(handler.getConnection() == null || Connection.Status.CLOSED == handler.getConnection().getStatus()); } }
@Test public void testChainedErrorHandler() { TestHandler handler = new TestHandler(); Options o = new Options.Builder().errorListener(handler).build(); assertEquals("default verbose", false, o.isVerbose()); // One from a different type assertEquals("chained error handler", handler, o.getErrorListener()); }