summaryrefslogtreecommitdiffstats
path: root/vespajlib/src/test/java/com/yahoo/io/ListenerTestCase.java
blob: 173197ae27c7f4cb874f167a85977cab6ad26975 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.io;

import static org.junit.Assert.*;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.nio.channels.SelectionKey;
import java.nio.channels.SocketChannel;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.yahoo.collections.Tuple2;
import com.yahoo.concurrent.Receiver;
import com.yahoo.concurrent.Receiver.MessageState;

/**
 * Test a NIO based Reactor pattern implementation, com.yahoo.io.Listener.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class ListenerTestCase {

    @Before
    public void setUp() throws Exception {
    }

    @After
    public void tearDown() throws Exception {
    }

    Receiver<Byte> r = new Receiver<>();

    private final class MockConnection implements Connection {

        private SocketChannel channel;

        MockConnection(SocketChannel channel, Listener listener) {
            this.channel = channel;
        }

        @Override
        public void write() throws IOException {
        }

        @Override
        public void read() throws IOException {
            ByteBuffer b = ByteBuffer.allocate(1);
            channel.read(b);
            b.flip();
            r.put(b.get());
        }

        @Override
        public void close() throws IOException {
            channel.close();

        }

        @Override
        public void connect() throws IOException {
        }

        @Override
        public int selectOps() {
            return SelectionKey.OP_READ;
        }

        @Override
        public SocketChannel socketChannel() {
            return channel;
        }
    }

    private final class GetConnection implements ConnectionFactory {

        @Override
        public Connection newConnection(SocketChannel channel, Listener listener) {
            return new MockConnection(channel, listener);
        }
    }

    @Test
    public final void testRun() throws IOException, InterruptedException {
        Listener l = new Listener("ListenerTestCase");
        l.listen(new GetConnection(), 0);
        l.start();
        int port = ((InetSocketAddress) l.acceptors.get(0).socket.getLocalAddress()).getPort();
        Socket s = new Socket("127.0.0.1", port);
        final byte expected = 42;
        s.getOutputStream().write(expected);
        s.getOutputStream().flush();
        s.close();
        Tuple2<MessageState, Byte> received = r.get(60 * 1000);
        l.acceptors.get(0).interrupt();
        l.acceptors.get(0).socket.close();
        l.acceptors.get(0).join();
        l.interrupt();
        l.join();
        assertTrue("Test timed out.", received.first == MessageState.VALID);
        assertEquals(expected, received.second.byteValue());
    }

}