aboutsummaryrefslogtreecommitdiffstats
path: root/documentapi/src/test/java/com/yahoo/documentapi/messagebus/protocol/test/RoutableFactoryTestCase.java
blob: 6d253e1f48a757e286b4f509187efd173b02c04e (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
// Copyright Vespa.ai. 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.component.VersionSpecification;
import com.yahoo.document.DocumentTypeManager;
import com.yahoo.document.serialization.DocumentDeserializer;
import com.yahoo.document.serialization.DocumentSerializer;
import com.yahoo.documentapi.messagebus.protocol.DocumentMessage;
import com.yahoo.documentapi.messagebus.protocol.DocumentProtocol;
import com.yahoo.documentapi.messagebus.protocol.DocumentReply;
import com.yahoo.documentapi.messagebus.protocol.RoutableFactories60;
import com.yahoo.jrt.ListenFailedException;
import com.yahoo.jrt.slobrok.server.Slobrok;
import com.yahoo.messagebus.DestinationSession;
import com.yahoo.messagebus.DestinationSessionParams;
import com.yahoo.messagebus.ErrorCode;
import com.yahoo.messagebus.Message;
import com.yahoo.messagebus.MessageBusParams;
import com.yahoo.messagebus.Reply;
import com.yahoo.messagebus.SourceSession;
import com.yahoo.messagebus.SourceSessionParams;
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.routing.Route;
import com.yahoo.messagebus.test.Receptor;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

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

    private Slobrok slobrok;
    private DocumentProtocol srcProtocol, dstProtocol;
    private TestServer srcServer, dstServer;
    private SourceSession srcSession;
    private DestinationSession dstSession;

    @Before
    public void setUp() throws ListenFailedException {
        slobrok = new Slobrok();
        DocumentTypeManager docMan = new DocumentTypeManager();
        dstProtocol = new DocumentProtocol(docMan);
        dstServer = new TestServer(new MessageBusParams().addProtocol(dstProtocol),
                                   new RPCNetworkParams().setNumNetworkThreads(1).setIdentity(new Identity("dst")).setSlobrokConfigId(TestServer.getSlobrokConfig(slobrok)));
        dstSession = dstServer.mb.createDestinationSession(new DestinationSessionParams().setName("session").setMessageHandler(new Receptor()));
        srcProtocol = new DocumentProtocol(docMan);
        srcServer = new TestServer(new MessageBusParams().addProtocol(srcProtocol),
                                   new RPCNetworkParams().setNumNetworkThreads(1).setSlobrokConfigId(TestServer.getSlobrokConfig(slobrok)));
        srcSession = srcServer.mb.createSourceSession(new SourceSessionParams().setReplyHandler(new Receptor()));
        assertTrue(srcServer.waitSlobrok("dst/session", 1));
    }

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

    @Test
    public void testFactory() {
        Route route = Route.parse("dst/session");

        // Source should fail to encode the message.
        assertTrue(srcSession.send(new MyMessage(), route).isAccepted());
        Reply reply = ((Receptor)srcSession.getReplyHandler()).getReply(60);
        assertNotNull(reply);
        System.out.println(reply.getTrace());
        assertTrue(reply.hasErrors());
        assertEquals(ErrorCode.ENCODE_ERROR, reply.getError(0).getCode());
        assertNull(reply.getError(0).getService());

        // Destination should fail to decode the message.
        srcProtocol.putRoutableFactory(MyMessage.TYPE, new MyMessageFactory(), new VersionSpecification());
        assertTrue(srcSession.send(new MyMessage(), route).isAccepted());
        assertNotNull(reply = ((Receptor)srcSession.getReplyHandler()).getReply(60));
        System.out.println(reply.getTrace());
        assertTrue(reply.hasErrors());
        assertEquals(ErrorCode.DECODE_ERROR, reply.getError(0).getCode());
        assertEquals("dst/session", reply.getError(0).getService());

        // Destination should fail to encode the reply.
        dstProtocol.putRoutableFactory(MyMessage.TYPE, new MyMessageFactory(), new VersionSpecification());
        assertTrue(srcSession.send(new MyMessage(), route).isAccepted());
        Message msg = ((Receptor)dstSession.getMessageHandler()).getMessage(60);
        assertNotNull(msg);
        reply = new MyReply();
        reply.swapState(msg);
        dstSession.reply(reply);
        assertNotNull(reply = ((Receptor)srcSession.getReplyHandler()).getReply(60));
        System.out.println(reply.getTrace());
        assertTrue(reply.hasErrors());
        assertEquals(ErrorCode.ENCODE_ERROR, reply.getError(0).getCode());
        assertEquals("dst/session", reply.getError(0).getService());

        // Source should fail to decode the reply.
        dstProtocol.putRoutableFactory(MyReply.TYPE, new MyReplyFactory(), new VersionSpecification());
        assertTrue(srcSession.send(new MyMessage(), route).isAccepted());
        assertNotNull(msg = ((Receptor)dstSession.getMessageHandler()).getMessage(60));
        reply = new MyReply();
        reply.swapState(msg);
        dstSession.reply(reply);
        assertNotNull(reply = ((Receptor)srcSession.getReplyHandler()).getReply(60));
        System.out.println(reply.getTrace());
        assertTrue(reply.hasErrors());
        assertEquals(ErrorCode.DECODE_ERROR, reply.getError(0).getCode());
        assertNull(reply.getError(0).getService());

        // All should succeed.
        srcProtocol.putRoutableFactory(MyReply.TYPE, new MyReplyFactory(), new VersionSpecification());
        assertTrue(srcSession.send(new MyMessage(), route).isAccepted());
        assertNotNull(msg = ((Receptor)dstSession.getMessageHandler()).getMessage(60));
        reply = new MyReply();
        reply.swapState(msg);
        dstSession.reply(reply);
        assertNotNull(reply = ((Receptor)srcSession.getReplyHandler()).getReply(60));
        System.out.println(reply.getTrace());
        assertFalse(reply.hasErrors());
    }

    private static class MyMessageFactory extends RoutableFactories60.DocumentMessageFactory {

        @Override
        protected DocumentMessage doDecode(DocumentDeserializer buf) {
            return new MyMessage();
        }

        @Override
        protected boolean doEncode(DocumentMessage msg, DocumentSerializer buf) {
            return true;
        }
    }

    private static class MyReplyFactory extends RoutableFactories60.DocumentReplyFactory {

        @Override
        protected DocumentReply doDecode(DocumentDeserializer buf) {
            return new MyReply();
        }

        @Override
        protected boolean doEncode(DocumentReply msg, DocumentSerializer buf) {
            return true;
        }
    }

    private static class MyMessage extends DocumentMessage {

        final static int TYPE = 666;

        MyMessage() {
            getTrace().setLevel(9);
        }

        @Override
        public DocumentReply createReply() {
            return new MyReply();
        }

        @Override
        public int getType() {
            return TYPE;
        }
    }

    private static class MyReply extends DocumentReply {

        final static int TYPE = 777;

        public MyReply() {
            super(TYPE);
        }
    }

}