blob: 341e55df777d83d36bed1d24cfec02be7b9c40c9 (
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
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.searchchain;
import com.yahoo.component.chain.Chain;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.Searcher;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.junit.Assert.assertEquals;
/**
* Tests async execution of search chains.
*
* @author Peter Thomas
* @author bratseth
*/
public class VespaAsyncSearcherTest {
private ExecutorService executor;
@Before
public void setUp() throws Exception {
executor = Executors.newFixedThreadPool(16);
}
@After
public void tearDown() {
assertEquals(0, executor.shutdownNow().size());
}
private static class FirstSearcher extends Searcher {
@Override
public Result search(Query query, Execution execution) {
int count = 10;
List<FutureResult> futures = new ArrayList<>(count);
for (int i = 0; i < count; i++) {
Query subQuery = query.clone();
FutureResult future = new AsyncExecution(execution).search(subQuery);
futures.add(future);
}
AsyncExecution.waitForAll(futures, 10 * 60 * 1000);
Result combinedResult = new Result(query);
for (FutureResult resultFuture : futures) {
Result result = resultFuture.get();
combinedResult.mergeWith(result);
combinedResult.hits().add(result.hits());
}
return combinedResult;
}
}
private static class SecondSearcher extends Searcher {
@Override
public Result search(Query query, Execution exctn) {
return new Result(query);
}
}
@Test
public void testAsyncExecution() {
Chain<Searcher> chain = new Chain<>(new FirstSearcher(), new SecondSearcher());
Execution execution = new Execution(chain, Execution.Context.createContextStub());
Query query = new Query();
execution.search(query);
}
}
|