aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/fs4/mplex/BackendTestCase.java
blob: 8696ca08d2b50c1defc1df2fc1bb90f4a57e02f2 (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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.fs4.mplex;

import com.yahoo.container.QrConfig;
import com.yahoo.container.search.Fs4Config;
import com.yahoo.fs4.BasicPacket;
import com.yahoo.fs4.ChannelTimeoutException;
import com.yahoo.fs4.PingPacket;
import com.yahoo.fs4.QueryPacket;
import com.yahoo.fs4.mplex.Backend.BackendStatistics;
import com.yahoo.prelude.fastsearch.FS4ResourcePool;
import com.yahoo.search.Query;
import org.junit.Test;

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.ByteBuffer;
import java.util.logging.Logger;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * Test networking code for talking to dispatch.
 *
 * @author Steinar Knutsen
 */
public class BackendTestCase {
    private static final long TIMEOUT = 30000;

    public static class MockDispatch implements Runnable {

        public final ServerSocket socket;
        public volatile Socket connection;
        volatile int channelId;

        public byte[] packetData = new byte[] {0,0,0,104,
                0,0,0,217-256,
                0,0,0,1,
                0,0,0,0,
                0,0,0,2,
                0,0,0,0,0,0,0,5,
                0x40,0x39,0,0,0,0,0,0,
                0,0,0,111,
                0,0,0,97,
                0,0,0,3, 1,1,1,1,1,1,1,1,1,1,1,1, 0x40,0x37,0,0,0,0,0,23, 0,0,0,7, 0,0,0,36,
                0,0,0,4, 2,2,2,2,2,2,2,2,2,2,2,2, 0x40,0x35,0,0,0,0,0,21, 0,0,0,8, 0,0,0,37};

        MockDispatch(ServerSocket socket) {
            this.socket = socket;
        }

        @Override
        public void run() {
            try {
                connection = socket.accept();
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            requestRespond();
        }

        void requestRespond() {
            byte[] length = new byte[4];
            try {
                connection.getInputStream().read(length);
            } catch (IOException e) {
                e.printStackTrace();
                return;
            }
            int actual = ByteBuffer.wrap(length).getInt();

            int read = 0;
            int i = 0;
            while (read != -1 && i < actual) {
                try {
                    read = connection.getInputStream().read();
                    ++i;
                } catch (IOException e) {
                    e.printStackTrace();
                    return;
                }
            }
            ByteBuffer reply = ByteBuffer.wrap(packetData);
            if (channelId != -1) {
                reply.putInt(8, channelId);
            }
            try {
                connection.getOutputStream().write(packetData);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        public void setNoChannel() {
            channelId = -1;
        }

    }

    public static class MockServer {
        public InetSocketAddress host;
        public Thread worker;
        public MockDispatch dispatch;

        public MockServer() throws IOException {
            ServerSocket socket = new ServerSocket(0, 50, InetAddress.getLoopbackAddress());
            host = (InetSocketAddress) socket.getLocalSocketAddress();
            dispatch = new MockDispatch(socket);
            worker = new Thread(dispatch);
            worker.start();
        }

    }

    private Backend backend;
    private MockServer server;
    private Logger logger;
    private boolean initUseParent;
    private FS4ResourcePool listeners;

    public static final byte[] PONG = new byte[] { 0, 0, 0, 32, 0, 0, 0, 221 - 256,
                                                   0,0,0,1, 0, 0, 0, 42, 0, 0, 0, 127, 0, 0, 0, 2, 0, 0, 0, 2, 0, 0, 0, 1, 0,
                                                   0, 0, 1 };

    public void setUp() throws Exception {
        logger = Logger.getLogger(Backend.class.getName());
        initUseParent = logger.getUseParentHandlers();
        logger.setUseParentHandlers(false);
        listeners = new FS4ResourcePool(new Fs4Config(new Fs4Config.Builder()), new QrConfig(new QrConfig.Builder()));

        server = new MockServer();
        backend = listeners.getBackend(server.host.getHostString(), server.host.getPort());
    }

    public void tearDown() throws Exception {
        listeners.deconstruct();
        server.dispatch.socket.close();
        if (server.dispatch.connection !=null) server.dispatch.connection.close();
        if (server.worker!=null) server.worker.join();
        if (logger !=null) logger.setUseParentHandlers(initUseParent);
    }

    private BasicPacket [] read(FS4Channel channel) throws InvalidChannelException {
        try {
            return channel.receivePackets(TIMEOUT, 1);
        } catch (ChannelTimeoutException e) {
            fail("Could not get packets from simulated backend.");
        }
        return new BasicPacket[1];
    }

    @Test
    public void testAll() throws Exception {
        setUp();
        doTestBackend();
        tearDown();

        setUp();
        doTestPinging();
        tearDown();

        setUp();
        doRequireStatistics();
        tearDown();
    }

    private void doTestBackend() throws IOException, InvalidChannelException {
        FS4Channel channel = backend.openChannel();
        Query q = new Query("/?query=a");
        int channelId = channel.getChannelId();
        server.dispatch.channelId = channelId;

        assertTrue(backend.sendPacket(QueryPacket.create("container.0", q), channelId));
        BasicPacket[] b = read(channel);
        assertEquals(1, b.length);
        assertEquals(217, b[0].getCode());
        channel.close();
    }

    private void doTestPinging() throws IOException, InvalidChannelException {
        FS4Channel channel = backend.openPingChannel();
        server.dispatch.setNoChannel();
        server.dispatch.packetData = PONG;

        assertTrue(channel.sendPacket(new PingPacket()));
        BasicPacket[] b = read(channel);
        assertEquals(1, b.length);
        assertEquals(221, b[0].getCode());
        channel.close();
    }

    private void doRequireStatistics() throws IOException, InvalidChannelException {
        FS4Channel channel = backend.openPingChannel();
        server.dispatch.channelId = -1;
        server.dispatch.packetData = PONG;

        assertTrue(channel.sendPacket(new PingPacket()));
        read(channel);
        BackendStatistics stats = backend.getStatistics();
        assertEquals(1, stats.totalConnections());
    }

}