aboutsummaryrefslogtreecommitdiffstats
path: root/jrt/tests/com/yahoo/jrt/BackTargetTest.java
blob: 05a131f332df5bfab87bf043dba02d5908c4589b (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
// 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 BackTargetTest {

    Supervisor server;
    Acceptor   acceptor;
    Supervisor client;
    Target     target;
    int        serverValue;
    int        clientValue;
    Target     serverBackTarget;
    Target     clientBackTarget;

    @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("inc", "", "", this::server_inc));
        server.addMethod(new Method("sample_target", "", "", this::server_sample_target));
        server.addMethod(new Method("back_inc", "", "", this::back_inc));

        client.addMethod(new Method("inc", "", "", this::client_inc));
        client.addMethod(new Method("sample_target", "", "", this::client_sample_target));
        client.addMethod(new Method("back_inc", "", "", this::back_inc));

        serverValue = 0;
        clientValue = 0;
        serverBackTarget = null;
        clientBackTarget = null;
    }

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

    private void server_inc(Request req) {
        serverValue++;
    }

    private void server_sample_target(Request req) {
        serverBackTarget = req.target();
    }

    private void client_inc(Request req) {
        clientValue++;
    }

    private void client_sample_target(Request req) {
        clientBackTarget = req.target();
    }

    private void back_inc(Request req) {
        Target t = req.target();
        t.invokeVoid(new Request("inc"));
    }

    private void checkValues(int server, int client) {
        assertEquals(server, serverValue);
        assertEquals(client, clientValue);
    }

    private void checkTargets(boolean server, boolean client) {
        assertTrue(server == (serverBackTarget != null));
        assertTrue(client == (clientBackTarget != null));
    }

    @org.junit.Test
    public void testBackTarget() {
        checkTargets(false, false);
        target.invokeSync(new Request("sample_target"), Duration.ofSeconds(5));
        checkTargets(true, false);
        serverBackTarget.invokeSync(new Request("sample_target"), Duration.ofSeconds(5));
        checkTargets(true, true);

        checkValues(0, 0);
        target.invokeSync(new Request("inc"), Duration.ofSeconds(5));
        checkValues(1, 0);
        serverBackTarget.invokeSync(new Request("inc"), Duration.ofSeconds(5));
        checkValues(1, 1);
        clientBackTarget.invokeSync(new Request("inc"), Duration.ofSeconds(5));
        checkValues(2, 1);

        target.invokeSync(new Request("back_inc"), Duration.ofSeconds(5));
        checkValues(2, 2);
        serverBackTarget.invokeSync(new Request("back_inc"), Duration.ofSeconds(5));
        checkValues(3, 2);
        clientBackTarget.invokeSync(new Request("back_inc"), Duration.ofSeconds(5));
        checkValues(3, 3);
    }

    @org.junit.Test
    public void testBogusBackTarget() {
        Request req = new Request("inc");
        try {
            req.target();
            assertTrue(false);
        } catch (IllegalStateException e) {}
    }

}