-
Notifications
You must be signed in to change notification settings - Fork 214
/
PersistencePingActorTest.java
124 lines (104 loc) · 5.23 KB
/
PersistencePingActorTest.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
/*
* Copyright (c) 2021 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.internal.utils.persistentactors;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.Optional;
import java.util.concurrent.TimeUnit;
import org.eclipse.ditto.policies.model.PolicyId;
import org.eclipse.ditto.internal.utils.pekko.PingCommand;
import org.eclipse.ditto.internal.utils.persistentactors.config.DefaultPingConfig;
import org.eclipse.ditto.internal.utils.persistentactors.config.PingConfig;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.testkit.TestProbe;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Tests {@link PersistencePingActor}.
*/
public final class PersistencePingActorTest {
public static final Config CONFIG = ConfigFactory.load("persistence-ping-test");
private static ActorSystem actorSystem;
@BeforeClass
public static void setUp() {
actorSystem = ActorSystem.create("PekkoTestSystem", CONFIG);
}
@AfterClass
public static void tearDown() {
TestKit.shutdownActorSystem(actorSystem, scala.concurrent.duration.Duration.apply(5, TimeUnit.SECONDS), false);
}
@Test
public void conversionBetweenCorrelationIdAndPersistenceIdIsOneToOne() {
final PolicyId id1 = PolicyId.of("foo:random-ID-jbxlkeimx");
final PolicyId id2 = PolicyId.of("foo:differentId");
final Optional<String> outputId1 = PersistencePingActor.toPersistenceId(
PersistencePingActor.toCorrelationId(id1));
final Optional<String> outputId2 = PersistencePingActor.toPersistenceId(
PersistencePingActor.toCorrelationId(id2));
assertThat(outputId1).contains(id1.toString());
assertThat(outputId2).contains(id2.toString());
assertThat(outputId1).isNotEqualTo(outputId2);
}
@Test
public void pingPersistenceActors() {
new TestKit(actorSystem) {{
final TestProbe probe = new TestProbe(actorSystem);
final PingConfig pingConfig =
DefaultPingConfig.of(actorSystem.settings().config().getConfig("ditto.test"));
final PolicyId persistenceId1 = PolicyId.of("some:pid-1");
final PolicyId persistenceId2 = PolicyId.of("some:pid-2");
final PolicyId persistenceId3 = PolicyId.of("some:pid-3");
final Props props = PersistencePingActor.propsForTests(probe.ref(), pingConfig,
() -> Source.from(Arrays.asList(
persistenceId1.getEntityType() + ":" + persistenceId1,
persistenceId1.getEntityType() + ":" + persistenceId2,
persistenceId1.getEntityType() + ":" + persistenceId3)));
actorSystem.actorOf(props);
final PingCommand msg1 = probe.expectMsgClass(PingCommand.class);
assertThat((CharSequence) msg1.getEntityId()).isEqualTo(persistenceId1);
assertThat(msg1.getCorrelationId()).contains(PersistencePingActor.toCorrelationId(persistenceId1));
final PingCommand msg2 = probe.expectMsgClass(PingCommand.class);
assertThat((CharSequence) msg2.getEntityId()).isEqualTo(persistenceId2);
assertThat(msg2.getCorrelationId()).contains(PersistencePingActor.toCorrelationId(persistenceId2));
final PingCommand msg3 = probe.expectMsgClass(PingCommand.class);
assertThat((CharSequence) msg3.getEntityId()).isEqualTo(persistenceId3);
assertThat(msg3.getCorrelationId()).contains(PersistencePingActor.toCorrelationId(persistenceId3));
}};
}
@Test
public void testPersistenceActorIsNotPingedTwice() {
new TestKit(actorSystem) {{
final TestProbe probe = new TestProbe(actorSystem);
final PingConfig pingConfig =
DefaultPingConfig.of(actorSystem.settings().config().getConfig("ditto.test"));
final String persistenceId1 = "some:pid-1";
final String persistenceId2 = "some:pid-2";
final String persistenceId3 = "some:pid-3";
final Props props = PersistencePingActor.propsForTests(probe.ref(), pingConfig,
() -> Source.from(Arrays.asList(persistenceId1, persistenceId2, persistenceId3)));
final ActorRef reconnectActor = actorSystem.actorOf(props);
reconnectActor.tell(PersistencePingActor.InternalMessages.START_PINGING, getRef());
probe.expectMsgClass(PingCommand.class);
probe.expectMsgClass(PingCommand.class);
probe.expectMsgClass(PingCommand.class);
probe.expectNoMessage();
}};
}
}