|
1 | 1 | package org.tron.common.logsfilter; |
2 | 2 |
|
3 | | -import java.util.concurrent.ExecutorService; |
| 3 | +import static org.mockito.ArgumentMatchers.anyString; |
| 4 | +import static org.mockito.Mockito.inOrder; |
| 5 | +import static org.mockito.Mockito.mock; |
| 6 | +import static org.mockito.Mockito.mockConstruction; |
| 7 | +import static org.mockito.Mockito.when; |
| 8 | + |
4 | 9 | import org.junit.After; |
5 | 10 | import org.junit.Assert; |
| 11 | +import org.junit.Before; |
6 | 12 | import org.junit.Test; |
7 | | -import org.tron.common.es.ExecutorServiceManager; |
| 13 | +import org.mockito.InOrder; |
| 14 | +import org.mockito.MockedConstruction; |
8 | 15 | import org.tron.common.logsfilter.nativequeue.NativeMessageQueue; |
9 | 16 | import org.zeromq.SocketType; |
10 | 17 | import org.zeromq.ZContext; |
11 | 18 | import org.zeromq.ZMQ; |
12 | 19 |
|
13 | 20 | public class NativeMessageQueueTest { |
14 | 21 |
|
15 | | - public int bindPort = 5555; |
16 | | - public String dataToSend = "################"; |
17 | | - public String topic = "testTopic"; |
18 | | - |
19 | | - private ExecutorService subscriberExecutor; |
20 | | - private final String zmqSubscriber = "zmq-subscriber"; |
| 22 | + private NativeMessageQueue queue; |
| 23 | + private ZMQ.Socket publisher; |
| 24 | + private MockedConstruction<ZContext> contexts; |
| 25 | + |
| 26 | + @Before |
| 27 | + public void setUp() { |
| 28 | + publisher = mock(ZMQ.Socket.class); |
| 29 | + when(publisher.bind(anyString())).thenReturn(true); |
| 30 | + contexts = mockConstruction(ZContext.class, (context, construction) -> |
| 31 | + when(context.createSocket(SocketType.PUB)).thenReturn(publisher)); |
| 32 | + queue = new NativeMessageQueue(); |
| 33 | + } |
21 | 34 |
|
22 | 35 | @After |
23 | 36 | public void tearDown() { |
24 | | - ExecutorServiceManager.shutdownAndAwaitTermination(subscriberExecutor, zmqSubscriber); |
25 | | - subscriberExecutor = null; |
| 37 | + try { |
| 38 | + if (queue != null) { |
| 39 | + queue.stop(); |
| 40 | + } |
| 41 | + } finally { |
| 42 | + if (contexts != null) { |
| 43 | + contexts.close(); |
| 44 | + } |
| 45 | + } |
26 | 46 | } |
27 | 47 |
|
28 | 48 | @Test |
29 | | - public void invalidBindPort() { |
30 | | - boolean bRet = NativeMessageQueue.getInstance().start(-1111, 0); |
31 | | - Assert.assertEquals(true, bRet); |
32 | | - NativeMessageQueue.getInstance().stop(); |
| 49 | + public void configuredSendQueueLengthIsAppliedBeforeSocketCreation() { |
| 50 | + assertStartup(6000, 2000, 6000, 2000); |
33 | 51 | } |
34 | 52 |
|
35 | 53 | @Test |
36 | | - public void invalidSendLength() { |
37 | | - boolean bRet = NativeMessageQueue.getInstance().start(0, -2222); |
38 | | - Assert.assertEquals(true, bRet); |
39 | | - NativeMessageQueue.getInstance().stop(); |
| 54 | + public void invalidBindPortUsesDefaultPort() { |
| 55 | + assertStartup(-1111, 1000, 5555, 1000); |
40 | 56 | } |
41 | 57 |
|
42 | 58 | @Test |
43 | | - public void publishTrigger() { |
44 | | - |
45 | | - int sendLength = 0; |
46 | | - boolean bRet = NativeMessageQueue.getInstance().start(bindPort, sendLength); |
47 | | - Assert.assertEquals(true, bRet); |
48 | | - |
49 | | - startSubscribeThread(); |
50 | | - |
51 | | - try { |
52 | | - Thread.sleep(1000); |
53 | | - } catch (InterruptedException e) { |
54 | | - Thread.currentThread().interrupt(); |
55 | | - } |
56 | | - |
57 | | - NativeMessageQueue.getInstance().publishTrigger(dataToSend, topic); |
58 | | - |
59 | | - try { |
60 | | - Thread.sleep(1000); |
61 | | - } catch (InterruptedException e) { |
62 | | - Thread.currentThread().interrupt(); |
63 | | - } |
| 59 | + public void negativeSendQueueLengthUsesDefaultSndHWM() { |
| 60 | + assertStartup(6000, -1, 6000, 1000); |
| 61 | + } |
64 | 62 |
|
65 | | - NativeMessageQueue.getInstance().stop(); |
| 63 | + @Test |
| 64 | + public void zeroSendQueueLengthUsesDefaultSndHWM() { |
| 65 | + assertStartup(6000, 0, 6000, 1000); |
66 | 66 | } |
67 | 67 |
|
68 | | - public void startSubscribeThread() { |
69 | | - subscriberExecutor = ExecutorServiceManager.newSingleThreadExecutor(zmqSubscriber); |
70 | | - subscriberExecutor.execute(() -> { |
71 | | - try (ZContext context = new ZContext()) { |
72 | | - ZMQ.Socket subscriber = context.createSocket(SocketType.SUB); |
| 68 | + @Test |
| 69 | + public void publishTriggerSendsTopicBeforePayload() { |
| 70 | + Assert.assertTrue(queue.start(6000, 1000)); |
73 | 71 |
|
74 | | - Assert.assertTrue(subscriber.connect(String.format("tcp://localhost:%d", bindPort))); |
75 | | - Assert.assertTrue(subscriber.subscribe(topic)); |
| 72 | + queue.publishTrigger("payload", "topic"); |
76 | 73 |
|
77 | | - while (!Thread.currentThread().isInterrupted()) { |
78 | | - byte[] message = subscriber.recv(); |
79 | | - String triggerMsg = new String(message); |
| 74 | + InOrder delivery = inOrder(publisher); |
| 75 | + delivery.verify(publisher).bind("tcp://*:6000"); |
| 76 | + delivery.verify(publisher).sendMore("topic"); |
| 77 | + delivery.verify(publisher).send("payload"); |
| 78 | + delivery.verifyNoMoreInteractions(); |
| 79 | + } |
80 | 80 |
|
81 | | - Assert.assertTrue(triggerMsg.contains(dataToSend) || triggerMsg.contains(topic)); |
82 | | - } |
83 | | - // ZMQ.Socket will be automatically closed when ZContext is closed |
84 | | - } |
85 | | - }); |
| 81 | + private void assertStartup(int port, int queueLength, int expectedPort, int expectedQueueLength) { |
| 82 | + Assert.assertTrue(queue.start(port, queueLength)); |
| 83 | + Assert.assertEquals(1, contexts.constructed().size()); |
| 84 | + ZContext context = contexts.constructed().get(0); |
| 85 | + |
| 86 | + // ZContext applies its defaults when creating the socket, so ordering matters. |
| 87 | + InOrder startup = inOrder(context, publisher); |
| 88 | + startup.verify(context).setSndHWM(expectedQueueLength); |
| 89 | + startup.verify(context).createSocket(SocketType.PUB); |
| 90 | + startup.verify(publisher).bind("tcp://*:" + expectedPort); |
| 91 | + startup.verifyNoMoreInteractions(); |
86 | 92 | } |
87 | 93 | } |
0 commit comments