@Test(expected = IllegalStateException.class) public void testCantUnsubSubFromDispatcher() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final CompletableFuture<Message> msgFuture = new CompletableFuture<>(); Dispatcher d = nc.createDispatcher((msg) -> { msgFuture.complete(msg); }); d.subscribe("subject"); nc.flush(Duration.ofMillis(500));// Get them all to the server nc.publish("subject", new byte[16]); Message msg = msgFuture.get(500, TimeUnit.MILLISECONDS); msg.getSubscription().unsubscribe(); // Should throw assertFalse(true); } }
@Test(expected = IllegalStateException.class) public void testCantAutoUnsubSubFromDispatcher() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final CompletableFuture<Message> msgFuture = new CompletableFuture<>(); Dispatcher d = nc.createDispatcher((msg) -> { msgFuture.complete(msg); }); d.subscribe("subject"); nc.flush(Duration.ofMillis(500));// Get them all to the server nc.publish("subject", new byte[16]); Message msg = msgFuture.get(500, TimeUnit.MILLISECONDS); msg.getSubscription().unsubscribe(1); // Should throw 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 = IllegalStateException.class) public void throwsOnCreateIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { nc.close(); nc.createDispatcher((msg) -> {}); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullSubjectInUnsub() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.unsubscribe(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=IllegalArgumentException.class) public void testThrowOnNullSubject() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe(null); assertFalse(true); } }
@Test(expected=IllegalArgumentException.class) public void testThrowOnNullQueue() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("subject", null); 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 throwsOnSubscribeIfClosed() throws IOException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.close(); d.subscribe("subject"); assertFalse(true); } }
@Test(expected=IllegalStateException.class) public void testThrowOnSubscribeWhenClosed() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); nc.closeDispatcher(d); d.subscribe("foo"); assertFalse(true); } }
@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 testManualRequestReply() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Dispatcher d = nc.createDispatcher((msg) -> { nc.publish(msg.getReplyTo(), msg.getData()); }); d.subscribe("request"); Subscription sub = nc.subscribe("reply"); nc.publish("request", "reply", "hello".getBytes(StandardCharsets.UTF_8)); Message msg = sub.nextMessage(Duration.ofMillis(400)); assertNotNull(msg); assertEquals("hello", new String(msg.getData(), StandardCharsets.UTF_8)); } }
@Test(expected=IllegalStateException.class) public void testThrowOnUnsubscribeWhenClosed() throws IOException, InterruptedException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { Dispatcher d = nc.createDispatcher((msg) -> {}); d.subscribe("foo"); nc.closeDispatcher(d); d.unsubscribe("foo"); assertFalse(true); } }
@Test public void testUTF8String() throws Exception { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Dispatcher d = nc.createDispatcher((msg) -> { nc.publish(msg.getReplyTo(), msg.getData()); }); d.subscribe("subject"); String body = "??????"; byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8); Future<Message> incoming = nc.request("subject", bodyBytes); Message msg = incoming.get(500, TimeUnit.MILLISECONDS); assertNotNull(msg); assertEquals(bodyBytes.length, msg.getData().length); assertEquals(body, new String(msg.getData(), StandardCharsets.UTF_8)); } }
@Test public void testZeros() throws Exception { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Dispatcher d = nc.createDispatcher((msg) -> { nc.publish(msg.getReplyTo(), msg.getData()); }); d.subscribe("subject"); byte[] data = new byte[17]; Future<Message> incoming = nc.request("subject", data); Message msg = incoming.get(500, TimeUnit.MILLISECONDS); assertNotNull(msg); assertEquals(data.length, msg.getData().length); assertTrue(Arrays.equals(msg.getData(), data)); } }
@Test public void testSimpleRequest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(new Options.Builder().server(ts.getURI()).maxReconnects(0).build())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Dispatcher d = nc.createDispatcher((msg) -> { assertTrue(msg.getReplyTo().startsWith(Options.DEFAULT_INBOX_PREFIX)); nc.publish(msg.getReplyTo(), null); }); d.subscribe("subject"); Future<Message> incoming = nc.request("subject", null); Message msg = incoming.get(500, TimeUnit.MILLISECONDS); assertEquals(0, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); assertNotNull(msg); assertEquals(0, msg.getData().length); assertTrue(msg.getSubject().indexOf('.') < msg.getSubject().lastIndexOf('.')); } }
@Test public void testSafeRequest() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(new Options.Builder().server(ts.getURI()).maxReconnects(0).build())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Dispatcher d = nc.createDispatcher((msg) -> { nc.publish(msg.getReplyTo(), null); }); d.subscribe("subject"); Message msg = nc.request("subject", null, Duration.ofMillis(1000)); assertEquals(0, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); assertNotNull(msg); assertEquals(0, msg.getData().length); assertTrue(msg.getSubject().indexOf('.') < msg.getSubject().lastIndexOf('.')); } }