/
BaseClientActorTest.java
695 lines (564 loc) · 31.5 KB
/
BaseClientActorTest.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
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/*
* Copyright (c) 2019 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.connectivity.service.messaging;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Mockito.timeout;
import static org.mockito.Mockito.verify;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
import javax.annotation.Nullable;
import org.eclipse.ditto.base.model.auth.AuthorizationContext;
import org.eclipse.ditto.base.model.auth.AuthorizationSubject;
import org.eclipse.ditto.base.model.auth.DittoAuthorizationContextType;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.connectivity.api.BaseClientState;
import org.eclipse.ditto.connectivity.api.InboundSignal;
import org.eclipse.ditto.connectivity.api.OutboundSignal;
import org.eclipse.ditto.connectivity.model.Connection;
import org.eclipse.ditto.connectivity.model.ConnectionId;
import org.eclipse.ditto.connectivity.model.ConnectivityModelFactory;
import org.eclipse.ditto.connectivity.model.Target;
import org.eclipse.ditto.connectivity.model.Topic;
import org.eclipse.ditto.connectivity.model.signals.announcements.ConnectionClosedAnnouncement;
import org.eclipse.ditto.connectivity.model.signals.announcements.ConnectionOpenedAnnouncement;
import org.eclipse.ditto.connectivity.model.signals.announcements.ConnectivityAnnouncement;
import org.eclipse.ditto.connectivity.model.signals.commands.exceptions.ConnectionSignalIllegalException;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.CloseConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.OpenConnection;
import org.eclipse.ditto.connectivity.model.signals.commands.modify.TestConnection;
import org.eclipse.ditto.connectivity.service.config.DittoConnectivityConfig;
import org.eclipse.ditto.connectivity.service.messaging.internal.ClientConnected;
import org.eclipse.ditto.connectivity.service.messaging.internal.ImmutableClientDisconnected;
import org.eclipse.ditto.connectivity.service.messaging.internal.ImmutableConnectionFailure;
import org.eclipse.ditto.connectivity.service.util.ConnectivityMdcEntryKey;
import org.eclipse.ditto.internal.utils.config.DefaultScopedConfig;
import org.eclipse.ditto.protocol.Adaptable;
import org.eclipse.ditto.protocol.TopicPath;
import org.eclipse.ditto.protocol.mapper.SignalMapperFactory;
import org.eclipse.ditto.thingsearch.model.signals.commands.subscription.CancelSubscription;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.FSM;
import akka.actor.Props;
import akka.actor.Status;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
/**
* Unit test for basic {@link BaseClientActor} functionality.
*/
@RunWith(MockitoJUnitRunner.class)
public final class BaseClientActorTest {
private static final Status.Success CONNECTED_STATUS = new Status.Success(BaseClientState.CONNECTED);
private static final Status.Success DISCONNECTED_STATUS = new Status.Success(BaseClientState.DISCONNECTED);
private static final Duration DEFAULT_MESSAGE_TIMEOUT = Duration.ofSeconds(3);
private static Duration DISCONNECT_TIMEOUT;
private static ActorSystem actorSystem;
private static DittoConnectivityConfig connectivityConfig;
@Mock
private BaseClientActor delegate;
@BeforeClass
public static void beforeClass() {
actorSystem = ActorSystem.create("AkkaTestSystem", TestConstants.CONFIG);
connectivityConfig =
DittoConnectivityConfig.of(DefaultScopedConfig.dittoScoped(actorSystem.settings().config()));
DISCONNECT_TIMEOUT = connectivityConfig.getClientConfig().getDisconnectAnnouncementTimeout()
.plus(connectivityConfig.getClientConfig().getDisconnectingMaxTimeout());
}
@AfterClass
public static void tearDown() {
if (null != actorSystem) {
TestKit.shutdownActorSystem(actorSystem, scala.concurrent.duration.Duration.apply(5, TimeUnit.SECONDS),
false);
}
}
@Test
public void reconnectsInConnectingStateIfFailureResponseReceived() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
andConnectionNotSuccessful(dummyClientActor);
expectMsgClass(Status.Failure.class);
thenExpectConnectClientCalled();
thenExpectConnectClientCalledAfterTimeout(connectivityConfig.getClientConfig().getConnectingMinTimeout());
}};
}
@Test
public void handlesCloseConnectionInConnectingState() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andClosingConnection(dummyClientActor, CloseConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectDisconnectClientCalled();
andDisconnectionSuccessful(dummyClientActor, getRef());
expectMsg(new Status.Success(BaseClientState.DISCONNECTED));
}};
}
@Test
public void handlesOpenConnectionInConnectingState() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
final TestProbe probe1 = TestProbe.apply(actorSystem);
final TestProbe probe2 = TestProbe.apply(actorSystem);
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
probe1.ref());
thenExpectConnectClientCalled();
// send another OpenConnection command while connecting
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
probe2.ref());
andConnectionSuccessful(dummyClientActor, probe1.ref());
// both recipients get responses
probe1.expectMsg(CONNECTED_STATUS);
probe2.expectMsg(CONNECTED_STATUS);
}};
}
@Test
public void handlesCloseConnectionInDisconnectingState() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andConnectionSuccessful(dummyClientActor, getRef());
expectMsg(CONNECTED_STATUS);
andClosingConnection(dummyClientActor, CloseConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectDisconnectClientCalled();
// send another CloseConnection command while disconnecting
andClosingConnection(dummyClientActor, CloseConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
expectMsgClass(ConnectionSignalIllegalException.class);
}};
}
@Test
public void handlesCloseConnectionInDisconnectedState() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props =
DummyClientActor.props(connection, actorSystem.deadLetters(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andConnectionSuccessful(dummyClientActor, getRef());
expectMsg(CONNECTED_STATUS);
andClosingConnection(dummyClientActor, CloseConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectDisconnectClientCalled();
andDisconnectionSuccessful(dummyClientActor, getRef());
expectMsg(DISCONNECTED_STATUS);
// send another CloseConnection command while disconnected
andClosingConnection(dummyClientActor, CloseConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
expectMsg(DISCONNECTED_STATUS);
}};
}
@Test
public void handlesOpenConnectionInConnectedState() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andConnectionSuccessful(dummyClientActor, getRef());
expectMsg(CONNECTED_STATUS);
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalledOnceAfterTimeout(Duration.ofSeconds(1));
expectMsg(CONNECTED_STATUS);
}};
}
@Test
public void reconnectsInConnectingStateIfNoResponseReceived() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andStateTimeoutSent(dummyClientActor);
expectMsgClass(Status.Failure.class);
thenExpectConnectClientCalledAfterTimeout(connectivityConfig.getClientConfig().getConnectingMinTimeout());
}};
}
@Test
public void shouldReconnectIfSocketIsClosed() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0])
.toBuilder()
.uri("amqps://username:password@127.0.0.1:65536") // port 65536 does not even exist ;)
.build();
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
expectMsgClass(Status.Failure.class);
thenExpectCleanupResourcesCalled();
Mockito.clearInvocations(delegate);
thenExpectCleanupResourcesCalledAfterTimeout(
connectivityConfig.getClientConfig().getConnectingMinTimeout());
thenExpectNoConnectClientCalled();
}};
}
@Test
public void doesNotReconnectIfConnectionSuccessful() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
Mockito.clearInvocations(delegate);
andConnectionSuccessful(dummyClientActor, getRef());
expectMsgClass(Status.Success.class);
thenExpectNoConnectClientCalledAfterTimeout(connectivityConfig.getClientConfig().getConnectingMinTimeout());
}};
}
@Test
public void connectsAutomaticallyAfterActorStart() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection =
TestConstants.createConnection(randomConnectionId, new Target[0]);
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
thenExpectConnectClientCalledAfterTimeout(Duration.ofSeconds(5L));
Mockito.clearInvocations(delegate);
andConnectionSuccessful(dummyClientActor, getRef());
}};
}
@Test
public void dispatchesSearchCommandsAccordingToSubscriptionIdPrefix() {
new TestKit(actorSystem) {{
final int clientCount = 12; // 12 clients fit inside 1 hexadecimal digit
final ConnectionId connectionId = TestConstants.createRandomConnectionId();
final Connection connection = TestConstants.createConnection(connectionId).toBuilder()
.clientCount(clientCount)
.build();
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef underTest = watch(actorSystem.actorOf(props, "zToBeLargerThanTestProbeRefs"));
expectMsg(underTest);
final List<TestProbe> probes = new ArrayList<>(clientCount - 1);
for (int i = 0; i < clientCount - 1; ++i) {
final TestProbe ithProbe = TestProbe.apply("aProbe" + Integer.toHexString(i), actorSystem);
probes.add(ithProbe);
underTest.tell(ithProbe.ref(), ActorRef.noSender());
}
for (int i = 0; i < probes.size(); ++i) {
final String prefix = Integer.toHexString(i);
final String subscriptionId = prefix + "-subscription-id";
final CancelSubscription command = CancelSubscription.of(subscriptionId, DittoHeaders.empty());
underTest.tell(InboundSignal.of(command), getRef());
probes.get(i).expectMsg(command);
}
}};
}
@Test
public void doesNotDispatchSearchCommandsWithInvalidSubscriptionId() {
new TestKit(actorSystem) {{
final ConnectionId connectionId = TestConstants.createRandomConnectionId();
final Connection connection = TestConstants.createConnection(connectionId).toBuilder()
.clientCount(2)
.build();
final Props props = DummyClientActor.props(connection, getRef(), getRef(), getRef(), delegate);
final ActorRef underTest = watch(actorSystem.actorOf(props, "underTest"));
expectMsg(underTest);
final String prefix = Integer.toHexString(5); // out-of-bound
final String subscriptionId = prefix + "-subscription-id";
final CancelSubscription command = CancelSubscription.of(subscriptionId, DittoHeaders.empty());
underTest.tell(InboundSignal.of(command), getRef());
expectNoMessage();
}};
}
@Test
public void sendsConnectionOpenedAnnouncementAfterConnect() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection = createConnectionWithConnectivityAnnouncementTarget(randomConnectionId);
final TestProbe publisherActor = TestProbe.apply(getSystem());
final Props props = DummyClientActor.props(connection, getRef(), getRef(), publisherActor.ref(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andConnectionSuccessful(dummyClientActor, getRef());
expectMsg(CONNECTED_STATUS);
thenExpectConnectionOpenedAnnouncement(publisherActor, randomConnectionId);
}};
}
@Test
public void sendsConnectionOpenedAnnouncementAfterReconnect() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection = createConnectionWithConnectivityAnnouncementTarget(randomConnectionId);
final TestProbe publisherActor = TestProbe.apply(getSystem());
final Props props = DummyClientActor.props(connection, getRef(), getRef(), publisherActor.ref(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andConnectionSuccessful(dummyClientActor, getRef());
expectMsg(CONNECTED_STATUS);
thenExpectConnectionOpenedAnnouncement(publisherActor, randomConnectionId);
andConnectionFails(dummyClientActor, getRef());
// not expecting a closed announcement after connection failure, since it's not possible to send a message
// if a connecting is failed and thus not connected
andConnectionSuccessful(dummyClientActor, getRef());
thenExpectConnectionOpenedAnnouncement(publisherActor, randomConnectionId);
}};
}
@Test
public void sendsConnectionClosedAnnouncementWhenConnectionGetsClosed() {
new TestKit(actorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection = createConnectionWithConnectivityAnnouncementTarget(randomConnectionId);
final TestProbe publisherActor = TestProbe.apply(getSystem());
final Props props = DummyClientActor.props(connection, getRef(), getRef(), publisherActor.ref(), delegate);
final ActorRef dummyClientActor = watch(actorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andConnectionSuccessful(dummyClientActor, getRef());
expectMsg(CONNECTED_STATUS);
thenExpectConnectionOpenedAnnouncement(publisherActor, randomConnectionId);
andClosingConnection(dummyClientActor, CloseConnection.of(randomConnectionId, DittoHeaders.empty()), getRef());
thenExpectDisconnectClientNotCalledForAtLeast(
connectivityConfig.getClientConfig().getConnectingMinTimeout().dividedBy(2L));
thenExpectConnectionClosedAnnouncement(publisherActor, randomConnectionId);
thenExpectDisconnectClientCalled();
}};
}
@Test
public void sendsConnectionClosedAnnouncementBeforeSystemShutdown() {
final ActorSystem closableActorSystem = ActorSystem.create("AkkaTestSystem-closableActorSystem", TestConstants.CONFIG);
new TestKit(closableActorSystem) {{
final ConnectionId randomConnectionId = TestConstants.createRandomConnectionId();
final Connection connection = createConnectionWithConnectivityAnnouncementTarget(randomConnectionId);
final TestProbe publisherActor = TestProbe.apply(getSystem());
final Props props = DummyClientActor.props(connection, getRef(), getRef(), publisherActor.ref(), delegate);
final ActorRef dummyClientActor = watch(closableActorSystem.actorOf(props));
whenOpeningConnection(dummyClientActor, OpenConnection.of(randomConnectionId, DittoHeaders.empty()),
getRef());
thenExpectConnectClientCalled();
andConnectionSuccessful(dummyClientActor, getRef());
expectMsg(CONNECTED_STATUS);
thenExpectConnectionOpenedAnnouncement(publisherActor, randomConnectionId);
getSystem().terminate();
thenExpectConnectionClosedAnnouncement(publisherActor, randomConnectionId);
}};
closableActorSystem.terminate();
}
private Connection createConnectionWithConnectivityAnnouncementTarget(final ConnectionId connectionId) {
final Target target = ConnectivityModelFactory.newTargetBuilder()
.address("/")
.topics(Collections.singleton(
ConnectivityModelFactory.newFilteredTopicBuilder(Topic.CONNECTION_ANNOUNCEMENTS).build()))
.authorizationContext(
AuthorizationContext.newInstance(DittoAuthorizationContextType.PRE_AUTHENTICATED_CONNECTION,
AuthorizationSubject.newInstance("nginx:ditto")))
.build();
return TestConstants.createConnection(connectionId, target);
}
private void thenExpectConnectionOpenedAnnouncement(final TestProbe publisherActorProbe,
final ConnectionId connectionId) {
final ConnectionOpenedAnnouncement announcement =
ConnectionOpenedAnnouncement.of(connectionId, Instant.now(), DittoHeaders.empty());
thenExpectConnectivityAnnouncement(publisherActorProbe, announcement);
}
private void thenExpectConnectionClosedAnnouncement(final TestProbe publisherActorProbe,
final ConnectionId connectionId) {
final ConnectionClosedAnnouncement announcement =
ConnectionClosedAnnouncement.of(connectionId, Instant.now(), DittoHeaders.empty());
thenExpectConnectivityAnnouncement(publisherActorProbe, announcement);
}
private void thenExpectConnectivityAnnouncement(final TestProbe publisherActorProbe,
final ConnectivityAnnouncement<?> announcement) {
final Adaptable expectedAdaptable = SignalMapperFactory.newConnectivityAnnouncementSignalMapper()
.mapSignalToAdaptable(announcement, TopicPath.Channel.NONE);
final OutboundSignal.MultiMapped outboundSignal =
publisherActorProbe.expectMsgClass(OutboundSignal.MultiMapped.class);
final Adaptable sentAdaptable = outboundSignal.first().getAdaptable();
assertThat(sentAdaptable.getTopicPath()).isEqualTo(expectedAdaptable.getTopicPath());
}
private void thenExpectConnectClientCalled() {
thenExpectConnectClientCalledAfterTimeout(DEFAULT_MESSAGE_TIMEOUT);
}
private void thenExpectDisconnectClientNotCalledForAtLeast(final Duration duration) {
verify(delegate, timeout(duration.toMillis()).times(0))
.doDisconnectClient(any(Connection.class), nullable(ActorRef.class), anyBoolean());
}
private void thenExpectDisconnectClientCalled() {
verify(delegate, timeout(DISCONNECT_TIMEOUT.toMillis())).doDisconnectClient(any(Connection.class),
nullable(ActorRef.class), anyBoolean());
}
private void thenExpectConnectClientCalledAfterTimeout(final Duration connectingTimeout) {
verify(delegate, timeout(connectingTimeout.toMillis() + 200).atLeastOnce())
.doConnectClient(any(Connection.class), nullable(ActorRef.class));
}
private void thenExpectConnectClientCalledOnceAfterTimeout(final Duration connectingTimeout) {
verify(delegate, timeout(connectingTimeout.toMillis() + 200).times(1))
.doConnectClient(any(Connection.class), nullable(ActorRef.class));
}
private void thenExpectNoConnectClientCalled() {
thenExpectNoConnectClientCalledAfterTimeout(Duration.ZERO);
}
private void thenExpectNoConnectClientCalledAfterTimeout(final Duration connectingTimeout) {
verify(delegate, timeout(connectingTimeout.toMillis() + 200).times(0)).doConnectClient(any(Connection.class),
nullable(ActorRef.class));
}
private void thenExpectCleanupResourcesCalled() {
thenExpectCleanupResourcesCalledAfterTimeout(Duration.ZERO);
}
private void thenExpectCleanupResourcesCalledAfterTimeout(final Duration connectingTimeout) {
verify(delegate, timeout(connectingTimeout.toMillis() + 500).atLeastOnce()).cleanupResourcesForConnection();
}
private static void whenOpeningConnection(final ActorRef clientActor, final OpenConnection openConnection,
final ActorRef sender) {
clientActor.tell(openConnection, sender);
}
private static void andClosingConnection(final ActorRef clientActor, final CloseConnection closeConnection,
final ActorRef sender) {
clientActor.tell(closeConnection, sender);
}
private static void andConnectionSuccessful(final ActorRef clientActor, final ActorRef origin) {
clientActor.tell((ClientConnected) () -> Optional.of(origin), clientActor);
}
private static void andConnectionFails(final ActorRef clientActor, final ActorRef origin) {
clientActor.tell(new ImmutableConnectionFailure(null, null, null), clientActor);
}
private static void andDisconnectionSuccessful(final ActorRef clientActor, final ActorRef origin) {
clientActor.tell(new ImmutableClientDisconnected(origin, false), clientActor);
}
private static void andConnectionNotSuccessful(final ActorRef clientActor) {
clientActor.tell(new ImmutableConnectionFailure(null, null, "expected exception"),
clientActor);
}
private static void andStateTimeoutSent(final ActorRef clientActor) {
clientActor.tell(FSM.StateTimeout$.MODULE$, clientActor);
}
private static final class DummyClientActor extends BaseClientActor {
private final ActorRef publisherActor;
private final BaseClientActor delegate;
public DummyClientActor(final Connection connection,
final ActorRef connectionActor,
@Nullable final ActorRef proxyActor,
final ActorRef publisherActor,
final BaseClientActor delegate) {
super(connection, proxyActor, connectionActor);
this.publisherActor = publisherActor;
this.delegate = delegate;
}
/**
* Creates Akka configuration object for this actor.
*
* @param connection the connection.
* @param connectionActor the connectionPersistenceActor which created this client.
* @param proxyActor the actor used to send signals into the ditto cluster.
* @param publisherActor the actor that publishes to external system
* @return the Akka configuration Props object.
*/
public static Props props(final Connection connection, final ActorRef connectionActor,
@Nullable final ActorRef proxyActor,
final ActorRef publisherActor, final BaseClientActor delegate) {
return Props.create(DummyClientActor.class, connection, connectionActor, proxyActor,
publisherActor, delegate);
}
@Override
protected CompletionStage<Status.Status> doTestConnection(final TestConnection testConnectionCommand) {
logger.withCorrelationId(testConnectionCommand)
.withMdcEntry(ConnectivityMdcEntryKey.CONNECTION_ID, testConnectionCommand.getEntityId())
.info("doTestConnection");
return delegate.doTestConnection(testConnectionCommand);
}
@Override
protected void allocateResourcesOnConnection(final ClientConnected clientConnected) {
logger.info("allocateResourcesOnConnection");
delegate.allocateResourcesOnConnection(clientConnected);
}
@Override
protected void cleanupResourcesForConnection() {
logger.info("cleanupResourcesForConnection");
delegate.cleanupResourcesForConnection();
}
@Override
protected void doConnectClient(final Connection connection, @Nullable final ActorRef origin) {
logger.info("doConnectClient");
delegate.doConnectClient(connection, origin);
}
@Override
protected void doDisconnectClient(final Connection connection, @Nullable final ActorRef origin,
final boolean shutdownAfterDisconnect) {
logger.info("doDisconnectClient");
delegate.doDisconnectClient(connection, origin, false);
}
@Override
protected CompletionStage<Status.Status> startPublisherActor() {
return CompletableFuture.completedFuture(DONE);
}
@Nullable
@Override
protected ActorRef getPublisherActor() {
return publisherActor;
}
}
}