aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/TraceTripTestCase.java
blob: e8d2fd72a47d1e43dd1fbbf0cdb46bc027c6c3ea (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
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus;

import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.network.rpc.test.TestServer;
import com.yahoo.messagebus.routing.RoutingTableSpec;
import com.yahoo.messagebus.test.Receptor;
import com.yahoo.messagebus.test.SimpleMessage;
import com.yahoo.messagebus.test.SimpleProtocol;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.net.UnknownHostException;
import java.util.Arrays;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

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

    Slobrok slobrok;
    TestServer src;
    TestServer pxy;
    TestServer dst;

    @BeforeEach
    public void setUp() throws ListenFailedException {
        RoutingTableSpec table = new RoutingTableSpec(SimpleProtocol.NAME)
                                 .addHop("pxy", "test/pxy/session", Arrays.asList("test/pxy/session"))
                                 .addHop("dst", "test/dst/session", Arrays.asList("test/dst/session"))
                                 .addRoute("test", Arrays.asList("pxy", "dst"));

        slobrok = new Slobrok();
        src = new TestServer("test/src", table, slobrok, null);
        pxy = new TestServer("test/pxy", table, slobrok, null);
        dst = new TestServer("test/dst", table, slobrok, null);
    }

    @AfterEach
    public void tearDown() {
        dst.destroy();
        pxy.destroy();
        src.destroy();
        slobrok.stop();
    }

    @Test
    void testTrip() {
        Receptor src_rr = new Receptor();
        SourceSession src_s = src.mb.createSourceSession(src_rr);

        new Proxy(pxy.mb);
        assertTrue(src.waitSlobrok("test/pxy/session", 1));

        new Server(dst.mb);
        assertTrue(src.waitSlobrok("test/dst/session", 1));
        assertTrue(pxy.waitSlobrok("test/dst/session", 1));

        Message msg = new SimpleMessage("");
        msg.getTrace().setLevel(1);
        msg.getTrace().trace(1, "Client message", false);
        src_s.send(msg, "test");
        Reply reply = src_rr.getReply(60);
        reply.getTrace().trace(1, "Client reply", false);
        assertEquals(reply.getNumErrors(), 0);

        TraceNode t = new TraceNode()
                .addChild("Client message")
                .addChild("Proxy message")
                .addChild("Server message")
                .addChild("Server reply")
                .addChild("Proxy reply")
                .addChild("Client reply");
        System.out.println("reply: " + reply.getTrace().getRoot().encode());
        System.out.println("want : " + t.encode());
        assertEquals(reply.getTrace().getRoot().encode(), t.encode());
    }

    private static class Proxy implements MessageHandler, ReplyHandler {
        private IntermediateSession session;

        public Proxy(MessageBus bus) {
            session = bus.createIntermediateSession("session", true, this, this);
        }

        public void handleMessage(Message msg) {
            msg.getTrace().trace(1, "Proxy message", false);
            System.out.println(msg.getTrace().getRoot().encode());
            session.forward(msg);
        }

        public void handleReply(Reply reply) {
            reply.getTrace().trace(1, "Proxy reply", false);
            System.out.println(reply.getTrace().getRoot().encode());
            session.forward(reply);
        }
    }

    private static class Server implements MessageHandler {
        private DestinationSession session;

        public Server(MessageBus bus) {
            session = bus.createDestinationSession("session", true, this);
        }

        public void handleMessage(Message msg) {
            msg.getTrace().trace(1, "Server message", false);
            System.out.println(msg.getTrace().getRoot().encode());
            Reply reply = new EmptyReply();
            msg.swapState(reply);
            reply.getTrace().trace(1, "Server reply", false);
            System.out.println(reply.getTrace().getRoot().encode());
            session.reply(reply);
        }
    }

}