View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
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   * Unit tests for {@link RemoteReceiverStreamClient}.
28   *
29   * @author Carl Harris
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          // MockEventQueue will interrupt the thread when the queue is drained
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  }