/
BatchSupervisorActorTest.java
291 lines (241 loc) · 12.4 KB
/
BatchSupervisorActorTest.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
/*
* 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.concierge.batch.actors;
import static akka.testkit.JavaTestKit.shutdownActorSystem;
import static org.eclipse.ditto.signals.commands.base.assertions.CommandAssertions.assertThat;
import java.util.Arrays;
import java.util.UUID;
import org.eclipse.ditto.model.base.headers.DittoHeaders;
import org.eclipse.ditto.model.things.Feature;
import org.eclipse.ditto.model.things.Thing;
import org.eclipse.ditto.services.models.concierge.ConciergeWrapper;
import org.eclipse.ditto.services.utils.akka.JavaTestProbe;
import org.eclipse.ditto.services.utils.test.Retry;
import org.eclipse.ditto.signals.base.JsonParsableRegistry;
import org.eclipse.ditto.signals.base.Signal;
import org.eclipse.ditto.signals.commands.base.Command;
import org.eclipse.ditto.signals.commands.base.GlobalCommandRegistry;
import org.eclipse.ditto.signals.commands.batch.ExecuteBatch;
import org.eclipse.ditto.signals.commands.batch.ExecuteBatchResponse;
import org.eclipse.ditto.signals.commands.things.modify.ModifyFeature;
import org.eclipse.ditto.signals.commands.things.modify.ModifyFeatureResponse;
import org.eclipse.ditto.signals.commands.things.modify.ModifyThing;
import org.eclipse.ditto.signals.commands.things.modify.ModifyThingResponse;
import org.eclipse.ditto.signals.events.base.assertions.EventAssertions;
import org.eclipse.ditto.signals.events.batch.BatchExecutionFinished;
import org.eclipse.ditto.signals.events.batch.BatchExecutionStarted;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.cluster.pubsub.DistributedPubSub;
import akka.cluster.pubsub.DistributedPubSubMediator;
import akka.japi.pf.ReceiveBuilder;
import akka.routing.ConsistentHashingRouter;
/**
* Unit test for {@link BatchSupervisorActor}.
*/
@SuppressWarnings({"squid:S3599", "squid:S1171"})
public final class BatchSupervisorActorTest {
private static final Config CONFIG = ConfigFactory.load("test");
public static final String THING1_ID = "com.bosch.iot.things.test:thing1";
public static final String THING2_ID = "com.bosch.iot.things.test:thing2";
private static ActorSystem actorSystem;
private static ActorRef pubSubMediator;
private static ActorRef underTest;
private static ActorRef conciergeForwarder;
@BeforeClass
public static void setUpActorSystem() {
actorSystem = ActorSystem.create("AkkaTestSystem", CONFIG);
pubSubMediator = DistributedPubSub.get(actorSystem).mediator();
final ActorRef sharedRegionProxy = actorSystem.actorOf(Props.create(SharedRegionProxyMock.class));
conciergeForwarder = actorSystem.actorOf(ConciergeForwarderActorMock.props(sharedRegionProxy),
ConciergeForwarderActorMock.ACTOR_NAME);
underTest = createBatchSupervisorActor();
}
/** */
@AfterClass
public static void tearDownActorSystem() {
shutdownActorSystem(actorSystem);
}
/** */
@Test
public void batchExecutionWorksAsExpected() {
new JavaTestProbe(actorSystem) {
{
subscribeToEvents(this, BatchExecutionFinished.TYPE);
final String batchId = randomBatchId();
final ExecuteBatch executeBatch =
ExecuteBatch.of(batchId, Arrays.asList(
ModifyThing.of(THING1_ID,
Thing.newBuilder().setId(THING1_ID).build(),
null,
DittoHeaders.newBuilder().build()),
ModifyThing.of(THING2_ID,
Thing.newBuilder().setId(THING2_ID).build(),
null,
DittoHeaders.newBuilder().build())),
DittoHeaders.newBuilder().correlationId(batchId).build());
underTest.tell(executeBatch, ref());
expectMsgAllClass()
.of(ExecuteBatchResponse.class, executeBatchResponse ->
assertThat(executeBatchResponse).hasCorrelationId(batchId))
.of(BatchExecutionFinished.class, batchExecutionFinished ->
EventAssertions.assertThat(batchExecutionFinished).hasCorrelationId(batchId))
.run();
}
};
}
/** */
@Test
public void batchExecutionResumesAfterRecovery() {
new JavaTestProbe(actorSystem) {
{
subscribeToEvents(this, BatchExecutionStarted.TYPE, BatchExecutionFinished.TYPE);
// batch1
final String batchId1 = randomBatchId();
final ExecuteBatch executeBatch1 =
ExecuteBatch.of(batchId1, Arrays.asList(
ModifyThing.of(THING1_ID,
Thing.newBuilder().setId(THING1_ID).build(),
null,
DittoHeaders.newBuilder().build()),
ModifyThing.of(THING2_ID,
Thing.newBuilder().setId(THING2_ID).build(),
null,
DittoHeaders.newBuilder().build())),
DittoHeaders.newBuilder().correlationId(batchId1).build());
underTest.tell(executeBatch1, ref());
final ExecuteBatchResponse executeBatchResponse1 = expectMsgClass(ExecuteBatchResponse.class);
assertThat(executeBatchResponse1).hasCorrelationId(batchId1);
final BatchExecutionStarted batchExecutionStarted1 = expectMsgClass(BatchExecutionStarted.class);
EventAssertions.assertThat(batchExecutionStarted1).hasCorrelationId(batchId1);
final BatchExecutionFinished batchExecutionFinished1 = expectMsgClass(BatchExecutionFinished.class);
EventAssertions.assertThat(batchExecutionFinished1).hasCorrelationId(batchId1);
// batch2
final String batchId2 = randomBatchId();
final ExecuteBatch executeBatch2 =
ExecuteBatch.of(batchId2, Arrays.asList(
ModifyFeature.of(
THING1_ID,
Feature.newBuilder().withId("fluxCompensator").build(),
DittoHeaders.newBuilder().build()),
ModifyFeature.of(
THING2_ID,
Feature.newBuilder().withId("fluxCompensator").build(),
DittoHeaders.newBuilder().build())),
DittoHeaders.newBuilder().correlationId(batchId2).build());
underTest.tell(executeBatch2, ref());
final ExecuteBatchResponse executeBatchResponse2 = expectMsgClass(ExecuteBatchResponse.class);
assertThat(executeBatchResponse2).hasCorrelationId(batchId2);
final BatchExecutionStarted batchExecutionStarted2 = expectMsgClass(BatchExecutionStarted.class);
EventAssertions.assertThat(batchExecutionStarted2).hasCorrelationId(batchId2);
// terminate and restart - only batch2 must resume execution and deliver an event
terminate(this, underTest);
// sometimes the actor name is not unique, try for a while
underTest = Retry.untilSuccess(BatchSupervisorActorTest::createBatchSupervisorActor);
final BatchExecutionFinished batchExecutionFinished2 = expectMsgClass(BatchExecutionFinished.class);
EventAssertions.assertThat(batchExecutionFinished2).hasCorrelationId(batchId2);
expectNoMsg();
}
};
}
private static ActorRef createBatchSupervisorActor() {
final String name = BatchSupervisorActor.ACTOR_NAME;
return actorSystem.actorOf(BatchSupervisorActor.props(pubSubMediator, conciergeForwarder), name);
}
private static void terminate(final JavaTestProbe javaTestProbe, final ActorRef underTest) {
javaTestProbe.watch(underTest);
actorSystem.stop(underTest);
javaTestProbe.expectTerminated(underTest);
}
private static String randomBatchId() {
return "BatchSupervisorActorTest-" + UUID.randomUUID().toString();
}
private static void subscribeToEvents(final JavaTestProbe javaTestProbe, final String... events) {
final String group = "BatchSupervisorActorTest" + UUID.randomUUID().toString();
for (final String e : events) {
pubSubMediator.tell(new DistributedPubSubMediator.Subscribe(e, group, javaTestProbe.ref()),
javaTestProbe.ref());
javaTestProbe.expectMsgClass(DistributedPubSubMediator.SubscribeAck.class);
}
}
private static final class SharedRegionProxyMock extends AbstractActor {
private int modifyFeatureCount = 0;
private final JsonParsableRegistry<? extends Command> commandRegistry = GlobalCommandRegistry.getInstance();
SharedRegionProxyMock() {
}
@Override
public Receive createReceive() {
return ReceiveBuilder.create()
.match(ConsistentHashingRouter.ConsistentHashableEnvelope.class, this::extractCommandFromEnvelop)
.matchAny(this::unhandled)
.build();
}
private void extractCommandFromEnvelop(ConsistentHashingRouter.ConsistentHashableEnvelope envelope) {
final Object obj = envelope.message();
if (obj instanceof ModifyThing) {
final ModifyThing command = (ModifyThing) obj;
getSender().tell(ModifyThingResponse.modified(command.getId(),
command.getDittoHeaders()), getSelf());
}
else if (obj instanceof ModifyFeature) {
final ModifyFeature command = (ModifyFeature) obj;
if (command.getDittoHeaders().isDryRun()) {
respondToModifyFeature(command);
} else {
modifyFeatureCount++;
if (modifyFeatureCount > 2) {
respondToModifyFeature(command);
}
}
}
}
private void respondToModifyFeature(final ModifyFeature command) {
final ModifyFeatureResponse response = ModifyFeatureResponse.modified(command.getId(), command.getFeatureId(),
command.getDittoHeaders());
getSender().tell(response, getSelf());
}
}
public static final class ConciergeForwarderActorMock extends AbstractActor {
public static final String ACTOR_NAME = "conciergeForwarder";
private final ActorRef enforcerActor;
@SuppressWarnings("unused")
private ConciergeForwarderActorMock(final ActorRef enforcerActor) {
this.enforcerActor = enforcerActor;
}
/**
* Creates Akka configuration object Props for this actor.
*
* @param enforcerActor the ActorRef of the enforcerActor.
* @return the Akka configuration Props object.
*/
public static Props props(final ActorRef enforcerActor) {
return Props.create(ConciergeForwarderActorMock.class, enforcerActor);
}
@Override
public AbstractActor.Receive createReceive() {
return ReceiveBuilder.create()
.match(Signal.class, signal -> {
final Object msg = ConciergeWrapper.wrapForEnforcerRouter(signal);
enforcerActor.tell(msg, getSender());
})
.build();
}
}
}