aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/InvokeVoidTest.java
blob: bcd188a2bc1cc0747eec16e0f9d5a09991f4a03b (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
// Copyright Vespa.ai. 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 java.time.Duration;

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

public class InvokeVoidTest {

    Test.Orb server;
    Acceptor acceptor;
    Test.Orb client;
    Target   target;

    @Before
    public void setUp() throws ListenFailedException {
        server   = new Test.Orb(new Transport());
        client   = new Test.Orb(new Transport());
        acceptor = server.listen(new Spec(0));
        target   = client.connect(new Spec("localhost", acceptor.port()));

        server.addMethod(new Method("set", "i", "", this::rpc_set)
                         .methodDesc("Set the stored value")
                         .paramDesc(0, "value", "the new value"));
        server.addMethod(new Method("inc", "", "", this::rpc_inc)
                         .methodDesc("Increase the stored value"));
        server.addMethod(new Method("get", "", "i", this::rpc_get)
                         .methodDesc("Get the stored value")
                         .returnDesc(0, "value", "the stored value"));
    }

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

    private int value = 0;

    private void rpc_set(Request req) {
        value = req.parameters().get(0).asInt32();
    }
    private void rpc_inc(Request req) {
        value++;
    }
    private void rpc_get(Request req) {
        req.returnValues().add(new Int32Value(value));
    }

    @org.junit.Test
    public void testInvokeVoid() {
        Request req = new Request("set");
        req.parameters().add(new Int32Value(40));
        target.invokeSync(req, Duration.ofSeconds(5));
        assertTrue(!req.isError());
        assertEquals(0, req.returnValues().size());

        target.invokeVoid(new Request("inc"));
        target.invokeVoid(new Request("inc"));

        req = new Request("get");
        target.invokeSync(req, Duration.ofSeconds(5));
        assertTrue(!req.isError());
        assertEquals(42, req.returnValues().get(0).asInt32());

        assertTrue(server.checkReadCounts(4, 0, 0));
        assertTrue(server.checkWriteCounts(0, 2, 0));
        assertTrue(client.checkReadCounts(0, 2, 0));
        assertTrue(client.checkWriteCounts(4, 0, 0));
        assertTrue(server.readBytes == client.writeBytes);
        assertTrue(client.readBytes == server.writeBytes);
    }

}