-
Notifications
You must be signed in to change notification settings - Fork 214
/
CreditsTest.java
171 lines (149 loc) · 7.1 KB
/
CreditsTest.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
/*
* 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.cleanup;
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.time.Duration;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.LongAccumulator;
import org.bson.Document;
import org.eclipse.ditto.internal.utils.persistence.mongo.streaming.MongoReadJournal;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import com.mongodb.client.result.DeleteResult;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.event.Logging;
import org.apache.pekko.japi.Pair;
import org.apache.pekko.stream.Attributes;
import org.apache.pekko.stream.Materializer;
import org.apache.pekko.stream.SystemMaterializer;
import org.apache.pekko.stream.javadsl.Keep;
import org.apache.pekko.stream.javadsl.Source;
import org.apache.pekko.stream.testkit.TestPublisher;
import org.apache.pekko.stream.testkit.TestSubscriber;
import org.apache.pekko.stream.testkit.javadsl.TestSink;
import org.apache.pekko.stream.testkit.javadsl.TestSource;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Tests {@link Credits}.
*/
public final class CreditsTest {
private final ActorSystem actorSystem = ActorSystem.create();
private Materializer materializer;
private LongAccumulator mockTimer;
@Before
public void init() {
materializer = SystemMaterializer.get(actorSystem).materializer();
mockTimer = mock(LongAccumulator.class);
}
@After
public void cleanUp() {
TestKit.shutdownActorSystem(actorSystem);
}
@Test
public void requestElementsUpstreamOneAtATime() {
when(mockTimer.getThenReset()).thenReturn(0L);
final Credits underTest = new Credits(getFastCreditConfig(4), mockTimer);
final var probePair = materializeProbePair(underTest);
final var sourceProbe = probePair.first();
final var sinkProbe = probePair.second();
sinkProbe.ensureSubscription();
sinkProbe.request(12);
assertThat(sourceProbe.expectRequest()).isEqualTo(1); // initial request due to input buffer
sourceProbe.sendNext(0);
assertThat(sourceProbe.expectRequest()).isEqualTo(1);
sourceProbe.expectNoMessage();
}
@Test
public void noElementRequestedWithoutCredit() {
doAnswer(inv -> 1001L).when(mockTimer).getThenReset();
final Credits underTest = new Credits(getFastCreditConfig(2), mockTimer);
final var probePair = materializeProbePair(underTest);
final var sourceProbe = probePair.first();
final var sinkProbe = probePair.second();
sinkProbe.ensureSubscription();
sinkProbe.request(5);
assertThat(sourceProbe.expectRequest()).isEqualTo(1L); // initial input buffer
sourceProbe.expectNoMessage();
doAnswer(inv -> 0L).when(mockTimer).getThenReset();
sourceProbe.sendNext(0);
assertThat(sourceProbe.expectRequest()).isEqualTo(1L); // credit 1/2
doAnswer(inv -> 1001L).when(mockTimer).getThenReset();
sourceProbe.sendNext(0);
assertThat(sourceProbe.expectRequest()).isEqualTo(1L); // credit 2/2
sourceProbe.sendNext(0);
sourceProbe.expectNoMessage();
}
@Test
public void onePersistenceWriteAllowedPerCredit() {
final var mongoReadJournal = mock(MongoReadJournal.class);
final var opsCounter = new AtomicInteger(0);
when(mongoReadJournal.getNewestSnapshotsAbove(any(), anyInt(), eq(true), any(), any()))
.thenReturn(Source.single(new Document().append("_id", "thing:p:id")
.append("__lifecycle", "DELETED")
.append("sn", 50L)));
when(mongoReadJournal.getSmallestEventSeqNo(any())).thenReturn(Source.single(Optional.of(30L)));
when(mongoReadJournal.getSmallestSnapshotSeqNo(any())).thenReturn(Source.single(Optional.of(40L)));
doAnswer(invocation -> {
opsCounter.incrementAndGet();
return Source.single(DeleteResult.acknowledged(
invocation.<Long>getArgument(1) * 100L + invocation.<Long>getArgument(2)));
}).when(mongoReadJournal).deleteEvents(any(), anyLong(), anyLong());
doAnswer(invocation -> {
opsCounter.incrementAndGet();
return Source.single(DeleteResult.acknowledged(
invocation.<Long>getArgument(1) * 1000L + invocation.<Long>getArgument(2) * 10L));
}).when(mongoReadJournal).deleteSnapshots(any(), anyLong(), anyLong());
// mock timer permits 1 batch of credit, after which no credit is given out
final var mockTimerResult = new AtomicLong(0L);
doAnswer(inv -> mockTimerResult.getAndSet(1001L)).when(mockTimer).getThenReset();
final var cleanup = new Cleanup(mongoReadJournal, materializer, () -> Pair.create(0, 1),
Duration.ZERO, 1, 4, true);
final var underTest = new Credits(getFastCreditConfig(4), mockTimer);
final var log = Logging.getLogger(actorSystem, this);
final var sinkProbe = underTest.regulate(cleanup.getCleanupStream(""), log)
.flatMapConcat(x -> x)
.toMat(TestSink.probe(actorSystem), Keep.right())
.withAttributes(Attributes.inputBuffer(1, 1))
.run(materializer);
sinkProbe.ensureSubscription();
sinkProbe.request(2L);
sinkProbe.expectNextN(2L);
sinkProbe.expectNoMessage();
assertThat(opsCounter.get()).isEqualTo(3); // 2 requested, 1 buffered
sinkProbe.request(10L);
sinkProbe.expectNextN(2L);
sinkProbe.expectNoMessage();
assertThat(opsCounter.get()).isEqualTo(4); // 4 credit given out in total
}
private Pair<TestPublisher.Probe<Object>, TestSubscriber.Probe<Object>> materializeProbePair(
final Credits credits) {
return credits.regulate(TestSource.probe(actorSystem), Logging.getLogger(actorSystem, this))
.toMat(TestSink.probe(actorSystem), Keep.both())
.withAttributes(Attributes.inputBuffer(1, 1))
.run(materializer);
}
private static CleanupConfig getFastCreditConfig(final int creditPerBatch) {
return new DefaultCleanupConfig(true, Duration.ZERO, Duration.ZERO, Duration.ofMillis(100), Duration.ofNanos(1000),
creditPerBatch, 100, 100, false);
}
}