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 static org.junit.Assert.assertEquals;
17  import static org.junit.Assert.assertFalse;
18  
19  import java.io.ByteArrayInputStream;
20  import java.io.ByteArrayOutputStream;
21  import java.io.ObjectInputStream;
22  
23  import org.junit.Before;
24  import org.junit.Test;
25  
26  import ch.qos.logback.core.net.mock.MockContext;
27  
28  /**
29   * Unit tests for {@link RemoteReceiverStreamClient}.
30   *
31   * @author Carl Harris
32   */
33  public class RemoteReceiverStreamClientTest {
34  
35      private static final String TEST_EVENT = "test event";
36  
37      private MockContext context = new MockContext();
38  
39      private MockEventQueue queue = new MockEventQueue();
40  
41      private ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
42  
43      private RemoteReceiverStreamClient client = new RemoteReceiverStreamClient("someId", outputStream);
44  
45      @Before
46      public void setUp() throws Exception {
47          client.setContext(context);
48          client.setQueue(queue);
49      }
50  
51      @Test
52      public void testOfferEventAndRun() throws Exception {
53          client.offer(TEST_EVENT);
54  
55          Thread thread = new Thread(client);
56          thread.start();
57  
58          // MockEventQueue will interrupt the thread when the queue is drained
59          thread.join(1000);
60          assertFalse(thread.isAlive());
61  
62          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
63          assertEquals(TEST_EVENT, ois.readObject());
64      }
65  
66      @Test
67      public void testOfferEventSequenceAndRun() throws Exception {
68          for (int i = 0; i < 10; i++) {
69              client.offer(TEST_EVENT + i);
70          }
71  
72          Thread thread = new Thread(client);
73          thread.start();
74          thread.join(1000);
75          assertFalse(thread.isAlive());
76  
77          ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
78          for (int i = 0; i < 10; i++) {
79              assertEquals(TEST_EVENT + i, ois.readObject());
80          }
81      }
82  
83  }