1
2
3
4
5
6
7
8
9
10
11
12
13
14 package ch.qos.logback.core.net.server;
15
16 import java.io.ByteArrayInputStream;
17 import java.io.ByteArrayOutputStream;
18 import java.io.ObjectInputStream;
19
20 import org.junit.jupiter.api.Assertions;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23
24 import ch.qos.logback.core.net.mock.MockContext;
25
26
27
28
29
30
31 public class RemoteReceiverStreamClientTest {
32
33 private static final String TEST_EVENT = "test event";
34
35 private MockContext context = new MockContext();
36
37 private MockEventQueue queue = new MockEventQueue();
38
39 private ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
40
41 private RemoteReceiverStreamClient client = new RemoteReceiverStreamClient("someId", outputStream);
42
43 @BeforeEach
44 public void setUp() throws Exception {
45 client.setContext(context);
46 client.setQueue(queue);
47 }
48
49 @Test
50 public void testOfferEventAndRun() throws Exception {
51 client.offer(TEST_EVENT);
52
53 Thread thread = new Thread(client);
54 thread.start();
55
56
57 thread.join(1000);
58 Assertions.assertFalse(thread.isAlive());
59
60 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
61 Assertions.assertEquals(TEST_EVENT, ois.readObject());
62 }
63
64 @Test
65 public void testOfferEventSequenceAndRun() throws Exception {
66 for (int i = 0; i < 10; i++) {
67 client.offer(TEST_EVENT + i);
68 }
69
70 Thread thread = new Thread(client);
71 thread.start();
72 thread.join(1000);
73 Assertions.assertFalse(thread.isAlive());
74
75 ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
76 for (int i = 0; i < 10; i++) {
77 Assertions.assertEquals(TEST_EVENT + i, ois.readObject());
78 }
79 }
80
81 }