aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/rendering/EventRendererTestCase.java
blob: 2cfb6552379db19a6a89a343d76718c891ee1b97 (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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package com.yahoo.search.rendering;

import com.yahoo.search.result.EventStream;
import com.yahoo.concurrent.ThreadFactoryFactory;
import com.yahoo.document.DocumentId;
import com.yahoo.search.Query;
import com.yahoo.search.Result;
import com.yahoo.search.result.ErrorMessage;
import com.yahoo.search.result.Hit;
import com.yahoo.search.result.HitGroup;
import com.yahoo.search.searchchain.Execution;
import com.yahoo.text.Utf8;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;

import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;

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

public class EventRendererTestCase {

    private static ThreadPoolExecutor executor;
    private static EventRenderer blueprint;
    private EventRenderer renderer;

    @BeforeAll
    public static void createExecutorAndBlueprint() {
        ThreadFactory threadFactory = ThreadFactoryFactory.getThreadFactory("test-rendering");
        executor = new ThreadPoolExecutor(4, 4, 1L, TimeUnit.MINUTES, new LinkedBlockingQueue<>(), threadFactory);
        executor.prestartAllCoreThreads();
        blueprint = new EventRenderer(executor);
    }

    @BeforeEach
    public void createClone() {
        // Use the shared renderer as a prototype object, as specified in the API contract
        renderer = (EventRenderer) blueprint.clone();
        renderer.init();
    }

    @AfterEach
    public void deconstructClone() {
        if (renderer != null) {
            renderer.deconstruct();
            renderer = null;
        }
    }

    @AfterAll
    public static void deconstructBlueprintAndExecutor() throws InterruptedException {
        blueprint.deconstruct();
        blueprint = null;
        executor.shutdown();
        if (!executor.awaitTermination(1, TimeUnit.MINUTES)) {
            throw new RuntimeException("Failed to shutdown executor");
        }
        executor = null;
    }

    @Test
    @Timeout(5)
    public void testRendering() throws InterruptedException, ExecutionException {
        var expected = """
                event: token
                data: {"token":"Ducks"}
                
                event: token
                data: {"token":" have"}
                
                event: token
                data: {"token":" adorable"}
                
                event: token
                data: {"token":" waddling"}
                
                event: token
                data: {"token":" walks"}
                
                event: end
                """;
        var tokenStream = new EventStream();
        for (String token : splitter("Ducks have adorable waddling walks")) {
            tokenStream.add(token);
        }
        tokenStream.markComplete();
        var result = render(new Result(new Query(), newHitGroup(tokenStream, "token_stream")));
        assertEquals(expected, result);
    }

    @Test
    @Timeout(5)
    public void testAsyncRendering() throws InterruptedException, ExecutionException {
        var expected = """
                event: token
                data: {"token":"Ducks"}
                
                event: token
                data: {"token":" have"}
                
                event: token
                data: {"token":" adorable"}
                
                event: token
                data: {"token":" waddling"}
                
                event: token
                data: {"token":" walks"}
                
                event: end
                """;
        var result = "";
        var executor = Executors.newFixedThreadPool(1);
        try {
            var tokenStream = new EventStream();
            var future = completeAsync("Ducks have adorable waddling walks", executor, token -> {
                tokenStream.add(token);
            }).exceptionally(e -> {
                tokenStream.error("error", new ErrorMessage(400, e.getMessage()));
                tokenStream.markComplete();
                return false;
            }).thenAccept(finishReason -> {
                tokenStream.markComplete();
            });
            assertFalse(future.isDone());
            result = render(new Result(new Query(), newHitGroup(tokenStream, "token_stream")));
            future.join();  // Renderer waits for async completion

        } finally {
            executor.shutdownNow();
        }
        assertEquals(expected, result);
    }

    @Test
    public void testErrorEndsStream() throws ExecutionException, InterruptedException {
        var tokenStream = new EventStream();
        tokenStream.add("token1");
        tokenStream.add("token2");
        tokenStream.error("my_llm", new ErrorMessage(400, "Something went wrong"));
        tokenStream.markComplete();
        var result = render(new Result(new Query(), newHitGroup(tokenStream, "token_stream")));
        var expected = """
                event: token
                data: {"token":"token1"}

                event: token
                data: {"token":"token2"}

                event: error
                data: {"source":"my_llm","error":400,"message":"Something went wrong"}

                event: end
                """;
        assertEquals(expected, result);
    }

    @Test
    public void testPromptRendering() throws ExecutionException, InterruptedException {
        String prompt = "Why are ducks better than cats?\n\nBe concise.\n";

        var tokenStream = new EventStream();
        tokenStream.add(prompt, "prompt");
        tokenStream.add("Just");
        tokenStream.add(" because");
        tokenStream.add(".");
        tokenStream.markComplete();
        var result = render(new Result(new Query(), newHitGroup(tokenStream, "token_stream")));

        var expected = """
                event: prompt
                data: {"prompt":"Why are ducks better than cats?\\n\\nBe concise.\\n"}
                
                event: token
                data: {"token":"Just"}

                event: token
                data: {"token":" because"}

                event: token
                data: {"token":"."}

                event: end
                """;
        assertEquals(expected, result);
    }

    @Test
    @Timeout(5)
    public void testResultRenderingIsSkipped() throws ExecutionException, InterruptedException {
        var tokenStream = new EventStream();
        tokenStream.add("token1");
        tokenStream.add("token2");
        tokenStream.markComplete();

        var resultsHitGroup = new HitGroup("test_results");
        var hit1 = new Hit("result_1");
        var hit2 = new Hit("result_2");
        hit1.setField("documentid", new DocumentId("id:unittest:test::1"));
        hit2.setField("documentid", new DocumentId("id:unittest:test::2"));
        resultsHitGroup.add(hit1);
        resultsHitGroup.add(hit2);

        var combined = new HitGroup("all");
        combined.add(resultsHitGroup);
        combined.add(newHitGroup(tokenStream, "token_stream"));

        var result = render(new Result(new Query(), combined));
        var expected = """
                event: token
                data: {"token":"token1"}

                event: token
                data: {"token":"token2"}

                event: end
                """;
        assertEquals(expected, result);  // Todo: support other types of data such as search results (hits), timing and trace
    }

    static HitGroup newHitGroup(EventStream eventStream, String id) {
        var hitGroup = new HitGroup(id);
        hitGroup.add(eventStream);
        return hitGroup;
    }

    private String render(Result r) throws InterruptedException, ExecutionException {
        var execution = new Execution(Execution.Context.createContextStub());
        return render(execution, r);
    }

    private String render(Execution execution, Result r) throws InterruptedException, ExecutionException {
        if (renderer == null) createClone();
        try {
            ByteArrayOutputStream bs = new ByteArrayOutputStream();  //new DebugOutputStream();
            CompletableFuture<Boolean> f = renderer.renderResponse(bs, r, execution, null);
            assertTrue(f.get());
            return Utf8.toString(bs.toByteArray());
        } finally {
            deconstructClone();
        }
    }

    private static class DebugOutputStream extends ByteArrayOutputStream {
        @Override
        public synchronized void write(byte[] b, int off, int len) {
            super.write(b, off, len);
            System.out.print(new String(b, off, len));
        }
    }

    private static List<String> splitter(String text) {
        var list = new ArrayList<String>();
        for (String token : text.split(" ")) {
            list.add(list.isEmpty() ? token : " " + token);
        }
        return list;
    }

    private static CompletableFuture<Boolean> completeAsync(String text, ExecutorService executor, Consumer<String> consumer) {
        var completionFuture = new CompletableFuture<Boolean>();
        executor.submit(() -> {
            try {
                for (String s : splitter(text)) {
                    consumer.accept(s);
                    Thread.sleep(10);
                }
                completionFuture.complete(true);
            } catch (Exception e) {
                completionFuture.completeExceptionally(e);
            }
        });
        return completionFuture;
    }

}