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

import com.fasterxml.jackson.databind.ObjectMapper;
import com.yahoo.concurrent.Receiver;
import com.yahoo.processing.response.Data;
import com.yahoo.processing.response.DataList;
import com.yahoo.processing.response.DefaultIncomingData;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.search.result.Relevance;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.text.Utf8;
import org.junit.jupiter.api.Test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.Executor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.function.BiConsumer;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

/**
 * Test adding hits to a hit group during rendering.
 *
 * @author <a href="mailto:steinar@yahoo-inc.com">Steinar Knutsen</a>
 */
public class AsyncGroupPopulationTestCase {
    private static class WrappedFuture<F> extends CompletableFuture<F> {
        Receiver<Boolean> isListening = new Receiver<>();

        private final CompletableFuture<F> wrapped;

        WrappedFuture(CompletableFuture<F> wrapped) {
            this.wrapped = wrapped;
        }

        @Override
        public CompletableFuture<F> whenCompleteAsync(BiConsumer<? super F, ? super Throwable> action, Executor executor) {
            wrapped.whenCompleteAsync(action);
            isListening.put(Boolean.TRUE);
            return this;
        }

        public boolean cancel(boolean mayInterruptIfRunning) {
            return wrapped.cancel(mayInterruptIfRunning);
        }

        public boolean isCancelled() {
            return wrapped.isCancelled();
        }

        public boolean isDone() {
            return wrapped.isDone();
        }

        public F get() throws InterruptedException, ExecutionException {
            return wrapped.get();
        }

        public F get(long timeout, TimeUnit unit) throws InterruptedException,
                ExecutionException, TimeoutException {
            return wrapped.get(timeout, unit);
        }
    }

    private static class ObservableIncoming<DATATYPE extends Data> extends DefaultIncomingData<DATATYPE> {
        volatile WrappedFuture<DataList<DATATYPE>> waitForIt = null;
        private final Object lock = new Object();

        @Override
        public CompletableFuture<DataList<DATATYPE>> completedFuture() {
            synchronized (lock) {
                if (waitForIt == null) {
                    waitForIt = new WrappedFuture<>(super.completedFuture());
                }
            }
            return waitForIt;
        }
    }

    private static class InstrumentedGroup extends HitGroup {
        private static final long serialVersionUID = 4585896586414935558L;

        InstrumentedGroup(String id) {
            super(id, new Relevance(1), new ObservableIncoming<Hit>());
            ((ObservableIncoming<Hit>) incoming()).assignOwner(this);
        }

    }

    @Test
    final void test() throws InterruptedException, ExecutionException,
            IOException {
        String rawExpected = "{"
                + "    \"root\": {"
                + "        \"children\": ["
                + "            {"
                + "                \"id\": \"yahoo1\","
                + "                \"relevance\": 1.0"
                + "            },"
                + "            {"
                + "                \"id\": \"yahoo2\","
                + "                \"relevance\": 1.0"
                + "            }"
                + "        ],"
                + "        \"fields\": {"
                + "            \"totalCount\": 0"
                + "        },"
                + "        \"id\": \"yahoo\","
                + "        \"relevance\": 1.0"
                + "    }"
                + "}";
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        HitGroup h = new InstrumentedGroup("yahoo");
        h.incoming().add(new Hit("yahoo1"));
        JsonRenderer renderer = new JsonRenderer();
        Result result = new Result(new Query(), h);
        renderer.init();
        CompletableFuture<Boolean> f = renderer.renderResponse(out, result,
                new Execution(Execution.Context.createContextStub()),
                result.getQuery());
        WrappedFuture<DataList<Hit>> x = (WrappedFuture<DataList<Hit>>) h.incoming().completedFuture();
        x.isListening.get(86_400_000);
        h.incoming().add(new Hit("yahoo2"));
        h.incoming().markComplete();
        Boolean b = f.get();
        assertTrue(b);
        String rawGot = Utf8.toString(out.toByteArray());
        ObjectMapper m = new ObjectMapper();
        Map<?, ?> expected = m.readValue(rawExpected, Map.class);
        Map<?, ?> got = m.readValue(rawGot, Map.class);
        assertEquals(expected, got);
    }

}