aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/network/local/LocalNetworkTest.java
blob: 60618744c83de0fd3f68bee260cb39bdb2d4f56f (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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.network.local;

import com.yahoo.concurrent.SystemTimer;
import com.yahoo.messagebus.DestinationSession;
import com.yahoo.messagebus.DestinationSessionParams;
import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.ErrorCode;
import com.yahoo.messagebus.IntermediateSession;
import com.yahoo.messagebus.IntermediateSessionParams;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.MessageBus;
import com.yahoo.messagebus.MessageBusParams;
import com.yahoo.messagebus.MessageHandler;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.ReplyHandler;
import com.yahoo.messagebus.Result;
import com.yahoo.messagebus.SourceSession;
import com.yahoo.messagebus.SourceSessionParams;
import com.yahoo.messagebus.StaticThrottlePolicy;
import com.yahoo.messagebus.ThrottlePolicy;
import com.yahoo.messagebus.routing.Hop;
import com.yahoo.messagebus.routing.Route;
import com.yahoo.messagebus.test.SimpleMessage;
import com.yahoo.messagebus.test.SimpleProtocol;
import com.yahoo.messagebus.test.SimpleReply;
import org.junit.jupiter.api.Test;

import java.util.concurrent.BlockingDeque;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;

import static org.junit.jupiter.api.Assertions.*;

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

    @Test
    void requireThatLocalNetworkCanSendAndReceive() throws InterruptedException {
        final LocalWire wire = new LocalWire();

        final Server serverA = new Server(wire);
        final SourceSession source = serverA.newSourceSession();

        final Server serverB = new Server(wire);
        final IntermediateSession intermediate = serverB.newIntermediateSession();

        final Server serverC = new Server(wire);
        final DestinationSession destination = serverC.newDestinationSession();

        Message msg = new SimpleMessage("foo");
        msg.setRoute(new Route().addHop(Hop.parse(intermediate.getConnectionSpec()))
                .addHop(Hop.parse(destination.getConnectionSpec())));
        assertTrue(source.send(msg).isAccepted());

        msg = serverB.messages.poll(60, TimeUnit.SECONDS);
        assertTrue(msg instanceof SimpleMessage);
        assertEquals("foo", ((SimpleMessage) msg).getValue());
        intermediate.forward(msg);

        msg = serverC.messages.poll(60, TimeUnit.SECONDS);
        assertTrue(msg instanceof SimpleMessage);
        assertEquals("foo", ((SimpleMessage) msg).getValue());
        Reply reply = new SimpleReply("bar");
        reply.swapState(msg);
        destination.reply(reply);

        reply = serverB.replies.poll(60, TimeUnit.SECONDS);
        assertTrue(reply instanceof SimpleReply);
        assertEquals("bar", ((SimpleReply) reply).getValue());
        intermediate.forward(reply);

        reply = serverA.replies.poll(60, TimeUnit.SECONDS);
        assertTrue(reply instanceof SimpleReply);
        assertEquals("bar", ((SimpleReply) reply).getValue());

        serverA.mbus.destroy();
        serverB.mbus.destroy();
        serverC.mbus.destroy();
    }

    @Test
    void requireThatUnknownServiceRepliesWithNoAddressForService() throws InterruptedException {
        final Server server = new Server(new LocalWire());
        final SourceSession source = server.newSourceSession();

        final Message msg = new SimpleMessage("foo").setRoute(Route.parse("bar"));
        assertTrue(source.send(msg).isAccepted());
        final Reply reply = server.replies.poll(60, TimeUnit.SECONDS);
        assertTrue(reply instanceof EmptyReply);

        server.mbus.destroy();
    }

    @Test
    void requireThatBlockingSendTimeOutInSendQ() throws InterruptedException {
        final LocalWire wire = new LocalWire();

        final Server serverA = new Server(wire);
        final SourceSession source = serverA.newSourceSession(new StaticThrottlePolicy().setMaxPendingCount(1));

        final Server serverB = new Server(wire);
        final IntermediateSession intermediate = serverB.newIntermediateSession();

        final Server serverC = new Server(wire);
        final DestinationSession destination = serverC.newDestinationSession();

        Message msg = new SimpleMessage("foo");
        msg.setRoute(new Route().addHop(Hop.parse(intermediate.getConnectionSpec()))
                .addHop(Hop.parse(destination.getConnectionSpec())));
        assertTrue(source.sendBlocking(msg).isAccepted());
        long start = SystemTimer.INSTANCE.milliTime();
        Message msg2 = new SimpleMessage("foo2");
        msg2.setRoute(new Route().addHop(Hop.parse(intermediate.getConnectionSpec()))
                .addHop(Hop.parse(destination.getConnectionSpec())));
        long TIMEOUT = 1000;
        msg2.setTimeRemaining(TIMEOUT);
        Result res = source.sendBlocking(msg2);
        assertFalse(res.isAccepted());
        assertEquals(ErrorCode.TIMEOUT, res.getError().getCode());
        assertTrue(res.getError().getMessage().endsWith("Timed out in sendQ"));
        long end = SystemTimer.INSTANCE.milliTime();
        assertTrue(end >= start + TIMEOUT);
        assertTrue(end < start + 5 * TIMEOUT);

        msg = serverB.messages.poll(60, TimeUnit.SECONDS);
        assertTrue(msg instanceof SimpleMessage);
        assertEquals("foo", ((SimpleMessage) msg).getValue());
        intermediate.forward(msg);

        msg = serverC.messages.poll(60, TimeUnit.SECONDS);
        assertTrue(msg instanceof SimpleMessage);
        assertEquals("foo", ((SimpleMessage) msg).getValue());
        Reply reply = new SimpleReply("bar");
        reply.swapState(msg);
        destination.reply(reply);

        reply = serverB.replies.poll(60, TimeUnit.SECONDS);
        assertTrue(reply instanceof SimpleReply);
        assertEquals("bar", ((SimpleReply) reply).getValue());
        intermediate.forward(reply);

        reply = serverA.replies.poll(60, TimeUnit.SECONDS);
        assertEquals(ErrorCode.TIMEOUT, reply.getError(0).getCode());
        assertTrue(reply.getError(0).getMessage().endsWith("Timed out in sendQ"));

        reply = serverA.replies.poll(60, TimeUnit.SECONDS);
        assertTrue(reply instanceof SimpleReply);
        assertEquals("bar", ((SimpleReply) reply).getValue());

        serverA.mbus.destroy();
        serverB.mbus.destroy();
        serverC.mbus.destroy();

    }

    private static class Server implements MessageHandler, ReplyHandler {

        final MessageBus mbus;
        final BlockingDeque<Message> messages = new LinkedBlockingDeque<>();
        final BlockingDeque<Reply> replies = new LinkedBlockingDeque<>();

        Server(LocalWire wire) {
            mbus = new MessageBus(new LocalNetwork(wire),
                                  new MessageBusParams().addProtocol(new SimpleProtocol())
                                                        .setRetryPolicy(null));
        }

        SourceSession newSourceSession() {
            return mbus.createSourceSession(new SourceSessionParams()
                    .setTimeout(600.0)
                    .setReplyHandler(this));
        }
        SourceSession newSourceSession(ThrottlePolicy throttlePolicy) {
            return mbus.createSourceSession(new SourceSessionParams()
                    .setTimeout(600.0)
                    .setReplyHandler(this)
                    .setThrottlePolicy(throttlePolicy));
        }
        IntermediateSession newIntermediateSession() {
            return mbus.createIntermediateSession(new IntermediateSessionParams()
                                                          .setMessageHandler(this)
                                                          .setReplyHandler(this));
        }

        DestinationSession newDestinationSession() {
            return mbus.createDestinationSession(new DestinationSessionParams()
                                                         .setMessageHandler(this));
        }

        @Override
        public void handleMessage(Message msg) {
            messages.addLast(msg);
        }

        @Override
        public void handleReply(Reply reply) {
            replies.addLast(reply);
        }
    }
}