/
RabbitMQClientActorTest.java
362 lines (302 loc) · 15.9 KB
/
RabbitMQClientActorTest.java
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
/*
* Copyright (c) 2017 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Eclipse Public License 2.0 which is available at
* http://www.eclipse.org/legal/epl-2.0
*
* SPDX-License-Identifier: EPL-2.0
*/
package org.eclipse.ditto.services.connectivity.messaging.rabbitmq;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.eclipse.ditto.services.connectivity.messaging.TestConstants.MODIFY_THING_WITH_ACK;
import static org.eclipse.ditto.services.connectivity.messaging.TestConstants.Sources.AMQP_SOURCE_ADDRESS;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.io.IOException;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.stream.Stream;
import org.assertj.core.api.ThrowableAssert;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.connectivity.Connection;
import org.eclipse.ditto.model.connectivity.ConnectionConfigurationInvalidException;
import org.eclipse.ditto.model.connectivity.ConnectionId;
import org.eclipse.ditto.model.connectivity.ConnectionType;
import org.eclipse.ditto.model.connectivity.ConnectivityModelFactory;
import org.eclipse.ditto.model.connectivity.ConnectivityStatus;
import org.eclipse.ditto.model.connectivity.Target;
import org.eclipse.ditto.model.connectivity.Topic;
import org.eclipse.ditto.services.connectivity.messaging.AbstractBaseClientActorTest;
import org.eclipse.ditto.services.connectivity.messaging.TestConstants;
import org.eclipse.ditto.services.models.connectivity.BaseClientState;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.connectivity.modify.CloseConnection;
import org.eclipse.ditto.signals.commands.connectivity.modify.OpenConnection;
import org.eclipse.ditto.signals.commands.things.modify.ModifyThing;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import com.newmotion.akka.rabbitmq.AmqpShutdownSignal;
import com.rabbitmq.client.AMQP;
import com.rabbitmq.client.Channel;
import com.rabbitmq.client.ConnectionFactory;
import com.rabbitmq.client.Consumer;
import com.rabbitmq.client.Envelope;
import com.rabbitmq.client.ShutdownSignalException;
import akka.actor.Actor;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.actor.Status;
import akka.testkit.CallingThreadDispatcher;
import akka.testkit.javadsl.TestKit;
@RunWith(MockitoJUnitRunner.class)
public final class RabbitMQClientActorTest extends AbstractBaseClientActorTest {
private static final Status.Success CONNECTED_SUCCESS = new Status.Success(BaseClientState.CONNECTED);
private static final Status.Success DISCONNECTED_SUCCESS = new Status.Success(BaseClientState.DISCONNECTED);
private static final IllegalArgumentException CUSTOM_EXCEPTION =
new IllegalArgumentException("custom error message");
private static final ConnectionId CONNECTION_ID = TestConstants.createRandomConnectionId();
@SuppressWarnings("NullableProblems") private static ActorSystem actorSystem;
private static Connection connection;
@Mock private ConnectionFactory mockConnectionFactory;
@Mock private ConnectionFactory failingMockConnectionFactory;
@Mock private com.rabbitmq.client.Connection mockConnection;
@Mock private com.rabbitmq.client.Connection mockReconnection;
@Mock private Channel mockChannel;
@Mock private Channel mockChannelReconnected;
private final RabbitConnectionFactoryFactory rabbitConnectionFactoryFactory =
(con, exHandler, connectionLogger) -> mockConnectionFactory;
@BeforeClass
public static void setUp() {
actorSystem = ActorSystem.create("AkkaTestSystem", TestConstants.CONFIG);
connection = TestConstants.createConnection(CONNECTION_ID)
.toBuilder()
.connectionStatus(ConnectivityStatus.CLOSED)
.build();
}
@AfterClass
public static void tearDown() {
TestKit.shutdownActorSystem(actorSystem, scala.concurrent.duration.Duration.apply(5, TimeUnit.SECONDS), false);
}
@Before
public void init() throws IOException, TimeoutException {
when(mockConnectionFactory.newConnection()).thenReturn(mockConnection);
when(failingMockConnectionFactory.newConnection())
.thenReturn(mockConnection)
.thenReturn(mockReconnection);
when(mockConnection.createChannel()).thenReturn(mockChannel);
when(mockReconnection.createChannel()).thenReturn(mockChannelReconnected);
}
@Test
public void invalidTargetFormatThrowsConnectionConfigurationInvalidException() {
final Connection connection = ConnectivityModelFactory.newConnectionBuilder(CONNECTION_ID,
ConnectionType.AMQP_091, ConnectivityStatus.OPEN, TestConstants.getUriOfNewMockServer())
.targets(Collections.singletonList(ConnectivityModelFactory.newTargetBuilder()
.address("exchangeOnly")
.authorizationContext(TestConstants.Authorization.AUTHORIZATION_CONTEXT)
.topics(Topic.TWIN_EVENTS)
.build()))
.build();
final ThrowableAssert.ThrowingCallable props1 =
() -> RabbitMQClientActor.propsForTests(connection, Actor.noSender(), Actor.noSender(), null);
final ThrowableAssert.ThrowingCallable props2 =
() -> RabbitMQClientActor.propsForTests(connection, Actor.noSender(), Actor.noSender(),
rabbitConnectionFactoryFactory
);
Stream.of(props1, props2)
.forEach(throwingCallable ->
assertThatExceptionOfType(ConnectionConfigurationInvalidException.class)
.isThrownBy(throwingCallable)
.withMessageContaining("exchangeOnly")
.withNoCause()
);
}
@Test
public void testExceptionDuringConnectionFactoryCreation() {
new TestKit(actorSystem) {{
final Props props =
RabbitMQClientActor.propsForTests(connection, getRef(),
getRef(), (con, exHandler, connectionLogger) -> { throw CUSTOM_EXCEPTION; })
.withDispatcher(CallingThreadDispatcher.Id());
final ActorRef connectionActor = actorSystem.actorOf(props);
connectionActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsgClass(Status.Failure.class);
}};
}
@Test
public void testConnectionHandling() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef(), getConnection(false));
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
watch(rabbitClientActor);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}};
}
@Test
public void testReconnectionHandling() throws IOException {
new TestKit(actorSystem) {{
final ArgumentCaptor<Consumer> consumer = ArgumentCaptor.forClass(Consumer.class);
final ArgumentCaptor<Consumer> reconnectedConsumer = ArgumentCaptor.forClass(Consumer.class);
final Connection connection = getConnection(false);
final Props props = getClientActorProps(getRef(), connection, failingMockConnectionFactory);
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
watch(rabbitClientActor);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
// 1 consume channel + 1 publish channel
verify(mockConnection, times(2)).createChannel();
// 2 invocations because consumer count is set to 2
verify(mockChannel, times(2)).basicConsume(eq(AMQP_SOURCE_ADDRESS), eq(false),
consumer.capture());
// verify inbound signal processed
sendInboundModifyThingProtocolMessage(consumer.getValue());
final ModifyThing modifyThing = expectMsgClass(ModifyThing.class);
// verify outbound signal processed
final Signal<?> thingModified = TestConstants.thingModified(
TestConstants.Targets.TWIN_TARGET.getAuthorizationContext().getAuthorizationSubjects());
rabbitClientActor.tell(thingModified, ActorRef.noSender());
verifyPublishOnChannel(mockChannel);
// simulate rabbitmq connection gets interrupted
final AmqpShutdownSignal shutdownSignal =
AmqpShutdownSignal.apply(new ShutdownSignalException(true, true, null, mockConnection));
ActorSelection.apply(rabbitClientActor, "rmq-connection*").tell(shutdownSignal, getRef());
// verify consume and publish channel created on new connection
verify(mockReconnection, timeout(6_000).times(2)).createChannel();
// verify basicConsume called on new channel
verify(mockChannelReconnected, timeout(6_000).times(2))
.basicConsume(eq(AMQP_SOURCE_ADDRESS), eq(false), reconnectedConsumer.capture());
// verify inbound signal processed after reconnect
sendInboundModifyThingProtocolMessage(reconnectedConsumer.getValue());
expectMsgClass(ModifyThing.class);
// verify outbound signal processed after reconnect
rabbitClientActor.tell(thingModified, ActorRef.noSender());
verifyPublishOnChannel(mockChannelReconnected);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}};
}
private void verifyPublishOnChannel(final Channel ch) throws IOException {
verify(ch, timeout(500).times(1))
.basicPublish(eq("twinEventExchange"), eq("twinEventRoutingKey"), eq(true),
any(AMQP.BasicProperties.class), any(byte[].class));
}
private void sendInboundModifyThingProtocolMessage(final Consumer consumer) throws IOException {
final Envelope envelope = new Envelope(1L, false, "exchange", "key");
final AMQP.BasicProperties basicProperties = new AMQP.BasicProperties.Builder().build();
consumer.handleDelivery("whatever", envelope, basicProperties, MODIFY_THING_WITH_ACK.getBytes());
}
@Test
public void testConnectionWithoutPublisherHandling() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connectionWithoutTargets =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props =
RabbitMQClientActor.propsForTests(connectionWithoutTargets, getRef(), getRef(),
(con, exHandler, connectionLogger) -> mockConnectionFactory)
.withDispatcher(CallingThreadDispatcher.Id());
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
watch(rabbitClientActor);
rabbitClientActor.tell(OpenConnection.of(randomConnectionId, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(randomConnectionId, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}};
}
@Test
public void testReconnection() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef(), getConnection(false));
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
// reconnect a few times
for (int i = 0; i < 3; ++i) {
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}
}};
}
@Test
public void sendCommandDuringInit() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef(), getConnection(false));
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
watch(rabbitClientActor);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
}};
}
@Test
public void sendConnectCommandWhenAlreadyConnected() throws IOException {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef(), getConnection(false));
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
// a publisher and a consumer channel should be created
verify(mockConnection, Mockito.timeout(500).times(2)).createChannel();
}};
}
@Test
public void sendDisconnectWhenAlreadyDisconnected() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef(), getConnection(false));
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
Mockito.verifyZeroInteractions(mockConnection);
}};
}
@Test
public void testCloseConnectionFails() {
new TestKit(actorSystem) {{
final Props props = createClientActor(getRef(), getConnection(false));
final ActorRef rabbitClientActor = actorSystem.actorOf(props);
rabbitClientActor.tell(OpenConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(CONNECTED_SUCCESS);
rabbitClientActor.tell(CloseConnection.of(CONNECTION_ID, DittoHeaders.empty()), getRef());
expectMsg(DISCONNECTED_SUCCESS);
}};
}
@Override
protected Connection getConnection(final boolean isSecure) {
return isSecure ? setScheme(connection, "amqps") : connection;
}
@Override
protected Props createClientActor(final ActorRef proxyActor, final Connection connection) {
return getClientActorProps(proxyActor, connection, mockConnectionFactory);
}
private Props getClientActorProps(final ActorRef proxyActor, final Connection connection,
final ConnectionFactory connectionFactory) {
return RabbitMQClientActor.propsForTests(connection, proxyActor,
proxyActor, (con, exHandler, connectionLogger) -> connectionFactory)
.withDispatcher(CallingThreadDispatcher.Id());
}
@Override
protected ActorSystem getActorSystem() {
return actorSystem;
}
}