aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/PolicyTestFrame.java
blob: a067261ba1acdf32da08600e3aa986104e43c592 (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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.documentapi.messagebus.protocol.test;

import com.yahoo.document.DocumentTypeManager;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.slobrok.api.Mirror;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.ErrorCode;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.MessageBus;
import com.yahoo.messagebus.MessageBusParams;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.network.Identity;
import com.yahoo.messagebus.network.Network;
import com.yahoo.messagebus.network.ServiceAddress;
import com.yahoo.messagebus.network.rpc.RPCNetwork;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;
import com.yahoo.messagebus.network.rpc.test.TestServer;
import com.yahoo.messagebus.routing.HopSpec;
import com.yahoo.messagebus.routing.Route;
import com.yahoo.messagebus.routing.RoutingNode;
import com.yahoo.messagebus.routing.RoutingSpec;
import com.yahoo.messagebus.routing.RoutingTableSpec;
import com.yahoo.messagebus.test.Receptor;
import com.yahoo.messagebus.test.SimpleProtocol;
import com.yahoo.messagebus.test.SimpleReply;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

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

/**
 * This is a utility class to allow easier policy test cases. The most important reason to use this is to make sure that
 * each test uses a "clean" mbus and slobrok instance.
 *
 * @author Simon Thoresen Hult
 */
@SuppressWarnings("deprecation")
public class PolicyTestFrame {

    private final AtomicBoolean destroyed = new AtomicBoolean(false);
    private String identity;
    private Slobrok slobrok;
    private MessageBus mbus;
    private MyNetwork net;
    private Message msg = null;
    private HopSpec hop = null;
    private Receptor handler = new Receptor();

    /**
     * Create an anonymous test frame.
     *
     * @param documentMgr The document manager to use.
     */
    public PolicyTestFrame(DocumentTypeManager documentMgr) {
        this("anonymous", documentMgr);
    }

    /**
     * Create a named test frame.
     *
     * @param identity    The identity to use for the server.
     * @param documentMgr The document manager to use.
     */
    public PolicyTestFrame(String identity, DocumentTypeManager documentMgr) {
        this.identity = identity;
        try {
            slobrok = new Slobrok();
        } catch (ListenFailedException e) {
            e.printStackTrace();
            fail(e.getMessage());
        }
        net = new MyNetwork(new RPCNetworkParams().setNumNetworkThreads(1)
                .setIdentity(new Identity(identity))
                .setSlobrokConfigId(TestServer.getSlobrokConfig(slobrok)));
        mbus = new MessageBus(net, new MessageBusParams()
                .addProtocol(new DocumentProtocol(documentMgr)));
    }

    /**
     * Create a test frame running on the same slobrok and mbus as another.
     *
     * @param frame The frame whose internals to share.
     */
    public PolicyTestFrame(PolicyTestFrame frame) {
        identity = frame.identity;
        slobrok = frame.slobrok;
        net = frame.net;
        mbus = frame.mbus;
    }

    // Inherit doc from Object.
    @Override
    public void finalize() throws Throwable {
        destroy();
        super.finalize();
    }

    /**
     * Sets the destroyed flag to true. The very first time this method is called, it cleans up all its dependencies.
     * Even if you retain a reference to this object, all of its content is allowed to be garbage collected.
     */
    public void destroy() {
        if (!destroyed.getAndSet(true)) {
            mbus.destroy();
            mbus = null;
            net = null;

            slobrok.stop();
            slobrok = null;
        }
    }

    /**
     * Routes the contained message based on the current setup, and returns the leaf send contexts.
     *
     * @param numExpected The expected number of leaf nodes.
     * @return The list of selected send contexts.
     */
    public List<RoutingNode> select(int numExpected) {
        msg.setRoute(Route.parse(hop.getName()));
        new RoutingNode(mbus, net, null, handler, msg).send();
        List<RoutingNode> ret = net.removeNodes();
        assertEquals(numExpected, ret.size());
        return ret;
    }

    /**
     * Ensures that the current setup selects a given set of routes.
     *
     * @param expected A list of expected route leaf nodes.
     */
    public void assertSelect(List<String> expected) {
        if (expected == null) {
            assertEquals(0, select(0).size());
        } else {
            List<RoutingNode> selected = select(expected.size());
            for (RoutingNode node : selected) {
                assertTrue("Route '" + node.getRoute() + "' not selected.",
                           expected.contains(node.getRoute().toString()));
                node.handleReply(new EmptyReply());
            }
        }
        assertNotNull(handler.getReply(60));
    }

    /**
     * This is a convenience method for invoking {@link this#assertMerge(Map,List,List)} with no expected errors.
     *
     * @param replies       The errors to set in the leaf node replies.
     * @param allowedValues The list of allowed values in the final reply.
     */
    public void assertMergeOk(Map<String, Integer> replies, List<String> allowedValues) {
        assertMerge(replies, null, allowedValues);
    }

    /**
     * Ensures that the current setup generates as many leaf nodes as there are members of the errors argument. Each
     * error is then given one of these errors, and the method then ensures that the single returned reply contains the
     * given list of expected errors. Finally, if the expected value argument is non-null, this method ensures that the
     * reply is a SimpleReply whose string value exists in the allowed list.
     *
     * @param replies        The errors to set in the leaf node replies.
     * @param expectedErrors The list of expected errors in the merged reply.
     * @param allowedValues  The list of allowed values in the final reply.
     */
    public void assertMerge(Map<String, Integer> replies, List<Integer> expectedErrors, List<String> allowedValues) {
        List<RoutingNode> selected = select(replies.size());

        for (RoutingNode node : selected) {
            String route = node.getRoute().toString();
            assertTrue(replies.containsKey(route));
            Reply ret = new SimpleReply(route);
            if (replies.get(route) != ErrorCode.NONE) {
                ret.addError(new com.yahoo.messagebus.Error(replies.get(route), route));
            }
            node.handleReply(ret);
        }

        Reply reply = handler.getReply(60);
        assertNotNull(reply);
        if (expectedErrors != null) {
            assertEquals(expectedErrors.size(), reply.getNumErrors());
            for (int i = 0; i < expectedErrors.size(); ++i) {
                assertTrue(expectedErrors.contains(reply.getError(i).getCode()));
            }
        } else if (reply.hasErrors()) {
            StringBuilder err = new StringBuilder("Got unexpected error(s):\n");
            for (int i = 0; i < reply.getNumErrors(); ++i) {
                err.append("\t").append(reply.getError(i).toString());
                if (i < reply.getNumErrors() - 1) {
                    err.append("\n");
                }
            }
            fail(err.toString());
        }
        if (allowedValues != null) {
            assertEquals(SimpleProtocol.REPLY, reply.getType());
            assertTrue(allowedValues.contains(((SimpleReply)reply).getValue()));
        } else {
            assertEquals(0, reply.getType());
        }
    }

    /**
     * Ensures that the current setup chooses a single recipient, and that it merges similarly to how the
     * {@link DocumentProtocol} would merge these.
     *
     * @param recipient The expected recipient.
     */
    public void assertMergeOneReply(String recipient) {
        assertSelect(Arrays.asList(recipient));

        Map<String, Integer> replies = new HashMap<>();
        replies.put(recipient, ErrorCode.NONE);
        assertMergeOk(replies, List.of(recipient));

        replies.put(recipient, ErrorCode.TRANSIENT_ERROR);
        assertMerge(replies, List.of(ErrorCode.TRANSIENT_ERROR), List.of(recipient));
    }

    /**
     * Ensures that the current setup will choose the two given recipients, and that it merges similarly to how the
     * {@link DocumentProtocol} would merge these.
     *
     * @param recipientOne The first expected recipient.
     * @param recipientTwo The second expected recipient.
     */
    public void assertMergeTwoReplies(String recipientOne, String recipientTwo) {
        assertSelect(Arrays.asList(recipientOne, recipientTwo));

        Map<String, Integer> replies = new HashMap<>();
        replies.put(recipientOne, ErrorCode.NONE);
        replies.put(recipientTwo, ErrorCode.NONE);
        assertMergeOk(replies, List.of(recipientOne, recipientTwo));

        replies.put(recipientOne, ErrorCode.TRANSIENT_ERROR);
        replies.put(recipientTwo, ErrorCode.NONE);
        assertMerge(replies, List.of(ErrorCode.TRANSIENT_ERROR), List.of(recipientOne));

        replies.put(recipientOne, ErrorCode.TRANSIENT_ERROR);
        replies.put(recipientTwo, ErrorCode.FATAL_ERROR);
        assertMerge(replies, List.of(ErrorCode.TRANSIENT_ERROR, ErrorCode.FATAL_ERROR), List.of(recipientTwo));

        replies.put(recipientOne, ErrorCode.TRANSIENT_ERROR);
        replies.put(recipientTwo, ErrorCode.TRANSIENT_ERROR);
        assertMerge(replies, Arrays.asList(ErrorCode.TRANSIENT_ERROR, ErrorCode.TRANSIENT_ERROR), List.of(recipientOne, recipientTwo));

        replies.put(recipientOne, ErrorCode.NONE);
        replies.put(recipientTwo, DocumentProtocol.ERROR_MESSAGE_IGNORED);
        assertMergeOk(replies, List.of(recipientOne));

        replies.put(recipientOne, DocumentProtocol.ERROR_MESSAGE_IGNORED);
        replies.put(recipientTwo, ErrorCode.NONE);
        assertMergeOk(replies, List.of(recipientTwo));

        replies.put(recipientOne, DocumentProtocol.ERROR_MESSAGE_IGNORED);
        replies.put(recipientTwo, DocumentProtocol.ERROR_MESSAGE_IGNORED);
        assertMerge(replies, List.of(DocumentProtocol.ERROR_MESSAGE_IGNORED,
                                     DocumentProtocol.ERROR_MESSAGE_IGNORED),
                    null); // Only ignored errors specifically causes an EmptyReply.
    }

    /**
     * Waits for a given service pattern to resolve to the given number of hits in the local slobrok.
     *
     * @param pattern The pattern to lookup.
     * @param cnt     The number of entries to wait for.
     * @return True if the expected number of entries was found.
     */
    public boolean waitSlobrok(String pattern, int cnt) {
        for (int i = 0; i < 1000 && !Thread.currentThread().isInterrupted(); ++i) {
            List<Mirror.Entry> res = net.getMirror().lookup(pattern);
            if (res.size() == cnt) {
                return true;
            }
            try { Thread.sleep(10); } catch (InterruptedException e) { /* ignore */ }
        }
        return false;
    }

    /**
     * Returns the identity of this frame.
     *
     * @return The ident string.
     */
    public String getIdentity() {
        return identity;
    }

    /**
     * Returns the private slobrok server.
     *
     * @return The slobrok.
     */
    public Slobrok getSlobrok() {
        return slobrok;
    }

    /**
     * Returns the private message bus.
     *
     * @return The bus.
     */
    public MessageBus getMessageBus() {
        return mbus;
    }

    /**
     * Returns the private network layer.
     *
     * @return The network.
     */
    public Network getNetwork() {
        return net;
    }

    /**
     * Returns the message being tested.
     *
     * @return The message.
     */
    public Message getMessage() {
        return msg;
    }

    /**
     * Sets the message being tested.
     *
     * @param msg The message to set.
     */
    public void setMessage(Message msg) {
        this.msg = msg;
    }

    /**
     * Sets the spec of the hop to test with.
     *
     * @param hop The spec to set.
     */
    public void setHop(HopSpec hop) {
        this.hop = hop;
        mbus.setupRouting(new RoutingSpec().addTable(new RoutingTableSpec(DocumentProtocol.NAME).addHop(hop)));
    }

    /**
     * Returns the reply receptor used by this frame. All messages tested are tagged with this receptor, so after a
     * successful select, the receptor should contain a non-null reply.
     *
     * @return The reply receptor.
     */
    public Receptor getReceptor() {
        return handler;
    }

    /**
     * Implements a dummy network.
     */
    private class MyNetwork extends RPCNetwork {

        private List<RoutingNode> nodes = new ArrayList<>();

        public MyNetwork(RPCNetworkParams params) {
            super(params);
        }

        @Override
        public boolean allocServiceAddress(RoutingNode recipient) {
            recipient.setServiceAddress(new ServiceAddress() { });
            return true;
        }

        @Override
        public void freeServiceAddress(RoutingNode recipient) {
            recipient.setServiceAddress(null);
        }

        @Override
        public void send(Message msg, List<RoutingNode> recipients) {
            this.nodes.addAll(recipients);
        }

        public List<RoutingNode> removeNodes() {
            List<RoutingNode> ret = nodes;
            nodes = new ArrayList<>();
            return ret;
        }
    }

}