private Connection initConnection() throws IOException { Options options = new Options.Builder() .errorCb(this::connectionError) .disconnectedCb(this::disconnected) .reconnectedCb(this::reconnected) .build(); return Nats.connect(this.server, options); }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptyQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe("subject", ""); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.subscribe(null, "quque"); assertFalse(true); } }
@Test(expected=IOException.class) public void testLongProtocolOpThrows() throws Exception { try (NatsTestServer ts = new NatsTestServer(false); NatsConnection nc = (NatsConnection) Nats.connect(ts.getURI())) { NatsConnectionReader reader = nc.getReader(); byte[] bytes = ("thisistoolong\r\n").getBytes(StandardCharsets.US_ASCII); reader.fakeReadForTest(bytes); reader.gatherOp(bytes.length); assertFalse(true); } }
@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); } }
@Test(expected=IOException.class) public void testMissingLineFeed() throws Exception { try (NatsTestServer ts = new NatsTestServer(false); NatsConnection nc = (NatsConnection) Nats.connect(ts.getURI())) { NatsConnectionReader reader = nc.getReader(); byte[] bytes = ("PING\rPONG").getBytes(StandardCharsets.US_ASCII); reader.fakeReadForTest(bytes); reader.gatherOp(bytes.length); assertFalse(true); } }
@Test(expected = IllegalArgumentException.class) public void testThrowsWithoutReplyTo() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.publish("subject", "", null); assertFalse(true); } }
@Test(expected = IllegalArgumentException.class) public void testThrowsWithoutSubject() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.request(null, null); assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void throwsOnSubscribeIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.close(); nc.subscribe("subject"); assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void throwsIfClosedOnPublish() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.close(); nc.publish("subject", "replyto", null); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubject() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe(""); assertFalse(true); } }
@Test(expected = TimeoutException.class) public void throwsIfClosedOnFlush() throws IOException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.close(); nc.flush(null); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptyQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("subject", ""); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe(null, "quque"); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnEmptySubjectWithQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("", "quque"); assertFalse(true); } }
@Test(expected=IllegalStateException.class) public void testThrowOnConnClosed() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.close(); nc.closeDispatcher(d); assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void throwsOnUnsubscribeIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Subscription sub = nc.subscribe("subject"); nc.close(); sub.unsubscribe(); assertFalse(true); } }
@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(expected=IllegalArgumentException.class) public void testThrowOnDoubleClose() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.closeDispatcher(d); nc.closeDispatcher(d); assertFalse(true); } }
@Test public void testConnection() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false)) { Connection nc = Nats.connect(ts.getURI()); try { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); } finally { nc.close(); assertTrue("Closed Status", Connection.Status.CLOSED == nc.getStatus()); } } }