summaryrefslogtreecommitdiffstats
path: root/vespa_feed_perf/src/test/java/com/yahoo/vespa/feed/perf/FeederParamsTest.java
blob: ab1eb27e416fe5509f36d1926db249df7b1b915c (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
// 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.messagebus.routing.Route;
import org.apache.commons.cli.ParseException;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

/**
 * @author Simon Thoresen Hult
 */
public class FeederParamsTest {
    static final String TESTFILE_JSON = "test.json";
    static final String TESTFILE_VESPA = "test.vespa";
    static final String TESTFILE_UNKNOWN = "test.xyz";

    @Test
    public void requireThatAccessorsWork() {
        FeederParams params = new FeederParams();

        InputStream stdIn = new ByteArrayInputStream(new byte[1]);
        params.setStdIn(stdIn);
        assertSame(stdIn, params.getStdIn());

        PrintStream stdErr = new PrintStream(new ByteArrayOutputStream());
        params.setStdErr(stdErr);
        assertSame(stdErr, params.getStdErr());

        PrintStream stdOut = new PrintStream(new ByteArrayOutputStream());
        params.setStdOut(stdOut);
        assertSame(stdOut, params.getStdOut());

        params.setConfigId("my_config_id");
        assertEquals("my_config_id", params.getConfigId());

        params.setSerialTransfer(false);
        assertFalse(params.isSerialTransferEnabled());
        params.setSerialTransfer(true);
        assertTrue(params.isSerialTransferEnabled());
    }

    @Test
    public void requireThatParamsHaveReasonableDefaults() {
        FeederParams params = new FeederParams();
        assertSame(System.in, params.getStdIn());
        assertSame(System.err, params.getStdErr());
        assertSame(System.out, params.getStdOut());
        assertEquals(Route.parse("default"), params.getRoute());
        assertEquals("client", params.getConfigId());
        assertFalse(params.isSerialTransferEnabled());
    }

    @Test
    public void requireThatSerialTransferOptionIsParsed() throws ParseException, FileNotFoundException {
        assertTrue(new FeederParams().parseArgs("-s").isSerialTransferEnabled());
        assertTrue(new FeederParams().parseArgs("foo", "-s").isSerialTransferEnabled());
        assertTrue(new FeederParams().parseArgs("-s", "foo").isSerialTransferEnabled());
        assertTrue(new FeederParams().parseArgs("--serial").isSerialTransferEnabled());
        assertTrue(new FeederParams().parseArgs("foo", "--serial").isSerialTransferEnabled());
        assertTrue(new FeederParams().parseArgs("--serial", "foo").isSerialTransferEnabled());
    }

    @Test
    public void requireThatArgumentsAreParsedAsRoute() throws ParseException, FileNotFoundException {
        assertEquals(Route.parse("foo bar"), new FeederParams().parseArgs("-r foo bar").getRoute());
        assertEquals(Route.parse("foo bar"), new FeederParams().parseArgs("--route","foo bar").getRoute());
    }

    @Test
    public void requireThatRouteIsAnOptionalArgument() throws ParseException, FileNotFoundException {
        assertEquals(Route.parse("default"), new FeederParams().parseArgs().getRoute());
        assertEquals(Route.parse("default"), new FeederParams().parseArgs("-s").getRoute());
    }

    @Test
    public void requireThatNumThreadsAreParsed() throws ParseException, FileNotFoundException {
        assertEquals(1, new FeederParams().getNumDispatchThreads());
        assertEquals(17, new FeederParams().parseArgs("-n 17").getNumDispatchThreads());
    }

    @Test
    public void requireThatDumpStreamAreParsed() throws ParseException, IOException {
        assertNull(new FeederParams().getDumpStream());

        FeederParams p = new FeederParams().parseArgs("-o " + TESTFILE_JSON);
        assertNotNull(p.getDumpStream());
        assertEquals(FeederParams.DumpFormat.JSON, p.getDumpFormat());
        p.getDumpStream().close();

        p = new FeederParams().parseArgs("-o " + TESTFILE_VESPA);
        assertNotNull(p.getDumpStream());
        assertEquals(FeederParams.DumpFormat.VESPA, p.getDumpFormat());
        p.getDumpStream().close();

        p = new FeederParams().parseArgs("-o " + TESTFILE_UNKNOWN);
        assertNotNull(p.getDumpStream());
        assertEquals(FeederParams.DumpFormat.JSON, p.getDumpFormat());
        p.getDumpStream().close();

        assertTrue(new File(TESTFILE_JSON).delete());
        assertTrue(new File(TESTFILE_VESPA).delete());
        assertTrue(new File(TESTFILE_UNKNOWN).delete());
    }

}