-
Notifications
You must be signed in to change notification settings - Fork 214
/
BackOffActorTest.java
190 lines (145 loc) · 6.93 KB
/
BackOffActorTest.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
/*
* Copyright (c) 2019 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.connectivity.service.messaging.backoff;
import static org.assertj.core.api.Assertions.assertThat;
import java.time.Duration;
import org.eclipse.ditto.connectivity.service.config.BackOffConfig;
import org.eclipse.ditto.connectivity.service.config.TimeoutConfig;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import com.typesafe.config.Config;
import com.typesafe.config.ConfigFactory;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.testkit.javadsl.TestKit;
/**
* Unit test for {@link BackOffActor}.
*/
public final class BackOffActorTest {
private static final Config CONFIG = ConfigFactory.load("test");
private static final Duration BACK_OFF_DURATION = Duration.ofSeconds(2L);
private static final Duration DOUBLE_BACK_OFF_DURATION = BACK_OFF_DURATION.multipliedBy(2L);
private static final Duration HALF_BACK_OFF_DURATION = BACK_OFF_DURATION.dividedBy(2L);
private static ActorSystem actorSystem;
@BeforeClass
public static void setUp() {
actorSystem = ActorSystem.create("AkkaTestSystem", CONFIG);
}
@AfterClass
public static void tearDown() {
if (actorSystem != null) {
TestKit.shutdownActorSystem(actorSystem);
}
}
@Test
public void receiveMessageAfterBackOff() {
new TestKit(actorSystem) {{
final ActorRef underTest = childActorOf(BackOffActor.props(createBackOffConfig()));
final String message = "I expect to receive this after backOff";
underTest.tell(BackOffActor.createBackOffWithAnswerMessage(message), getRef());
// verify there happens a back off
expectNoMessage(HALF_BACK_OFF_DURATION);
// verify we receive the answer afterwards
expectMsg(BACK_OFF_DURATION, message);
}};
}
@Test
public void backOffDuringBackOffWillDelayMessage() {
new TestKit(actorSystem) {{
final ActorRef underTest = childActorOf(BackOffActor.props(createBackOffConfig()));
final String initialMessage = "This should not be received as the back off is cancelled.";
underTest.tell(BackOffActor.createBackOffWithAnswerMessage(initialMessage), getRef());
// verify there happens a back off
expectNoMessage(HALF_BACK_OFF_DURATION);
final String expectedMessage = "I expect to receive this after the second backOff";
underTest.tell(BackOffActor.createBackOffWithAnswerMessage(expectedMessage), getRef());
// verify there happens another back off
expectNoMessage(BACK_OFF_DURATION);
// verify we receive the answer afterwards
expectMsg(DOUBLE_BACK_OFF_DURATION, expectedMessage);
}};
}
@Test
public void backOffDelayWillBeResetAfterSomeTime() {
new TestKit(actorSystem) {{
final ActorRef underTest = childActorOf(BackOffActor.props(createBackOffConfig()));
final String message = "I expect to receive this after backOff";
underTest.tell(BackOffActor.createBackOffWithAnswerMessage(message), getRef());
// verify there happens a back off
expectNoMessage(HALF_BACK_OFF_DURATION);
// verify we receive the answer afterwards
expectMsg(BACK_OFF_DURATION, message);
// wait for ~BACK_OFF_DURATION, the timer should reset itself. Using double so test should always work
expectNoMessage(DOUBLE_BACK_OFF_DURATION);
// test again with the same duration to verify the actor doesn't use the double duration
underTest.tell(BackOffActor.createBackOffWithAnswerMessage(message), getRef());
expectNoMessage(HALF_BACK_OFF_DURATION);
expectMsg(BACK_OFF_DURATION, message);
}};
}
@Test
public void providesInformationIfCurrentlyInBackOff() {
final TestKit probe = new TestKit(actorSystem);
final ActorRef underTest = probe.childActorOf(BackOffActor.props(createBackOffConfig()));
final String message = "I expect to receive this after backOff";
assertIsInBackOffMode(underTest, probe, false);
underTest.tell(BackOffActor.createBackOffWithAnswerMessage(message), probe.getRef());
assertIsInBackOffMode(underTest, probe, true);
// verify we receive the answer afterwards
probe.expectNoMessage(HALF_BACK_OFF_DURATION);
probe.expectMsg(BACK_OFF_DURATION, message);
assertIsInBackOffMode(underTest, probe, false);
}
private static void assertIsInBackOffMode(final ActorRef underTest, final TestKit probe,
final boolean isInBackOff) {
underTest.tell(BackOffActor.createIsInBackOffMessage(), probe.getRef());
final BackOffActor.IsInBackOffResponse response = probe.expectMsgClass(BackOffActor.IsInBackOffResponse.class);
assertThat(response.isInBackOff()).isEqualTo(isInBackOff);
}
@Test
public void maxBackOffTimeIsNotExceeded() {
final Duration minBackOff = Duration.ofSeconds(1);
final Duration maxBackOff = Duration.ofSeconds(2);
final BackOffConfig configWithSmallMaxTimeout = createBackOffConfig(minBackOff, maxBackOff);
final TestKit probe = new TestKit(actorSystem);
final ActorRef underTest = probe.childActorOf(BackOffActor.props(configWithSmallMaxTimeout));
final String message = "I expect to receive this after backOff";
final int iterations = 10;
Duration currentBackOff = minBackOff;
for (int i = 0; i < iterations; ++i) {
underTest.tell(BackOffActor.createBackOffWithAnswerMessage(message), probe.getRef());
probe.expectNoMessage(currentBackOff.dividedBy(2L));
probe.expectMsg(currentBackOff, message);
currentBackOff = min(currentBackOff.multipliedBy(2L), maxBackOff);
}
}
private static Duration min(final Duration d1, final Duration d2) {
return d1.minus(d2).isNegative() ? d1 : d2;
}
private BackOffConfig createBackOffConfig() {
return createBackOffConfig(BACK_OFF_DURATION, DOUBLE_BACK_OFF_DURATION);
}
private BackOffConfig createBackOffConfig(final Duration minTimeout, final Duration maxTimeout) {
return () -> new TimeoutConfig() {
@Override
public Duration getMinTimeout() {
return minTimeout;
}
@Override
public Duration getMaxTimeout() {
return maxTimeout;
}
};
}
}