001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.net.server;
015
016import static org.junit.Assert.assertEquals;
017import static org.junit.Assert.assertFalse;
018
019import java.io.ByteArrayInputStream;
020import java.io.ByteArrayOutputStream;
021import java.io.ObjectInputStream;
022
023import org.junit.Before;
024import org.junit.Test;
025
026import ch.qos.logback.core.net.mock.MockContext;
027
028/**
029 * Unit tests for {@link RemoteReceiverStreamClient}.
030 *
031 * @author Carl Harris
032 */
033public class RemoteReceiverStreamClientTest {
034
035    private static final String TEST_EVENT = "test event";
036
037    private MockContext context = new MockContext();
038
039    private MockEventQueue queue = new MockEventQueue();
040
041    private ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
042
043    private RemoteReceiverStreamClient client = new RemoteReceiverStreamClient("someId", outputStream);
044
045    @Before
046    public void setUp() throws Exception {
047        client.setContext(context);
048        client.setQueue(queue);
049    }
050
051    @Test
052    public void testOfferEventAndRun() throws Exception {
053        client.offer(TEST_EVENT);
054
055        Thread thread = new Thread(client);
056        thread.start();
057
058        // MockEventQueue will interrupt the thread when the queue is drained
059        thread.join(1000);
060        assertFalse(thread.isAlive());
061
062        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
063        assertEquals(TEST_EVENT, ois.readObject());
064    }
065
066    @Test
067    public void testOfferEventSequenceAndRun() throws Exception {
068        for (int i = 0; i < 10; i++) {
069            client.offer(TEST_EVENT + i);
070        }
071
072        Thread thread = new Thread(client);
073        thread.start();
074        thread.join(1000);
075        assertFalse(thread.isAlive());
076
077        ObjectInputStream ois = new ObjectInputStream(new ByteArrayInputStream(outputStream.toByteArray()));
078        for (int i = 0; i < 10; i++) {
079            assertEquals(TEST_EVENT + i, ois.readObject());
080        }
081    }
082
083}