summaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/test/IntegrationTestCase.java
blob: 0a3e7bda318991eb6955835960fe86baae1391d9 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.prelude.test;

import com.yahoo.search.result.Hit;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.Test;

/**
 * Runs a query thru the configured search chain from a real http channel
 * to a mock fdispatch channel. The setup is rather complicated, as the point is
 * to span as much of the processing from query to result as possible.
 *
 * @author bratseth
 */
public class IntegrationTestCase {

    public static class SecondSearcher extends Searcher {
        public Result search(com.yahoo.search.Query query, Execution execution) {
            Result result = execution.search(query);
            result.hits().add(new Hit("searcher:2",1000));
            return result;
        }
    }
    public static class ThirdSearcher extends Searcher {
        public Result search(com.yahoo.search.Query query, Execution execution) {
            Result result = execution.search(query);
            result.hits().add(new Hit("searcher:3",1000));
            return result;
        }
    }

    @Test
    void testQuery() throws java.io.IOException {
        /*
       TODO: (JSB) This blocks forever on Linux (not Windows) because
             the ServerSocketChannel.accept method in Server
         seems to starve the test running thread,
         causing it to get stuck in waitForServerInitialization.
         This must be caused by starvation because
         replacing the test with Thread.sleep(n)
         gives the same result if n is large enough (2000
         is large enough, 1000 is not.
         Resolve this in some way, perhaps by switching to
         non-blocking io (and then remember to remove the next line).
     */
    }

    /*
        if (1==1) return;
        ServerThread serverThread=new ServerThread();
        try {
            serverThread.start();
            waitForServerInitialization();
            insertMockFs4Channel();
            ByteBuffer buffer=ByteBuffer.allocate(4096);
            buffer.put(getBytes("GET /?query=hans HTTP/1.1\n\n"));
            SocketChannel socket=
                SocketChannel.open(new InetSocketAddress(Server.get().getHost(),
                                                         Server.get().getPort()));
            buffer.flip();
            socket.write(buffer);

            buffer.clear();
            socket.read(buffer);
            // TODO: Validate return too

        }
        finally {
            serverThread.interrupt();
        }
    }

    private static void assertCorrectQueryData(QueryPacket packet) {
        assertEquals("Query x packet " +
                     "[query: query 'RANK hans bcatpat.bidxpatlvl1:hans' [path='/']]",
                     packet.toString());
    }

    private void insertMockFs4Channel() {
        Searcher current=SearchChain.get();
        while (current.getChained().getChained()!=null)
            current=current.getChained();
        assertTrue(current.getChained() instanceof FastSearcher);
        FastSearcher mockFastSearcher=
            new FastSearcher(new MockFSChannel(),
                            "file:etc/qr-summary.cf",
                             "testhittype");
        current.setChained(mockFastSearcher);
    }

    private void waitForServerInitialization() {
        int sleptMs=0;
        while (Server.get().getHost()==null) {
            try { Thread.sleep(10); } catch (Exception e) {}
            sleptMs+=10;
        }
    }

    private class ServerThread extends Thread {

        public void run() {
            try {
                Server.get().start(8081,new SearchRequestHandler());
            }
            catch (java.io.IOException e) {
                throw new RuntimeException("Failed",e);
            }
        }
    }

    private byte[] getBytes(String string) {
        try {
            return string.getBytes("utf-8");
        }
        catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException("Won't happen",e);
        }
    }
    */
    /** A channel which returns hardcoded packets of the same type as fdispatch */
    /*
    private static class MockFSChannel extends Channel {

        public MockFSChannel() {}

        public void sendPacket(Packet packet) {
            if (packet instanceof QueryPacket) {
                assertCorrectQueryData((QueryPacket)packet);
            }
            else {
                throw new RuntimeException("Mock channel don't know what to reply to " +
                                           packet);
            }
        }

        public Packet[] receivePackets() {
            List packets=new java.util.ArrayList();
            QueryResultPacket result=QueryResultPacket.create();
            result.addDocument(new DocumentInfo(123,2003,234,1000,1));
            result.addDocument(new DocumentInfo(456,1855,234,1001,1));
            packets.add(result);
            addDocsums(packets);
            return (Packet[])packets.toArray(new Packet[packets.size()]);
        }

        private void addDocsums(List packets) {
            ByteBuffer buffer=createDocsumPacketData(DocsumDefinitionTestCase.docsum4);
            buffer.position(0);
            packets.add(PacketDecoder.decode(buffer));

            buffer=createDocsumPacketData(DocsumDefinitionTestCase.docsum4);
            buffer.position(0);
            packets.add(PacketDecoder.decode(buffer));

            packets.add(EolPacket.create());
        }

        private ByteBuffer createDocsumPacketData(byte[] docsumData) {
            ByteBuffer buffer=ByteBuffer.allocate(docsumData.length+12+4);
            buffer.limit(buffer.capacity());
            buffer.position(0);
            buffer.putInt(docsumData.length+8+4);
            buffer.putInt(205); // Docsum packet code
            buffer.putInt(0);
            buffer.putInt(0);
            buffer.put(docsumData);
            return buffer;
        }

    }
    */
}