-
Notifications
You must be signed in to change notification settings - Fork 215
/
SubscriptionManagerTest.java
227 lines (197 loc) · 9.25 KB
/
SubscriptionManagerTest.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
/*
* Copyright (c) 2020 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.search;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import org.eclipse.ditto.base.model.exceptions.DittoInternalErrorException;
import org.eclipse.ditto.base.model.headers.DittoHeaders;
import org.eclipse.ditto.json.JsonArray;
import org.eclipse.ditto.json.JsonFieldSelector;
import org.eclipse.ditto.json.JsonObject;
import org.eclipse.ditto.things.model.Thing;
import org.eclipse.ditto.thingsearch.api.commands.sudo.SudoStreamThings;
import org.eclipse.ditto.thingsearch.model.signals.commands.subscription.CancelSubscription;
import org.eclipse.ditto.thingsearch.model.signals.commands.subscription.CreateSubscription;
import org.eclipse.ditto.thingsearch.model.signals.commands.subscription.RequestFromSubscription;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionComplete;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionCreated;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionFailed;
import org.eclipse.ditto.thingsearch.model.signals.events.SubscriptionHasNextPage;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import akka.actor.ActorRef;
import akka.actor.ActorSelection;
import akka.actor.ActorSystem;
import akka.actor.Props;
import akka.pattern.AskTimeoutException;
import akka.stream.Attributes;
import akka.stream.Materializer;
import akka.stream.SourceRef;
import akka.stream.SystemMaterializer;
import akka.stream.javadsl.Source;
import akka.stream.javadsl.StreamRefs;
import akka.testkit.TestProbe;
import akka.testkit.javadsl.TestKit;
import scala.concurrent.duration.FiniteDuration;
/**
* Tests {@link SubscriptionManager}.
*/
public final class SubscriptionManagerTest {
private ActorSystem actorSystem;
private Materializer materializer;
private TestProbe pubSubMediatorProbe;
private TestProbe edgeCommandForwarderProbe;
@Before
public void setUp() {
actorSystem = ActorSystem.create();
materializer = SystemMaterializer.get(actorSystem).materializer();
pubSubMediatorProbe = TestProbe.apply("pubSubMediator", actorSystem);
edgeCommandForwarderProbe = TestProbe.apply("edgeCommandForwarder", actorSystem);
}
@After
public void shutdown() {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
actorSystem = null;
}
}
@Test
public void noSuchSubscription() {
new TestKit(actorSystem) {{
final ActorRef underTest = createSubscriptionManager();
underTest.tell(RequestFromSubscription.of("nonexistentSid", 1L, DittoHeaders.empty()), getRef());
expectMsgClass(SubscriptionFailed.class);
}};
}
@Test
public void illegalPageSize() {
new TestKit(actorSystem) {{
final ActorRef underTest = createSubscriptionManager();
underTest.tell(CreateSubscription.of(null, "size(0)", null, null, DittoHeaders.empty()), getRef());
underTest.tell(request(expectMsgClass(SubscriptionCreated.class).getSubscriptionId()), getRef());
expectMsgClass(SubscriptionFailed.class);
}};
}
@Test
public void illegalDemand() {
new TestKit(actorSystem) {{
final ActorRef underTest = createSubscriptionManager();
underTest.tell(createSubscription(1), getRef());
final String sid = expectMsgClass(SubscriptionCreated.class).getSubscriptionId();
underTest.tell(RequestFromSubscription.of(sid, 0L, DittoHeaders.empty()), getRef());
expectMsgClass(SubscriptionFailed.class);
}};
}
@Test
public void parallelSessions() {
// Suppressing logs due to stacktrace for test4. Comment out to see logs.
actorSystem.eventStream().setLogLevel(Attributes.logLevelOff());
final ActorRef underTest = createSubscriptionManager();
final TestProbe probe1 = TestProbe.apply("test1", actorSystem);
final TestProbe probe2 = TestProbe.apply("test2", actorSystem);
final TestProbe probe3 = TestProbe.apply("test3", actorSystem);
final TestProbe probe4 = TestProbe.apply("test4", actorSystem);
// expect success with results
underTest.tell(createSubscription(1), probe1.ref());
// expect success with prefix
final String prefix = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
underTest.tell(createSubscription(2).setPrefix(prefix), probe2.ref());
// expect cancellation
underTest.tell(createSubscription(3), probe3.ref());
// expect upstream failure
underTest.tell(createSubscription(4), probe4.ref());
final String sid1 = probe1.expectMsgClass(SubscriptionCreated.class).getSubscriptionId();
final String sid2 = probe2.expectMsgClass(SubscriptionCreated.class).getSubscriptionId();
final String sid3 = probe3.expectMsgClass(SubscriptionCreated.class).getSubscriptionId();
final String sid4 = probe4.expectMsgClass(SubscriptionCreated.class).getSubscriptionId();
final List<?> sources = List.of(
Source.single("t:1"),
Source.single("t:2"),
Source.from(List.of("t:3", "t:4")),
new AskTimeoutException("mock error")
);
underTest.tell(request(sid1), probe1.ref());
underTest.tell(request(sid2), probe2.ref());
underTest.tell(request(sid3), probe3.ref());
underTest.tell(request(sid4), probe4.ref());
// there should be no upstream request until downstream requests.
for (int i = 0; i < 4; ++i) {
final SudoStreamThings sudoStreamThings = edgeCommandForwarderProbe.expectMsgClass(SudoStreamThings.class);
final ActorRef sender = edgeCommandForwarderProbe.sender();
final Object source = sources.get(getTag(sudoStreamThings) - 1);
if (source instanceof Source) {
final SourceRef<?> sourceRef = ((Source<?, ?>) source).runWith(StreamRefs.sourceRef(), materializer);
sender.tell(sourceRef, ActorRef.noSender());
} else {
sender.tell(source, ActorRef.noSender());
}
}
probe1.expectMsg(hasNext(sid1, "t:1"));
probe1.expectMsg(subscriptionComplete(sid1));
probe2.expectMsg(hasNext(sid2, "t:2"));
probe2.expectMsg(subscriptionComplete(sid2));
probe3.expectMsg(hasNext(sid3, "t:3"));
underTest.tell(CancelSubscription.of(sid3, DittoHeaders.empty()), probe3.ref());
assertThat(probe4.expectMsgClass(SubscriptionFailed.class).getError())
.isInstanceOf(DittoInternalErrorException.class);
CompletableFuture.allOf(
CompletableFuture.runAsync(
() -> probe3.expectNoMessage(FiniteDuration.apply(250L, TimeUnit.MILLISECONDS))),
CompletableFuture.runAsync(
() -> probe4.expectNoMessage(FiniteDuration.apply(250L, TimeUnit.MILLISECONDS)))
).join();
}
private SubscriptionComplete subscriptionComplete(final String subscriptionId) {
return SubscriptionComplete.of(subscriptionId, DittoHeaders.empty());
}
private SubscriptionHasNextPage hasNext(final String subscriptionId, final String thingId) {
return SubscriptionHasNextPage.of(subscriptionId,
JsonArray.of(JsonObject.newBuilder().set(Thing.JsonFields.ID, thingId).build()),
DittoHeaders.empty());
}
private static RequestFromSubscription request(final String subscriptionId) {
return RequestFromSubscription.of(subscriptionId, 1L, DittoHeaders.empty());
}
private int getTag(final SudoStreamThings sudoStreamThings) {
return sudoStreamThings.getFilter()
.map(filter -> {
int i = "exists(attributes/tag".length();
return filter.substring(i, i + 1);
})
.map(Integer::valueOf)
.orElse(0);
}
private static CreateSubscription createSubscription(final int i) {
return CreateSubscription.of("exists(attributes/tag" + i + ")",
"size(1)",
JsonFieldSelector.newInstance(Thing.JsonFields.ID.getPointer()),
null,
DittoHeaders.empty()
);
}
/**
* Create a subscription manager without retries to test upstream failures quickly.
*
* @return reference of the subscription manager.
*/
private ActorRef createSubscriptionManager() {
return actorSystem.actorOf(Props.create(SubscriptionManager.class, () ->
new SubscriptionManager(Duration.ofMinutes(5L), pubSubMediatorProbe.ref(),
ActorSelection.apply(edgeCommandForwarderProbe.ref(), ""), materializer))
);
}
}