aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/rendering/AsyncGroupPopulationTestCase.java
blob: 359aed85d30a87205d9f7454edb93e472542f5d7 (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 static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

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

import org.junit.Test;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.util.concurrent.ListenableFuture;
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;

/**
 * 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> implements ListenableFuture<F> {
        Receiver<Boolean> isListening = new Receiver<>();

        private ListenableFuture<F> wrapped;

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

        public void addListener(Runnable listener, Executor executor) {
            wrapped.addListener(listener, executor);
            isListening.put(Boolean.TRUE);
        }

        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> {
        WrappedFuture<DataList<DATATYPE>> waitForIt = null;
        private final Object lock = new Object();

        @Override
        public ListenableFuture<DataList<DATATYPE>> completed() {
            synchronized (lock) {
                if (waitForIt == null) {
                    waitForIt = new WrappedFuture<>(super.completed());
                }
            }
            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
    public final void test() throws InterruptedException, ExecutionException,
            JsonParseException, JsonMappingException, 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();
        ListenableFuture<Boolean> f = renderer.render(out, result,
                new Execution(Execution.Context.createContextStub()),
                result.getQuery());
        WrappedFuture<DataList<Hit>> x = (WrappedFuture<DataList<Hit>>) h.incoming().completed();
        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);
    }

}