@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=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 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 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 testSingleMessage() 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); assertTrue(d.isActive()); assertEquals("subject", msg.getSubject()); assertNotNull(msg.getSubscription()); assertNull(msg.getReplyTo()); assertEquals(16, msg.getData().length); } }
@Test public void testSimpleString() 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 = "hello world"; byte[] bodyBytes = body.getBytes(StandardCharsets.UTF_8); Future<Message> incoming = nc.request("subject", bodyBytes); Message msg = incoming.get(50000, 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 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('.')); } }
@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 testRequestWithCustomInbox() throws IOException, ExecutionException, TimeoutException, InterruptedException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(new Options.Builder().inboxPrefix("myinbox").server(ts.getURI()).maxReconnects(0).build())) { assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); Dispatcher d = nc.createDispatcher((msg) -> { assertTrue(msg.getReplyTo().startsWith("myinbox")); 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 testMultipleRequest() 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(), new byte[7]); }); d.subscribe("subject"); for (int i=0; i<10; i++) { Future<Message> incoming = nc.request("subject", new byte[11]); Message msg = incoming.get(500, TimeUnit.MILLISECONDS); assertEquals(0, ((NatsStatistics)nc.getStatistics()).getOutstandingRequests()); assertNotNull(msg); assertEquals(7, msg.getData().length); assertTrue(msg.getSubject().indexOf('.') < msg.getSubject().lastIndexOf('.')); } } }