@Test(expected=IllegalArgumentException.class) public void testThrowOnAsyncWithoutListener() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder(). server(ts.getURI()). build(); Nats.connectAsynchronously(options, false); } }
Nats.connectAsynchronously(options, true);
@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 testAsyncConnection() throws IOException, InterruptedException { TestHandler handler = new TestHandler(); Connection nc = null; try (NatsTestServer ts = new NatsTestServer(false)) { Options options = new Options.Builder(). server(ts.getURI()). connectionListener(handler). build(); handler.prepForStatusChange(Events.CONNECTED); Nats.connectAsynchronously(options, false); handler.waitForStatusChange(1, TimeUnit.SECONDS); try { nc = handler.getConnection(); assertNotNull(nc); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { if (nc != null) { nc.close(); } assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }