d.unsubscribe(this.inbox);
this.inboxDispatcher.get().subscribe(responseInbox).unsubscribe(responseInbox, 1);
d.unsubscribe(this.inbox);
this.inboxDispatcher.get().subscribe(responseInbox).unsubscribe(responseInbox, 1);
d.unsubscribe(sub.inbox); throw new IOException(ERR_SUB_REQ_TIMEOUT); response = SubscriptionResponse.parseFrom(reply.getData()); } catch (InvalidProtocolBufferException e) { d.unsubscribe(sub.inbox); throw e; d.unsubscribe(sub.inbox); throw new IOException(response.getError());
d.unsubscribe(sub.inbox); throw new IOException(ERR_SUB_REQ_TIMEOUT); response = SubscriptionResponse.parseFrom(reply.getData()); } catch (InvalidProtocolBufferException e) { d.unsubscribe(sub.inbox); throw e; d.unsubscribe(sub.inbox); throw new IOException(response.getError());
@Test public void testUnsubFromCallback() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final AtomicReference<Dispatcher> dispatcher = new AtomicReference<>(); final ConcurrentLinkedQueue<Message> q = new ConcurrentLinkedQueue<>(); final Dispatcher d = nc.createDispatcher((msg) -> { if (msg.getSubject().equals("done")) { done.complete(Boolean.TRUE); } else { q.add(msg); dispatcher.get().unsubscribe("subject"); } }); dispatcher.set(d); d.subscribe("subject"); d.subscribe("done"); nc.flush(Duration.ofMillis(500));// Get them all to the server nc.publish("subject", new byte[16]); nc.publish("subject", new byte[16]); nc.publish("done", new byte[16]); // when we get this we know the others are dispatched nc.flush(Duration.ofMillis(1000)); // Wait for the publish, or we will get multiples for sure done.get(200, TimeUnit.MILLISECONDS); // make sure we got them assertEquals(1, q.size()); } }
@Test public void testAutoUnsubFromCallback() throws IOException, InterruptedException, ExecutionException, TimeoutException { try (NatsTestServer ts = new NatsTestServer(false); Connection nc = Nats.connect(ts.getURI())) { final CompletableFuture<Boolean> done = new CompletableFuture<>(); assertTrue("Connected Status", Connection.Status.CONNECTED == nc.getStatus()); final AtomicReference<Dispatcher> dispatcher = new AtomicReference<>(); final ConcurrentLinkedQueue<Message> q = new ConcurrentLinkedQueue<>(); final Dispatcher d = nc.createDispatcher((msg) -> { if (msg.getSubject().equals("done")) { done.complete(Boolean.TRUE); } else { q.add(msg); dispatcher.get().unsubscribe("subject", 2); // get 1 more, for a total of 2 } }); dispatcher.set(d); d.subscribe("subject"); d.subscribe("done"); nc.flush(Duration.ofMillis(1000));// Get them all to the server nc.publish("subject", new byte[16]); nc.publish("subject", new byte[16]); nc.publish("subject", new byte[16]); nc.publish("done", new byte[16]); // when we get this we know the others are dispatched nc.flush(Duration.ofMillis(1000)); // Wait for the publish done.get(200, TimeUnit.MILLISECONDS); // make sure we got them assertEquals(2, q.size()); } }
@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); } }
d.unsubscribe("subject"); nc.flush(Duration.ofMillis(1000));// Get them all to the server
@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); } }
d.unsubscribe("draintest");