|
18 | 18 |
|
19 | 19 | import java.io.IOException; |
20 | 20 | import java.time.Duration; |
| 21 | +import java.util.ArrayList; |
| 22 | +import java.util.Collections; |
| 23 | +import java.util.List; |
21 | 24 | import java.util.concurrent.*; |
22 | 25 | import java.util.concurrent.atomic.AtomicInteger; |
23 | 26 | import java.util.concurrent.atomic.AtomicReference; |
24 | 27 |
|
25 | | -import static io.nats.client.utils.TestBase.sleep; |
26 | | -import static io.nats.client.utils.TestBase.waitUntilStatus; |
| 28 | +import static io.nats.client.utils.TestBase.*; |
27 | 29 | import static org.junit.jupiter.api.Assertions.*; |
28 | 30 |
|
29 | 31 |
|
|
33 | 35 | // the done message (or should) - wanted to note that somewhere |
34 | 36 |
|
35 | 37 | public class DispatcherTests { |
| 38 | + @Test |
| 39 | + public void testDispatcherMultipleSubscriptionsBySubject() throws Exception { |
| 40 | + try (NatsTestServer ts = new NatsTestServer(false); |
| 41 | + Connection nc = Nats.connect(ts.getURI())) { |
| 42 | + standardConnectionWait(nc); |
| 43 | + String subject1 = subject(); |
| 44 | + String subject2 = subject(); |
| 45 | + |
| 46 | + List<Integer> dflt = Collections.synchronizedList(new ArrayList<>()); |
| 47 | + List<Integer> sub21 = Collections.synchronizedList(new ArrayList<>()); |
| 48 | + List<Integer> sub22 = Collections.synchronizedList(new ArrayList<>()); |
| 49 | + List<Integer> sub31 = Collections.synchronizedList(new ArrayList<>()); |
| 50 | + List<Integer> sub32 = Collections.synchronizedList(new ArrayList<>()); |
| 51 | + Dispatcher d1 = nc.createDispatcher(m -> dflt.add(getDataId(m))); |
| 52 | + d1.subscribe(subject1); |
| 53 | + d1.subscribe(subject1, m -> sub21.add(getDataId(m))); |
| 54 | + d1.subscribe(subject1, m -> sub22.add(getDataId(m))); |
| 55 | + d1.subscribe(subject2, m -> sub31.add(getDataId(m))); |
| 56 | + d1.subscribe(subject2, m -> sub32.add(getDataId(m))); |
| 57 | + |
| 58 | + nc.publish(subject1, "1".getBytes()); |
| 59 | + nc.publish(subject2, "1".getBytes()); |
| 60 | + Thread.sleep(1000); |
| 61 | + d1.unsubscribe(subject1); |
| 62 | + nc.publish(subject1, "2".getBytes()); |
| 63 | + nc.publish(subject2, "2".getBytes()); |
| 64 | + Thread.sleep(1000); |
| 65 | + |
| 66 | + assertTrue(dflt.contains(1)); |
| 67 | + assertTrue(sub21.contains(1)); |
| 68 | + assertTrue(sub22.contains(1)); |
| 69 | + assertTrue(sub31.contains(1)); |
| 70 | + assertTrue(sub32.contains(1)); |
| 71 | + |
| 72 | + assertFalse(dflt.contains(2)); |
| 73 | + assertFalse(sub21.contains(2)); |
| 74 | + assertFalse(sub22.contains(2)); |
| 75 | + assertTrue(sub31.contains(2)); |
| 76 | + assertTrue(sub32.contains(2)); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + private static int getDataId(Message m) { |
| 81 | + return Integer.parseInt(new String(m.getData())); |
| 82 | + } |
| 83 | + |
36 | 84 | @Test |
37 | 85 | public void testSingleMessage() throws Exception { |
38 | 86 | try (NatsTestServer ts = new NatsTestServer(false); |
|
0 commit comments