aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/cluster/test/ClusterSearcherTestCase.java
blob: 33d86658f8e9de60d0e8a856da4aaa509d99739f (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.cluster.test;

import java.util.ArrayList;
import java.util.List;

import com.yahoo.component.ComponentId;

import static org.junit.jupiter.api.Assertions.assertEquals;
import com.yahoo.prelude.Ping;
import com.yahoo.prelude.Pong;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import com.yahoo.search.cluster.ClusterSearcher;
import com.yahoo.search.cluster.Hasher;
import com.yahoo.search.cluster.PingableSearcher;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;
import com.yahoo.search.searchchain.Execution;
import org.junit.jupiter.api.Test;

public class ClusterSearcherTestCase {

    class TestingBackendSearcher extends PingableSearcher {

        Hit hit;

        public TestingBackendSearcher(Hit hit) {
            this.hit = hit;
        }

        @Override
        public Result search(Query query,Execution execution) {
            Result result = execution.search(query);
            result.hits().add(hit);
            return result;
        }
    }

    class BlockingBackendSearcher extends TestingBackendSearcher {

        private boolean blocking = false;

        public BlockingBackendSearcher(Hit hit) {
            super(hit);
        }

         public Result search(Query query,Execution execution) {
             Result result = super.search(query,execution);
             if (isBlocking()) {
                 result.hits().addError(ErrorMessage.createUnspecifiedError("Dummy error"));
             }
             return result;
         }

        @Override
        public Pong ping(Ping ping, Execution execution) {
            if (isBlocking())
                return new Pong(ErrorMessage.createTimeout("Dummy timeout"));
            else
                return new Pong();
        }

        public boolean isBlocking() {
            return blocking;
        }

        public void setBlocking(boolean blocking) {
            this.blocking = blocking;
        }
    }

    class SimpleQuery extends Query {
        int hashValue;
        public SimpleQuery(int hashValue) {
            this.hashValue = hashValue;
        }

        @Override
        public int hashCode() {
            return hashValue;
        }
    }

    class SimpleHasher<T> extends Hasher<T> {


        class SimpleNodeList extends NodeList<T> {
            public SimpleNodeList() {
                super(null);
            }

            public T select(int code, int trynum) {
                return objects.get(code + trynum % objects.size());
            }

            public int getNodeCount() {
                return objects.size();
            }
        }

        List<T> objects = new ArrayList<>();

        @Override
        public synchronized void remove(T node) {
            objects.remove(node);
        }

        @Override
        public synchronized void add(T node) {
            objects.add(node);
        }

        @Override
        public NodeList<T> getNodes() {
            return new SimpleNodeList();

        }
    }

    /** A cluster searcher which clusters over a set of alternative searchers (search chains would be more realistic) */
    static class SearcherClusterSearcher extends ClusterSearcher<Searcher> {

        public SearcherClusterSearcher(ComponentId id,List<Searcher> searchers,Hasher<Searcher> hasher) {
            super(id,searchers,hasher,false);
        }

        @Override
        public Result search(Query query,Execution execution,Searcher searcher) {
            return searcher.search(query,execution);
        }

        @Override
        public void fill(Result result,String summaryName,Execution execution,Searcher searcher) {
            searcher.fill(result,summaryName,execution);
        }

        @Override
        public Pong ping(Ping ping,Searcher searcher) {
            return new Execution(searcher, Execution.Context.createContextStub()).ping(ping);
        }

    }

    @Test
    void testSimple() {
        Hit blockingHit = new Hit("blocking");
        Hit nonblockingHit = new Hit("nonblocking");
        BlockingBackendSearcher blockingSearcher  = new BlockingBackendSearcher(blockingHit);
        List<Searcher> searchers = new ArrayList<>();
        searchers.add(blockingSearcher);
        searchers.add(new TestingBackendSearcher(nonblockingHit));
        ClusterSearcher<?> provider = new SearcherClusterSearcher(new ComponentId("simple"), searchers, new SimpleHasher<>());

        Result blockingResult = new Execution(provider, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals(blockingHit, blockingResult.hits().get(0));
        Result nonblockingResult = new Execution(provider, Execution.Context.createContextStub()).search(new SimpleQuery(1));
        assertEquals(nonblockingHit, nonblockingResult.hits().get(0));

        blockingSearcher.setBlocking(true);

        blockingResult = new Execution(provider, Execution.Context.createContextStub()).search(new SimpleQuery(0));
        assertEquals(blockingResult.hits().get(0), nonblockingHit);
        nonblockingResult = new Execution(provider, Execution.Context.createContextStub()).search(new SimpleQuery(1));
        assertEquals(nonblockingResult.hits().get(0), nonblockingHit);
    }

}