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

import com.yahoo.jdisc.Request;
import com.yahoo.jdisc.Response;
import com.yahoo.jdisc.application.ContainerBuilder;
import com.yahoo.jdisc.handler.RequestDispatch;
import com.yahoo.jdisc.test.TestDriver;
import com.yahoo.messagebus.EmptyReply;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.test.SimpleMessage;
import org.junit.Test;

import java.net.URI;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

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

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

    @Test
    public void requireThatNonMbusRequestThrows() throws Exception {
        final TestDriver driver = newTestDriver(SameThreadReplier.INSTANCE);
        try {
            new RequestDispatch() {

                @Override
                protected Request newRequest() {
                    return new Request(driver, URI.create("mbus://localhost/"));
                }
            }.connect();
            fail();
        } catch (UnsupportedOperationException e) {
            assertEquals("Expected MbusRequest, got com.yahoo.jdisc.Request.", e.getMessage());
        }
        assertTrue(driver.close());
    }

    @Test
    public void requireThatHandlerCanRespondInSameThread() throws Exception {
        TestDriver driver = newTestDriver(SameThreadReplier.INSTANCE);

        Response response = dispatchMessage(driver, new SimpleMessage("msg")).get(60, TimeUnit.SECONDS);
        assertTrue(response instanceof MbusResponse);
        assertEquals(Response.Status.OK, response.getStatus());
        Reply reply = ((MbusResponse)response).getReply();
        assertTrue(reply instanceof EmptyReply);
        assertFalse(reply.hasErrors());

        assertTrue(driver.close());
    }

    @Test
    public void requireThatHandlerCanRespondInOtherThread() throws Exception {
        TestDriver driver = newTestDriver(ThreadedReplier.INSTANCE);

        Response response = dispatchMessage(driver, new SimpleMessage("msg")).get(60, TimeUnit.SECONDS);
        assertTrue(response instanceof MbusResponse);
        assertEquals(Response.Status.OK, response.getStatus());
        Reply reply = ((MbusResponse)response).getReply();
        assertTrue(reply instanceof EmptyReply);
        assertFalse(reply.hasErrors());

        assertTrue(driver.close());
    }

    private static TestDriver newTestDriver(MbusRequestHandler handler) {
        TestDriver driver = TestDriver.newSimpleApplicationInstanceWithoutOsgi();
        ContainerBuilder builder = driver.newContainerBuilder();
        builder.serverBindings().bind("mbus://*/*", handler);
        driver.activateContainer(builder);
        return driver;
    }

    private static CompletableFuture<Response> dispatchMessage(final TestDriver driver, final Message msg) {
        return new RequestDispatch() {

            @Override
            protected Request newRequest() {
                return new MbusRequest(driver, URI.create("mbus://localhost/"), msg);
            }
        }.dispatch();
    }

    private static class SameThreadReplier extends MbusRequestHandler {

        final static SameThreadReplier INSTANCE = new SameThreadReplier();

        @Override
        public void handleMessage(Message msg) {
            Reply reply = new EmptyReply();
            reply.swapState(msg);
            reply.popHandler().handleReply(reply);
        }
    }

    private static class ThreadedReplier extends MbusRequestHandler {

        final static ThreadedReplier INSTANCE = new ThreadedReplier();

        @Override
        public void handleMessage(final Message msg) {
            Executors.newSingleThreadExecutor().execute(new Runnable() {

                @Override
                public void run() {
                    SameThreadReplier.INSTANCE.handleMessage(msg);
                }
            });
        }
    }
}