-
Notifications
You must be signed in to change notification settings - Fork 214
/
MonitoredUnboundedMailboxTypeTest.java
121 lines (101 loc) · 4.69 KB
/
MonitoredUnboundedMailboxTypeTest.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
/*
* 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.pekko.mailbox;
import static org.apache.pekko.Done.done;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
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.AbstractActor;
import org.apache.pekko.actor.ActorRef;
import org.apache.pekko.actor.ActorSystem;
import org.apache.pekko.actor.Props;
import org.apache.pekko.dispatch.MessageQueue;
import org.apache.pekko.dispatch.UnboundedMailbox;
import org.apache.pekko.event.Logging;
import org.apache.pekko.event.LoggingAdapter;
import org.apache.pekko.testkit.javadsl.EventFilter;
import org.apache.pekko.testkit.javadsl.TestKit;
import scala.Option;
public final class MonitoredUnboundedMailboxTypeTest {
private final static String ACTOR_SYSTEM_NAME = "monitored-unbounded-mailbox-test";
private final static String TEST_MAILBOX_NAME = "test-monitored-unbounded-mailbox";
private static Config testConfig;
private static ActorSystem system;
@BeforeClass
public static void init() {
testConfig = ConfigFactory.load("monitored-mailbox-test");
system = ActorSystem.create(ACTOR_SYSTEM_NAME, testConfig);
final LoggingAdapter logger = Logging.getLogger(system, MonitoredUnboundedMailboxTypeTest.class);
}
@AfterClass
public static void cleanup() {
if (system != null) {
TestKit.shutdownActorSystem(system);
}
}
@Test
public void testMonitoredUnboundedMailboxShouldLogWhenThresholdIsReached() {
new TestKit(system) {
{
// Arrange
final CountDownLatch sleepUntilLatch = new CountDownLatch(1);
final int thresholdForLogging =
testConfig.getInt(TEST_MAILBOX_NAME + ".monitored-unbounded-mailbox.threshold-for-logging");
final Props props = Props.create(AbstractActor.class, () -> new AbstractActor() {
@Override
public Receive createReceive() {
return receiveBuilder()
.matchEquals("sleep",
s -> {
getSender().tell("sleepy", getSelf());
sleepUntilLatch.await(10, TimeUnit.SECONDS);
})
.match(Integer.class, s -> getSender().tell(s, getSelf()))
.build();
}
});
final ActorRef subject = system.actorOf(props.withMailbox(TEST_MAILBOX_NAME),
"sleepy-test-actor");
// Act, Assert
new EventFilter(Logging.Info.class, system)
.startsWith("Mailbox size is <" + thresholdForLogging)
.occurrences(1)
.intercept(() -> {
subject.tell("sleep", getRef());
expectMsg("sleepy");
IntStream.range(0, thresholdForLogging).forEach(val -> subject.tell(val, getRef()));
return done();
});
// clean-up, free actor
sleepUntilLatch.countDown();
}
};
}
@Test
public void testActorShouldNotBeTrackedByMonitoredUnboundedMailbox() {
final Props withMailbox = Props.empty().withMailbox(TEST_MAILBOX_NAME);
final ActorRef actorRef = system.actorOf(withMailbox, "excluded-actor");
final MonitoredUnboundedMailboxType monitoredUnboundedMailboxType =
new MonitoredUnboundedMailboxType(system.settings(),
testConfig.getConfig(TEST_MAILBOX_NAME));
final MessageQueue messageQueue =
monitoredUnboundedMailboxType.create(Option.apply(actorRef), Option.apply(system));
assertThat(messageQueue).isInstanceOf(UnboundedMailbox.MessageQueue.class);
}
}