aboutsummaryrefslogtreecommitdiffstats
path: root/messagebus/src/test/java/com/yahoo/messagebus/routing/RoutingContextTestCase.java
blob: 37d0bbd4c92eb1d12accd1ac7d1cf6942995989b (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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.routing;

import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.*;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.network.Identity;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;
import com.yahoo.messagebus.network.rpc.test.TestServer;
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.util.Arrays;
import java.util.List;

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

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

    public static final int TIMEOUT_SECS = 120;

    Slobrok slobrok;
    TestServer srcServer, dstServer;
    SourceSession srcSession;
    DestinationSession dstSession;

    @BeforeEach
    public void setUp() throws ListenFailedException {
        slobrok = new Slobrok();
        dstServer = new TestServer(new MessageBusParams().addProtocol(new SimpleProtocol()),
                                   new RPCNetworkParams().setIdentity(new Identity("dst")).setSlobrokConfigId(TestServer.getSlobrokConfig(slobrok)));
        dstSession = dstServer.mb.createDestinationSession(new DestinationSessionParams().setName("session").setMessageHandler(new Receptor()));
        srcServer = new TestServer(new MessageBusParams().setRetryPolicy(new RetryTransientErrorsPolicy().setBaseDelay(0)).addProtocol(new SimpleProtocol()),
                                   new RPCNetworkParams().setSlobrokConfigId(TestServer.getSlobrokConfig(slobrok)));
        srcSession = srcServer.mb.createSourceSession(
                new SourceSessionParams().setTimeout(600.0).setReplyHandler(new Receptor()));
        assertTrue(srcServer.waitSlobrok("dst/session", 1));
    }

    @AfterEach
    public void tearDown() {
        slobrok.stop();
        dstSession.destroy();
        dstServer.destroy();
        srcSession.destroy();
        srcServer.destroy();
    }

    @Test
    void testSingleDirective() {
        SimpleProtocol protocol = new SimpleProtocol();
        protocol.addPolicyFactory("Custom", new CustomPolicyFactory(
                false,
                Arrays.asList("foo", "bar", "baz/cox"),
                Arrays.asList("foo", "bar")));
        srcServer.mb.putProtocol(protocol);
        srcServer.setupRouting(new RoutingTableSpec(SimpleProtocol.NAME)
                .addRoute(new RouteSpec("myroute").addHop("myhop"))
                .addHop(new HopSpec("myhop", "[Custom]")
                        .addRecipient("foo").addRecipient("bar").addRecipient("baz/cox")));
        for (int i = 0; i < 2; ++i) {
            assertTrue(srcSession.send(createMessage("msg"), "myroute").isAccepted());
            Reply reply = ((Receptor) srcSession.getReplyHandler()).getReply(TIMEOUT_SECS);
            assertNotNull(reply);
            System.out.println(reply.getTrace());
            assertFalse(reply.hasErrors());
        }
    }

    @Test
    void testMoreDirectives() {
        SimpleProtocol protocol = new SimpleProtocol();
        protocol.addPolicyFactory("Custom", new CustomPolicyFactory(
                false,
                Arrays.asList("foo", "foo/bar", "foo/bar0/baz", "foo/bar1/baz", "foo/bar/baz/cox"),
                Arrays.asList("foo/bar0/baz", "foo/bar1/baz")));
        srcServer.mb.putProtocol(protocol);
        srcServer.setupRouting(new RoutingTableSpec(SimpleProtocol.NAME)
                .addRoute(new RouteSpec("myroute").addHop("myhop"))
                .addHop(new HopSpec("myhop", "foo/[Custom]/baz")
                        .addRecipient("foo").addRecipient("foo/bar")
                        .addRecipient("foo/bar0/baz").addRecipient("foo/bar1/baz")
                        .addRecipient("foo/bar/baz/cox")));
        for (int i = 0; i < 2; ++i) {
            assertTrue(srcSession.send(createMessage("msg"), "myroute").isAccepted());
            Reply reply = ((Receptor) srcSession.getReplyHandler()).getReply(TIMEOUT_SECS);
            assertNotNull(reply);
            assertFalse(reply.hasErrors());
        }
    }

    @Test
    void testRecipientsRemain() {
        SimpleProtocol protocol = new SimpleProtocol();
        protocol.addPolicyFactory("First", new CustomPolicyFactory(true, List.of("foo/bar"), List.of("foo/[Second]")));
        protocol.addPolicyFactory("Second", new CustomPolicyFactory(false, List.of("foo/bar"), List.of("foo/bar")));
        srcServer.mb.putProtocol(protocol);
        srcServer.setupRouting(new RoutingTableSpec(SimpleProtocol.NAME)
                .addRoute(new RouteSpec("myroute").addHop("myhop"))
                .addHop(new HopSpec("myhop", "[First]/[Second]")
                        .addRecipient("foo/bar")));
        for (int i = 0; i < 2; ++i) {
            assertTrue(srcSession.send(createMessage("msg"), "myroute").isAccepted());
            Reply reply = ((Receptor) srcSession.getReplyHandler()).getReply(TIMEOUT_SECS);
            assertNotNull(reply);
            assertFalse(reply.hasErrors());
        }
    }

    @Test
    void testToString() {
        assertEquals("node : null, directive: 1, errors: [], selectOnRetry: true context: null", new RoutingContext(null, 1).toString());
    }

    @Test
    void testConstRoute() {
        SimpleProtocol protocol = new SimpleProtocol();
        protocol.addPolicyFactory("DocumentRouteSelector",
                new CustomPolicyFactory(true, List.of("dst"), List.of("dst")));
        srcServer.mb.putProtocol(protocol);
        srcServer.setupRouting(new RoutingTableSpec(SimpleProtocol.NAME)
                .addRoute(new RouteSpec("default").addHop("indexing"))
                .addHop(new HopSpec("indexing", "[DocumentRouteSelector]").addRecipient("dst"))
                .addHop(new HopSpec("dst", "dst/session")));
        for (int i = 0; i < 2; ++i) {
            assertTrue(srcSession.send(createMessage("msg"), Route.parse("route:default")).isAccepted());
            Message msg = ((Receptor) dstSession.getMessageHandler()).getMessage(TIMEOUT_SECS);
            assertNotNull(msg);
            dstSession.acknowledge(msg);
            Reply reply = ((Receptor) srcSession.getReplyHandler()).getReply(TIMEOUT_SECS);
            assertNotNull(reply);
            assertFalse(reply.hasErrors());
        }
    }

    private Message createMessage(String msg) {
        Message ret = new SimpleMessage(msg);
        ret.getTrace().setLevel(9);
        return ret;
    }

    private static class CustomPolicyFactory implements SimpleProtocol.PolicyFactory {

        final boolean forward;
        final List<String> expectedAll;
        final List<String> expectedMatched;

        public CustomPolicyFactory(boolean forward, List<String> all, List<String> matched) {
            this.forward = forward;
            this.expectedAll = all;
            this.expectedMatched = matched;
        }

        public RoutingPolicy create(String param) {
            return new CustomPolicy(this);
        }
    }

    private static class CustomPolicy implements RoutingPolicy {

        CustomPolicyFactory factory;

        public CustomPolicy(CustomPolicyFactory factory) {
            this.factory = factory;
        }

        public void select(RoutingContext ctx) {
            Reply reply = new EmptyReply();
            reply.getTrace().setLevel(9);

            List<Route> recipients = ctx.getAllRecipients();
            if (factory.expectedAll.size() == recipients.size()) {
                ctx.trace(1, "Got " + recipients.size() + " expected recipients.");
                for (Route route : recipients) {
                    if (factory.expectedAll.contains(route.toString())) {
                        ctx.trace(1, "Got expected recipient '" + route + "'.");
                    } else {
                        reply.addError(new Error(ErrorCode.APP_FATAL_ERROR,
                                                 "Recipient '" + route + "' not expected."));
                    }
                }
            } else {
                reply.addError(new Error(ErrorCode.APP_FATAL_ERROR,
                                         "Expected " + factory.expectedAll.size() + " recipients, got " + recipients.size() + "."));
            }

            if (ctx.getNumRecipients() == recipients.size()) {
                for (int i = 0; i < recipients.size(); ++i) {
                    if (recipients.get(i) == ctx.getRecipient(i)) {
                        ctx.trace(1, "getRecipient(" + i + ") matches getAllRecipients().get(" + i + ")");
                    } else {
                        reply.addError(new Error(ErrorCode.APP_FATAL_ERROR,
                                                 "getRecipient(" + i + ") differs from getAllRecipients().get(" + i + ")"));
                    }
                }
            } else {
                reply.addError(new Error(ErrorCode.APP_FATAL_ERROR,
                                         "getNumRecipients() differs from getAllRecipients().size()"));
            }

            recipients = ctx.getMatchedRecipients();
            if (factory.expectedMatched.size() == recipients.size()) {
                ctx.trace(1, "Got " + recipients.size() + " matched recipients.");
                for (Route route : recipients) {
                    if (factory.expectedMatched.contains(route.toString())) {
                        ctx.trace(1, "Got matched recipient '" + route + "'.");
                    } else {
                        reply.addError(new Error(ErrorCode.APP_FATAL_ERROR,
                                                 "Matched recipient '" + route + "' not expected."));
                    }
                }
            } else {
                reply.addError(new Error(ErrorCode.APP_FATAL_ERROR,
                                         "Expected " + factory.expectedAll.size() + " matched recipients, got " + recipients.size() + "."));
            }

            if (!reply.hasErrors() && factory.forward) {
                for (Route route : recipients) {
                    ctx.addChild(route);
                }
            } else {
                ctx.setReply(reply);
            }
        }

        public void merge(RoutingContext ctx) {
            Reply ret = new EmptyReply();
            for (RoutingNodeIterator it = ctx.getChildIterator();
                 it.isValid(); it.next()) {
                Reply reply = it.getReplyRef();
                for (int i = 0; i < reply.getNumErrors(); ++i) {
                    ret.addError(reply.getError(i));
                }
            }
            ctx.setReply(ret);
        }

        @Override
        public void destroy() {
        }
    }

}