summaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/EchoTest.java
blob: 5acd8221a9b51961b6867a70f39b2b362641ed6a (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jrt;


import org.junit.After;
import org.junit.Before;

import static org.junit.Assert.assertTrue;

public class EchoTest {

    Supervisor server;
    Acceptor   acceptor;
    Supervisor client;
    Target     target;
    Values     refValues;

    @Before
    public void setUp() throws ListenFailedException {
        server   = new Supervisor(new Transport());
        client   = new Supervisor(new Transport());
        acceptor = server.listen(new Spec(0));
        target   = client.connect(new Spec("localhost", acceptor.port()));
        server.addMethod(new Method("echo", "*", "*", this, "rpc_echo"));
        refValues = new Values();
        byte[]   dataValue   = { 1, 2, 3, 4 };
        byte[]   int8Array   = { 1, 2, 3, 4 };
        short[]  int16Array  = { 2, 4, 6, 8 };
        int[]    int32Array  = { 4, 8, 12, 16 };
        long[]   int64Array  = { 8, 16, 24, 32 };
        float[]  floatArray  = { 1.5f, 2.0f, 2.5f, 3.0f };
        double[] doubleArray = { 1.25, 1.50, 1.75, 2.00 };
        byte[][] dataArray   = {{ 1, 0, 1, 0 },
                                { 0, 2, 0, 2 },
                                { 3, 0, 3, 0 },
                                { 0, 4, 0, 4 }};
        String[] stringArray = { "one", "two", "three", "four" };
        refValues.add(new Int8Value((byte)1));
        refValues.add(new Int8Array(int8Array));
        refValues.add(new Int16Value((short)2));
        refValues.add(new Int16Array(int16Array));
        refValues.add(new Int32Value(4));
        refValues.add(new Int32Array(int32Array));
        refValues.add(new Int64Value(8));
        refValues.add(new Int64Array(int64Array));
        refValues.add(new FloatValue(2.5f));
        refValues.add(new FloatArray(floatArray));
        refValues.add(new DoubleValue(3.75));
        refValues.add(new DoubleArray(doubleArray));
        refValues.add(new DataValue(dataValue));
        refValues.add(new DataArray(dataArray));
        refValues.add(new StringValue("test"));
        refValues.add(new StringArray(stringArray));
    }

    @After
    public void tearDown() {
        target.close();
        acceptor.shutdown().join();
        client.transport().shutdown().join();
        server.transport().shutdown().join();
    }

    public void rpc_echo(Request req) {
        if (!Test.equals(req.parameters(), refValues)) {
            System.err.println("Parameters does not match reference values");
            req.setError(ErrorCode.METHOD_FAILED, "parameter mismatch");
            return;
        }
        Values p = req.parameters();
        Values r = req.returnValues();
        for (int i = 0; i < p.size(); i++) {
            r.add(p.get(i));
        }
    }

    @org.junit.Test
    public void testEcho() {
        Request req = new Request("echo");
        Values p = req.parameters();
        for (int i = 0; i < refValues.size(); i++) {
            p.add(refValues.get(i));
        }
        target.invokeSync(req, 60.0);
        assertTrue(req.checkReturnTypes("bBhHiIlLfFdDxXsS"));
        assertTrue(Test.equals(req.returnValues(), req.parameters()));
        assertTrue(Test.equals(req.returnValues(), refValues));
        assertTrue(Test.equals(req.parameters(), refValues));
    }

}