-
Notifications
You must be signed in to change notification settings - Fork 214
/
AbstractDittoRootActorTest.java
191 lines (164 loc) · 6.1 KB
/
AbstractDittoRootActorTest.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
/*
* 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.base.service.actors;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.eclipse.ditto.internal.utils.config.raw.RawConfigSupplier;
import org.junit.After;
import org.junit.Test;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.cluster.Cluster;
import org.apache.pekko.japi.pf.ReceiveBuilder;
import org.apache.pekko.stream.Attributes;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Abstract class to test the service root actors.
*/
public abstract class AbstractDittoRootActorTest {
private final List<ActorSystem> actorSystems = new ArrayList<>();
/**
* Get the name of the service with which to load config files.
*
* @return the service name.
*/
protected abstract String serviceName();
/**
* Construct the Props object of the root actor under test.
*
* @param system the actor system.
* @return The props object.
*/
protected abstract Props getRootActorProps(final ActorSystem system);
/**
* Create the actor system. By default, create an actor system with the service default config and random ports
* that forms its own cluster.
*
* @return the actor system.
*/
protected ActorSystem createActorSystem() {
final Map<String, Object> configMap = Map.of(
// bind random ports
"pekko.management.http.port", 0,
"pekko.remote.artery.canonical.port", 0,
"pekko.cluster.seed-nodes", List.of(),
"ditto.http.port", 0,
"ditto.metrics.prometheus.port", 0,
// turn off System.exit(0) in the post-shutdown hook to avoid JVM termination at the end of this test
"pekko.coordinated-shutdown.exit-jvm", "off"
);
final Config config = ConfigFactory.parseMap(overrideConfig())
.withFallback(ConfigFactory.parseMap(configMap))
.withFallback(RawConfigSupplier.of(serviceName()).get());
final ActorSystem system = ActorSystem.create(getClass().getSimpleName(), config);
final Cluster cluster = Cluster.get(system);
cluster.join(cluster.selfAddress());
return system;
}
/**
* Check if the root actor restarted successfully. By default, check that the test kit receives no Terminated
* message.
*
* @param underTest the root actor experiencing restarts.
* @param failingChild the child actor throwing unknown exceptions.
* @param testKit the test kit.
*/
protected void assertRestartSuccess(final ActorRef underTest, final ActorRef failingChild, final TestKit testKit) {
testKit.expectNoMessage();
}
/**
* Whether to disable logging in tests.
* Override in subclasses to debug.
*
* @return whether to disable logging.
*/
protected boolean disableLogging() {
return true;
}
/**
* To be overridden in subclasses. Create root actor with a specific name if given.
*
* @return the root actor name if it is important, or an empty optional otherwise.
*/
protected Optional<String> getRootActorName() {
return Optional.empty();
}
/**
* Service-specific config override.
*
* @return the config override.
*/
protected Map<String, Object> overrideConfig() {
return Map.of();
}
@After
public void terminateCreatedActorSystems() {
for (final ActorSystem system : actorSystems) {
TestKit.shutdownActorSystem(system);
}
actorSystems.clear();
}
@Test
public void testRootActorRestart() {
final ActorSystem system = createAndRememberForCleanUp();
if (disableLogging()) {
system.eventStream().setLogLevel(Attributes.logLevelOff());
}
new TestKit(system) {{
final ActorRef underTest = getRootActorName()
.map(name -> watch(system.actorOf(getRootActorProps(system), name)))
.orElseGet(() -> watch(system.actorOf(getRootActorProps(system))));
final String failingChild = "failingChild";
underTest.tell(new StartChildActor(
Props.create(ThrowingActor.class, () -> new ThrowingActor(getRef())),
failingChild
), getRef());
final ActorRef throwingActor = expectMsgClass(Duration.ofSeconds(100L), ActorRef.class);
throwingActor.tell(new UnknownException(), getRef());
assertRestartSuccess(underTest, throwingActor, this);
}};
}
private ActorSystem createAndRememberForCleanUp() {
final ActorSystem system = createActorSystem();
actorSystems.add(system);
return system;
}
/**
* An exception not handled in the supervising strategy of DittoRootActor.
*/
private static final class UnknownException extends RuntimeException {}
/**
* An actor that throws each runtime exception it receives.
*/
protected static final class ThrowingActor extends AbstractActor {
public ThrowingActor() {}
public ThrowingActor(final ActorRef toNotify) {
toNotify.tell(getSelf(), getSelf());
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(RuntimeException.class, e -> {
throw e;
})
.build();
}
}
}