aboutsummaryrefslogtreecommitdiffstats
path: root/vespa_feed_perf/src/test/java/com/yahoo/vespa/feed/perf/SimpleFeederTest.java
blob: 25c56a5cc574ab7d64a827d360e1f1533ec24df8 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.feed.perf;

import com.yahoo.document.serialization.DeserializationException;
import com.yahoo.documentapi.messagebus.protocol.DocumentMessage;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.messagebus.DynamicThrottlePolicy;
import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.ErrorCode;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.MessageHandler;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.SourceSession;
import com.yahoo.messagebus.StaticThrottlePolicy;
import com.yahoo.messagebus.ThrottlePolicy;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.lang.reflect.Field;
import java.nio.charset.StandardCharsets;
import java.util.regex.Pattern;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

/**
 * @author Simon Thoresen Hult
 */
public class SimpleFeederTest {

    private static final String CONFIG_DIR = "target/test-classes/";

    @Test
    public void requireThatXMLFeederWorks() throws Throwable {
        assertFeed("<vespafeed>" +
                   "    <document documenttype='simple' documentid='doc:scheme:0'>" +
                   "        <my_str>foo</my_str>" +
                   "    </document>" +
                   "    <update documenttype='simple' documentid='doc:scheme:1'>" +
                   "        <assign field='my_str'>bar</assign>" +
                   "    </update>" +
                   "    <remove documenttype='simple' documentid='doc:scheme:2'/>" +
                   "</vespafeed>",
                   new MessageHandler() {

                       @Override
                       public void handleMessage(Message msg) {
                           Reply reply = ((DocumentMessage)msg).createReply();
                           reply.swapState(msg);
                           reply.popHandler().handleReply(reply);
                       }
                   },
                   "",
                   "(.+\n)+" +
                   "\\s*\\d+,\\s*3,.+\n");
    }

    @Test
    public void requireThatJsonFeederWorks() throws Throwable {
        assertFeed("[" +
                        "  { \"put\": \"id:simple:simple::0\", \"fields\": { \"my_str\":\"foo\"}}," +
                        "  { \"update\": \"id:simple:simple::1\", \"fields\": { \"my_str\": { \"assign\":\"bar\"}}}," +
                        "  { \"remove\": \"id:simple:simple::2\"}" +
                        "]",
                new MessageHandler() {

                    @Override
                    public void handleMessage(Message msg) {
                        Reply reply = ((DocumentMessage)msg).createReply();
                        reply.swapState(msg);
                        reply.popHandler().handleReply(reply);
                    }
                },
                "",
                "(.+\n)+" +
                        "\\s*\\d+,\\s*3,.+\n");
    }

    @Test
    public void requireThatParseFailuresThrowInMainThread() throws Throwable {
        TestDriver driver = new TestDriver(new FeederParams(),
                                           "<vespafeed>" +
                                           "    <document documenttype='unknown' documentid='doc:scheme:0'/>" +
                                           "</vespafeed>",
                                           null);
        try {
            driver.run();
            fail();
        } catch (DeserializationException e) {
            assertEquals("Field 'doc:scheme:0': Must specify an existing document type, not 'unknown' (at line 1, column 76)",
                         e.getMessage());
        }
        assertTrue(driver.close());
    }

    @Test
    public void requireThatSyncFailuresThrowInMainThread() throws Throwable {
        TestDriver driver = new TestDriver(new FeederParams(),
                                           "<vespafeed>" +
                                           "    <document documenttype='simple' documentid='doc:scheme:0'/>" +
                                           "</vespafeed>",
                                           null);
        getSourceSession(driver).close();
        try {
            driver.run();
            fail();
        } catch (IOException e) {
            assertEquals("[SEND_QUEUE_CLOSED @ localhost]: Source session is closed.", e.getMessage());
        }
        assertTrue(driver.close());
    }

    @Test
    public void requireThatAsyncFailuresThrowInMainThread() throws Throwable {
        TestDriver driver = new TestDriver(new FeederParams(),
                                           "<vespafeed><document documenttype='simple' documentid='doc:scheme:0'/></vespafeed>",
                                           new MessageHandler() {

                                               @Override
                                               public void handleMessage(Message msg) {
                                                   Reply reply = new EmptyReply();
                                                   reply.swapState(msg);
                                                   reply.addError(new Error(ErrorCode.APP_FATAL_ERROR + 6, "foo"));
                                                   reply.addError(new Error(ErrorCode.APP_FATAL_ERROR + 9, "bar"));
                                                   reply.popHandler().handleReply(reply);
                                               }
                                           });
        try {
            driver.run();
            fail();
        } catch (IOException e) {
            assertMatches("com.yahoo.documentapi.messagebus.protocol.PutDocumentMessage@.+\n" +
                          "\\[UNKNOWN\\(250006\\) @ .+\\]: foo\n" +
                          "\\[UNKNOWN\\(250009\\) @ .+\\]: bar\n",
                          e.getMessage());
        }
        assertTrue(driver.close());
    }

    @Test
    public void requireThatDynamicThrottlingIsDefault() throws Exception {
        TestDriver driver = new TestDriver(new FeederParams(), "", null);
        assertEquals(DynamicThrottlePolicy.class, getThrottlePolicy(driver).getClass());
        assertTrue(driver.close());
    }

    @Test
    public void requireThatSerialTransferModeConfiguresStaticThrottling() throws Exception {
        TestDriver driver = new TestDriver(new FeederParams().setSerialTransfer(true), "", null);
        assertEquals(StaticThrottlePolicy.class, getThrottlePolicy(driver).getClass());
        assertTrue(driver.close());
    }

    private static SourceSession getSourceSession(TestDriver driver) {
        return (SourceSession)getField(driver.feeder, "session");
    }

    private static ThrottlePolicy getThrottlePolicy(TestDriver driver) {
        return (ThrottlePolicy)getField(getSourceSession(driver), "throttlePolicy");
    }

    private static Object getField(Object obj, String fieldName) {
        try {
            Field field = obj.getClass().getDeclaredField(fieldName);
            field.setAccessible(true);
            return field.get(obj);
        } catch (IllegalAccessException | NoSuchFieldException e) {
            throw new AssertionError(e);
        }
    }

    private static void assertFeed(String in, MessageHandler validator, String expectedErr, String expectedOut)
            throws Throwable {
        TestDriver driver = new TestDriver(new FeederParams(), in, validator);
        driver.run();
        assertMatches(expectedErr, new String(driver.err.toByteArray(), StandardCharsets.UTF_8));
        assertMatches(expectedOut, new String(driver.out.toByteArray(), StandardCharsets.UTF_8));
        assertTrue(driver.close());
    }

    private static void assertMatches(String expected, String actual) {
        if (!Pattern.matches(expected, actual)) {
            assertEquals(expected, actual);
        }
    }

    private static class TestDriver {

        final ByteArrayOutputStream err = new ByteArrayOutputStream();
        final ByteArrayOutputStream out = new ByteArrayOutputStream();
        final SimpleFeeder feeder;
        final SimpleServer server;

        TestDriver(FeederParams params, String in, MessageHandler validator)
                throws IOException, ListenFailedException {
            server = new SimpleServer(CONFIG_DIR, validator);
            feeder = new SimpleFeeder(params.setConfigId("dir:" + CONFIG_DIR)
                                            .setStdErr(new PrintStream(err))
                                            .setStdIn(new ByteArrayInputStream(in.getBytes(StandardCharsets.UTF_8)))
                                            .setStdOut(new PrintStream(out)));
        }

        void run() throws Throwable {
            feeder.run();
        }

        boolean close() {
            feeder.close();
            server.close();
            return true;
        }
    }

}