aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-feed-client/src/test/java/ai/vespa/feed/client/JsonFeederTest.java
blob: 6c26e1d8ae837b3f7112d47d6f5afcbfba060dde (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
// Copyright Verizon Media. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package ai.vespa.feed.client;

import org.junit.jupiter.api.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import java.util.stream.IntStream;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.stream.Collectors.joining;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;

class JsonFeederTest {

    @Test
    void test() throws IOException {
        int docs = 1 << 14;
        String json = "[\n" +

                      IntStream.range(0, docs).mapToObj(i ->
                                                                "  {\n" +
                                                                "    \"id\": \"id:ns:type::abc" + i + "\",\n" +
                                                                "    \"fields\": {\n" +
                                                                "      \"lul\":\"lal\"\n" +
                                                                "    }\n" +
                                                                "  },\n"
                      ).collect(joining()) +

                      "  {\n" +
                      "    \"id\": \"id:ns:type::abc" + docs + "\",\n" +
                      "    \"fields\": {\n" +
                      "      \"lul\":\"lal\"\n" +
                      "    }\n" +
                      "  }\n" +
                      "]";
        AtomicReference<FeedException> exceptionThrow = new AtomicReference<>();
        Path tmpFile = Files.createTempFile(null, null);
        Files.write(tmpFile, json.getBytes(UTF_8));
        try (InputStream in = Files.newInputStream(tmpFile, StandardOpenOption.READ, StandardOpenOption.DELETE_ON_CLOSE)) {
            AtomicInteger resultsReceived = new AtomicInteger();
            AtomicBoolean completedSuccessfully = new AtomicBoolean();
            long startNanos = System.nanoTime();
            SimpleClient feedClient = new SimpleClient();
            JsonFeeder.builder(feedClient).build()
                    .feedMany(in, 1 << 7,
                            new JsonFeeder.ResultCallback() { // TODO: hangs when buffer is smaller than largest document
                                @Override
                                public void onNextResult(Result result, FeedException error) { resultsReceived.incrementAndGet(); }

                                @Override
                                public void onError(FeedException error) { exceptionThrow.set(error); }

                                @Override
                                public void onComplete() { completedSuccessfully.set(true); }
                            })
                    .join();

            System.err.println((json.length() / 1048576.0) + " MB in " + (System.nanoTime() - startNanos) * 1e-9 + " seconds");
            assertEquals(docs + 1, feedClient.ids.size());
            assertEquals(docs + 1, resultsReceived.get());
            assertTrue(completedSuccessfully.get());
            assertNull(exceptionThrow.get());
        }
    }

    @Test
    public void multipleJsonArrayOperationsAreDispatchedToFeedClient() throws IOException, ExecutionException, InterruptedException {
        SimpleClient client = new SimpleClient();
        try (JsonFeeder feeder = JsonFeeder.builder(client).build()) {
            String json = "[{" +
                          "  \"put\": \"id:ns:type::abc1\",\n" +
                          "  \"fields\": {\n" +
                          "    \"lul\":\"lal\"\n" +
                          "  }\n" +
                          "},\n" +
                          "{" +
                          "  \"put\": \"id:ns:type::abc2\",\n" +
                          "  \"fields\": {\n" +
                          "    \"lul\":\"lal\"\n" +
                          "  }\n" +
                          "}]\n";
            feeder.feedMany(new ByteArrayInputStream(json.getBytes(UTF_8))).get();
            assertEquals(new HashSet<>(Arrays.asList("abc1", "abc2")), client.ids);
        }
    }

    @Test
    public void multipleJsonLOperationsAreDispatchedToFeedClient() throws IOException, ExecutionException, InterruptedException {
        SimpleClient client = new SimpleClient();
        try (JsonFeeder feeder = JsonFeeder.builder(client).build()) {
            String json = "{" +
                          "  \"put\": \"id:ns:type::abc1\",\n" +
                          "  \"fields\": {\n" +
                          "    \"lul\":\"lal\"\n" +
                          "  }\n" +
                          "}\n" +
                          "{" +
                          "  \"put\": \"id:ns:type::abc2\",\n" +
                          "  \"fields\": {\n" +
                          "    \"lul\":\"lal\"\n" +
                          "  }\n" +
                          "}\n";
            feeder.feedMany(new ByteArrayInputStream(json.getBytes(UTF_8))).get();
            assertEquals(new HashSet<>(Arrays.asList("abc1", "abc2")), client.ids);
        }
    }

    @Test
    public void singleJsonOperationIsDispatchedToFeedClient() throws IOException, ExecutionException, InterruptedException {
        try (JsonFeeder feeder = JsonFeeder.builder(new SimpleClient()).build()) {
            String json = "{\"put\": \"id:ns:type::abc1\",\n" +
                    "    \"fields\": {\n" +
                    "      \"lul\":\"lal\"\n" +
                    "    }\n" +
                    "  }\n";
            Result result = feeder.feedSingle(json).get();
            assertEquals(DocumentId.of("id:ns:type::abc1"), result.documentId());
            assertEquals(Result.Type.success, result.type());
            assertEquals("success", result.resultMessage().get());
        }
    }

    private static class SimpleClient implements FeedClient {
        final Set<String> ids = new HashSet<>();

        @Override
        public CompletableFuture<Result> put(DocumentId documentId, String documentJson, OperationParameters params) {
            ids.add(documentId.userSpecific());
            return createSuccessResult(documentId);
        }

        @Override
        public CompletableFuture<Result> update(DocumentId documentId, String updateJson, OperationParameters params) {
            return createSuccessResult(documentId);
        }

        @Override
        public CompletableFuture<Result> remove(DocumentId documentId, OperationParameters params) {
            return createSuccessResult(documentId);
        }

        @Override
        public OperationStats stats() { return null; }

        @Override
        public void close(boolean graceful) { }

        private CompletableFuture<Result> createSuccessResult(DocumentId documentId) {
            return CompletableFuture.completedFuture(new Result(Result.Type.success, documentId, "success", null));
        }
    }

}