-
Notifications
You must be signed in to change notification settings - Fork 214
/
SubUpdaterTest.java
147 lines (127 loc) · 6.29 KB
/
SubUpdaterTest.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
/*
* Copyright (c) 2022 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.pubsub.actors;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import org.eclipse.ditto.internal.utils.pubsub.LiteralDDataProvider;
import org.eclipse.ditto.internal.utils.pubsub.config.PubSubConfig;
import org.eclipse.ditto.internal.utils.pubsub.ddata.DDataReader;
import org.eclipse.ditto.internal.utils.pubsub.ddata.DDataWriter;
import org.eclipse.ditto.internal.utils.pubsub.ddata.compressed.CompressedDData;
import org.eclipse.ditto.internal.utils.pubsub.ddata.literal.AbstractConfigAwareDDataProvider;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import akka.cluster.Cluster;
import akka.cluster.ddata.ORMultiMap;
import akka.cluster.ddata.Replicator;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
/**
* Tests {@link AckUpdater}.
*/
public final class SubUpdaterTest {
private static final AbstractConfigAwareDDataProvider PROVIDER =
LiteralDDataProvider.of("dc-default", "topic");
private ActorSystem system;
@Before
public void setUpCluster() throws Exception {
final CountDownLatch latch = new CountDownLatch(1);
system = ActorSystem.create("actorSystem", getTestConf());
final Cluster cluster1 = Cluster.get(system);
cluster1.registerOnMemberUp(latch::countDown);
cluster1.join(cluster1.selfAddress());
latch.await();
}
@After
public void shutdownCluster() {
TestKit.shutdownActorSystem(system);
}
@Test
public void clusterStateOutOfSync() {
final var system2 = ActorSystem.create("system2", getTestConf());
try {
new TestKit(system) {{
// GIVEN: distributed data contains entry for nonexistent cluster member
final var subscriber = TestProbe.apply(system);
final var config = PubSubConfig.of(system);
final var unknownActor = TestProbe.apply(system2).ref();
final var cluster2 = Cluster.get(system2);
final var addressMap = Map.of(mockActorRefWithAddress(unknownActor, cluster2), "unknown-value");
final CompressedDData ddata = mockDistributedData(addressMap);
final ActorRef underTest = system.actorOf(SubUpdater.props(config, subscriber.ref(), ddata));
// WHEN: AckUpdater is requested to sync against the cluster state
underTest.tell(ClusterStateSyncBehavior.Control.SYNC_CLUSTER_STATE, ActorRef.noSender());
// THEN: AckUpdater removes the extraneous entry
Mockito.verify(ddata.getReader(), Mockito.timeout(5000))
.getAllShards(eq((Replicator.ReadConsistency) Replicator.readLocal()));
Mockito.verify(ddata.getWriter(), Mockito.timeout(5000))
.removeAddress(eq(cluster2.selfAddress()),
eq((Replicator.WriteConsistency) Replicator.writeLocal()));
}};
} finally {
TestKit.shutdownActorSystem(system2);
}
}
@Test
public void clusterStateInSync() {
new TestKit(system) {{
// GIVEN: distributed data contains entry for the current cluster member and no extraneous entries
final var subscriberRef = TestProbe.apply(system).ref();
final var cluster = Cluster.get(system);
final var config = PubSubConfig.of(system);
final var addressMap = Map.of(mockActorRefWithAddress(subscriberRef, cluster), "unknown-value");
final CompressedDData ddata = mockDistributedData(addressMap);
final ActorRef underTest = system.actorOf(SubUpdater.props(config, subscriberRef, ddata));
// WHEN: AckUpdater is requested to sync against the cluster state
underTest.tell(ClusterStateSyncBehavior.Control.SYNC_CLUSTER_STATE, ActorRef.noSender());
// THEN: AckUpdater does not modify ddata more than needed
Mockito.verify(ddata.getReader(), Mockito.timeout(5000))
.getAllShards(eq((Replicator.ReadConsistency) Replicator.readLocal()));
Mockito.verify(ddata.getWriter(), Mockito.never()).put(any(), any(), any());
Mockito.verify(ddata.getWriter(), Mockito.never()).removeAddress(any(), any());
}};
}
private Config getTestConf() {
return ConfigFactory.load("pubsub-factory-test.conf");
}
private static ActorRef mockActorRefWithAddress(final ActorRef originalRef, final Cluster cluster) {
final var mock = Mockito.mock(ActorRef.class);
Mockito.when(mock.path()).thenReturn(cluster.remotePathOf(originalRef));
return mock;
}
@SuppressWarnings({"rawtypes", "unchecked"})
private static CompressedDData mockDistributedData(final Map<ActorRef, String> result) {
final ORMultiMap map = Mockito.mock(ORMultiMap.class);
Mockito.when(map.getEntries()).thenReturn(result);
final var mock = Mockito.mock(CompressedDData.class);
final var reader = Mockito.mock(DDataReader.class);
final var writer = Mockito.mock(DDataWriter.class);
Mockito.when(mock.getReader()).thenReturn(reader);
Mockito.when(mock.getWriter()).thenReturn(writer);
Mockito.when(reader.get(any(), any())).thenReturn(CompletableFuture.completedStage(Optional.of(map)));
Mockito.when(reader.getAllShards(any())).thenReturn(CompletableFuture.completedStage(List.of(map)));
Mockito.when(writer.put(any(), any(), any())).thenReturn(CompletableFuture.completedStage(null));
return mock;
}
}