+ testkit Add java.time.Duration to testkit's javadsl

This commit is contained in:
虎鸣 2018-03-20 12:48:53 +08:00
parent 3685ce619e
commit e45a638ce0
9 changed files with 311 additions and 22 deletions

View file

@ -741,7 +741,7 @@ public class ActorDocTest extends AbstractJavaTest {
{
watch(b);
system.stop(a);
assertEquals(expectMsgClass(Duration.create(2, TimeUnit.SECONDS), Terminated.class).actor(), b);
assertEquals(expectMsgClass(java.time.Duration.ofSeconds(2), Terminated.class).actor(), b);
}
};
}

View file

@ -16,10 +16,10 @@ import akka.util.Timeout;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import scala.concurrent.duration.Duration;
import scala.concurrent.duration.FiniteDuration;
import java.time.Duration;
import java.util.*;
import java.util.concurrent.CompletionStage;
import java.util.concurrent.TimeUnit;
@ -147,12 +147,12 @@ public class RecipeGlobalRateLimit extends RecipeTest {
public void work() throws Exception {
new TestKit(system) {
//#global-limiter-flow
public <T> Flow<T, T, NotUsed> limitGlobal(ActorRef limiter, FiniteDuration maxAllowedWait) {
public <T> Flow<T, T, NotUsed> limitGlobal(ActorRef limiter, Duration maxAllowedWait) {
final int parallelism = 4;
final Flow<T, T, NotUsed> f = Flow.create();
return f.mapAsync(parallelism, element -> {
final Timeout triggerTimeout = new Timeout(maxAllowedWait);
final Timeout triggerTimeout = Timeout.create(maxAllowedWait);
final CompletionStage<Object> limiterTriggerFuture =
PatternsCS.ask(limiter, Limiter.WANT_TO_PASS, triggerTimeout);
return limiterTriggerFuture.thenApplyAsync(response -> element, system.dispatcher());
@ -187,7 +187,7 @@ public class RecipeGlobalRateLimit extends RecipeTest {
}
};
final FiniteDuration twoSeconds = (FiniteDuration) dilated(Duration.create(2, TimeUnit.SECONDS));
final java.time.Duration twoSeconds = dilated(java.time.Duration.ofSeconds(2));
final Sink<String, TestSubscriber.Probe<String>> sink = TestSink.probe(system);
final TestSubscriber.Probe<String> probe =

View file

@ -129,7 +129,7 @@ public class TestKitDocTest extends AbstractJavaTest {
//#test-within
new TestKit(system) {{
getRef().tell(42, ActorRef.noSender());
within(Duration.Zero(), Duration.create(1, "second"), () -> {
within(java.time.Duration.ZERO, java.time.Duration.ofSeconds(1), () -> {
assertEquals((Integer) 42, expectMsgClass(Integer.class));
return null;
});
@ -190,7 +190,7 @@ public class TestKitDocTest extends AbstractJavaTest {
//#test-awaitCond
new TestKit(system) {{
getRef().tell(42, ActorRef.noSender());
awaitCond(duration("1 second"), duration("100 millis"), this::msgAvailable);
awaitCond(java.time.Duration.ofSeconds(1), java.time.Duration.ofMillis(100), this::msgAvailable);
}};
//#test-awaitCond
}
@ -260,9 +260,9 @@ public class TestKitDocTest extends AbstractJavaTest {
public void demonstrateDilated() {
//#duration-dilation
new TestKit(system) {{
final FiniteDuration original = duration("1 second");
final Duration stretched = dilated(original);
assertTrue("dilated", stretched.gteq(original));
final java.time.Duration original = java.time.Duration.ofSeconds(1);
final java.time.Duration stretched = dilated(original);
assertTrue("dilated", stretched.compareTo(original) >= 0);
}};
//#duration-dilation
}
@ -399,7 +399,7 @@ public class TestKitDocTest extends AbstractJavaTest {
//#test-within-probe
new TestKit(system) {{
final TestKit probe = new TestKit(system);
within(duration("1 second"), () -> probe.expectMsgEquals("hello"));
within(java.time.Duration.ofSeconds(1), () -> probe.expectMsgEquals("hello"));
}};
//#test-within-probe
} catch (AssertionError e) {

View file

@ -67,10 +67,10 @@ public class TestKitSampleTest extends AbstractJavaTest {
// like a real resource would be passed in production
subject.tell(probe.getRef(), getRef());
// await the correct response
expectMsg(duration("1 second"), "done");
expectMsg(java.time.Duration.ofSeconds(1), "done");
// the run() method needs to finish within 3 seconds
within(duration("3 seconds"), () -> {
within(java.time.Duration.ofSeconds(3), () -> {
subject.tell("hello", getRef());
// This is a demo: would normally use expectMsgEquals().
@ -78,9 +78,9 @@ public class TestKitSampleTest extends AbstractJavaTest {
awaitCond(probe::msgAvailable);
// response must have been enqueued to us before probe
expectMsg(Duration.Zero(), "world");
expectMsg(java.time.Duration.ZERO, "world");
// check that the probe we injected earlier got the msg
probe.expectMsg(Duration.Zero(), "hello");
probe.expectMsg(java.time.Duration.ZERO, "hello");
Assert.assertEquals(getRef(), probe.getLastSender());
// Will wait for the rest of the 3 seconds

View file

@ -203,7 +203,7 @@ public class DeviceGroupQueryTest extends JUnitSuite {
queryActor.tell(new Device.RespondTemperature(0L, Optional.of(1.0)), device1.getRef());
DeviceGroup.RespondAllTemperatures response = requester.expectMsgClass(
FiniteDuration.create(5, TimeUnit.SECONDS),
java.time.Duration.ofSeconds(5),
DeviceGroup.RespondAllTemperatures.class);
assertEquals(1L, response.requestId);

View file

@ -194,7 +194,7 @@ public class DeviceGroupQueryTest extends JUnitSuite {
queryActor.tell(new Device.RespondTemperature(0L, Optional.of(1.0)), device1.getRef());
DeviceGroup.RespondAllTemperatures response = requester.expectMsgClass(
FiniteDuration.create(5, TimeUnit.SECONDS),
java.time.Duration.ofSeconds(5),
DeviceGroup.RespondAllTemperatures.class);
assertEquals(1L, response.requestId);