aboutsummaryrefslogtreecommitdiffstats
path: root/container-messagebus/src/main/java/com/yahoo/messagebus/jdisc/test/ServerTestDriver.java
blob: 5528085c5b80fe616fa182877fdae9349067c074 (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
// Copyright Vespa.ai. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.messagebus.jdisc.test;

import com.google.inject.Module;
import com.yahoo.jdisc.application.ContainerBuilder;
import com.yahoo.jdisc.handler.RequestHandler;
import com.yahoo.jdisc.test.TestDriver;
import com.yahoo.messagebus.*;
import com.yahoo.messagebus.Error;
import com.yahoo.messagebus.jdisc.MbusServer;
import com.yahoo.messagebus.network.rpc.RPCNetworkParams;
import com.yahoo.messagebus.routing.Route;
import com.yahoo.messagebus.shared.ServerSession;
import com.yahoo.messagebus.shared.SharedMessageBus;
import com.yahoo.messagebus.test.SimpleProtocol;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.TimeUnit;

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

    private final RemoteClient client;
    private final MbusServer server;
    private final TestDriver driver;

    private ServerTestDriver(RemoteClient client, boolean activateContainer, RequestHandler requestHandler,
                             Protocol protocol, Module... guiceModules)
    {
        this.client = client;
        driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi(guiceModules);
        if (activateContainer) {
            ContainerBuilder builder = driver.newContainerBuilder();
            if (requestHandler != null) {
                builder.serverBindings().bind("mbus://*/*", requestHandler);
            }
            driver.activateContainer(builder);
        }

        MessageBusParams mbusParams = new MessageBusParams().addProtocol(protocol);
        RPCNetworkParams netParams = new RPCNetworkParams().setSlobroksConfig(client.slobroksConfig());
        SharedMessageBus mbus = SharedMessageBus.newInstance(mbusParams, netParams);
        ServerSession session = mbus.newDestinationSession(new DestinationSessionParams());
        server = new MbusServer(driver, session);
        server.start();
        session.release();
        mbus.release();
    }

    public boolean sendMessage(Message msg) {
        msg.setRoute(Route.parse(server.connectionSpec()));
        msg.getTrace().setLevel(9);
        return client.sendMessage(msg).isAccepted();
    }

    public Reply awaitReply() {
        Reply reply = null;
        try {
            reply = client.awaitReply(60, TimeUnit.SECONDS);
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }
        if (reply != null) {
            System.out.println(reply.getTrace());
        }
        return reply;
    }

    public Reply awaitSuccess() {
        Reply reply = awaitReply();
        if (reply == null || reply.hasErrors()) {
            return null;
        }
        return reply;
    }

    public Reply awaitErrors(Integer... errCodes) {
        Reply reply = awaitReply();
        if (reply == null) {
            return null;
        }
        List<Integer> lst = new LinkedList<>(Arrays.asList(errCodes));
        for (int i = 0, len = reply.getNumErrors(); i < len; ++i) {
            Error err = reply.getError(i);
            System.out.println(err);
            int idx = lst.indexOf(err.getCode());
            if (idx < 0) {
                return null;
            }
            lst.remove(idx);
        }
        if (!lst.isEmpty()) {
            return null;
        }
        return reply;
    }

    public boolean close() {
        server.close();
        server.release();
        client.close();
        return driver.close();
    }

    public TestDriver parent() {
        return driver;
    }

    public RemoteClient client() {
        return client;
    }

    public MbusServer server() {
        return server;
    }

    public static ServerTestDriver newInstance(RequestHandler requestHandler, boolean network, Module... guiceModules) {
        return new ServerTestDriver(RemoteClient.newInstanceWithInternSlobrok(network), true, requestHandler,
                                    new SimpleProtocol(), guiceModules);
    }

    public static ServerTestDriver newInstanceWithProtocol(Protocol protocol, RequestHandler requestHandler,
                                                           boolean network, Module... guiceModules)
    {
        return new ServerTestDriver(RemoteClient.newInstanceWithInternSlobrok(network), true, requestHandler, protocol,
                                    guiceModules);
    }

    public static ServerTestDriver newInactiveInstance(boolean network, Module... guiceModules) {
        return new ServerTestDriver(RemoteClient.newInstanceWithInternSlobrok(network), false, null,
                                    new SimpleProtocol(), guiceModules);
    }

    public static ServerTestDriver newInactiveInstanceWithProtocol(Protocol protocol, boolean network, Module... guiceModules) {
        return new ServerTestDriver(RemoteClient.newInstanceWithProtocolAndInternSlobrok(protocol, network), false, null,
                                    protocol, guiceModules);
    }

    public static ServerTestDriver newUnboundInstance(boolean network, Module... guiceModules) {
        return new ServerTestDriver(RemoteClient.newInstanceWithInternSlobrok(network), true, null,
                                    new SimpleProtocol(), guiceModules);
    }

}