aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/prelude/fastsearch/FS4SearchInvokerTestCase.java
blob: b91195284907308e9f7770c1eb00b92e99ad6e54 (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
// Copyright 2019 Oath Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

package com.yahoo.prelude.fastsearch;

import com.yahoo.fs4.BasicPacket;
import com.yahoo.fs4.mplex.FS4Channel;
import com.yahoo.fs4.mplex.InvalidChannelException;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.dispatch.InterleavedSearchInvoker;
import com.yahoo.search.dispatch.MockSearchCluster;
import com.yahoo.search.dispatch.ResponseMonitor;
import com.yahoo.search.searchchain.Execution;
import org.hamcrest.Matchers;
import org.junit.Test;

import java.io.IOException;
import java.util.Collections;
import java.util.Optional;

import static org.junit.Assert.assertThat;

public class FS4SearchInvokerTestCase {
    @SuppressWarnings("resource")
    @Test
    public void testThatConnectionErrorsAreReportedImmediately() throws IOException {
        var query = new Query("?");
        query.setTimeout(1000);

        var searcher = mockSearcher();
        var cluster = new MockSearchCluster("?", 1, 1);
        var fs4invoker = new FS4SearchInvoker(searcher, query, mockFailingChannel(), Optional.empty());
        var interleave = new InterleavedSearchInvoker(Collections.singleton(fs4invoker), cluster, null);

        long start = System.currentTimeMillis();
        interleave.search(query, null);
        long elapsed = System.currentTimeMillis() - start;

        assertThat("Connection error should fail fast", elapsed, Matchers.lessThan(500L));
    }

    private static VespaBackEndSearcher mockSearcher() {
        return new VespaBackEndSearcher() {
            @Override
            protected Result doSearch2(Query query, Execution execution) {
                return null;
            }

            @Override
            protected void doPartialFill(Result result, String summaryClass) {}
        };
    }

    private static FS4Channel mockFailingChannel() {
        return new FS4Channel() {
            @Override
            public boolean sendPacket(BasicPacket packet) throws InvalidChannelException, IOException {
                // pretend there's a connection error
                return false;
            }

            @Override
            public void setQuery(Query q) {}

            @Override
            public void setResponseMonitor(ResponseMonitor<FS4Channel> monitor) {}

            @Override
            public void close() {}
        };
    }
}