public class ConcurrencyTest extends JavaTestKit { @Test public void testConcurrencyLevel() { new JavaTestKit(system) { { final Props props = Props.create(MyActor.class); final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "myActor"); for (int i = 0; i < 10000; i++) { ref.tell("message", getRef()); } expectNoMessage(); } }; } } public class ConcurrencyRaceTest extends JavaTestKit { @Test public void testConcurrencyRace() { new JavaTestKit(system) { { final Props props = Props.create(MyActor.class); final TestActorRef<MyActor> ref = TestActorRef.create(system, props, "myActor"); for (int i = 0; i < 100; i++) { new Thread(() -> { ref.tell("message", getRef()); }).start(); } expectNoMessage(); } }; } } public class ShardingExample extends JavaTestKit { @Test public void testSharding() { new JavaTestKit(system) { { final Props props = Props.create(ShardedActor.class); final ClusterShardingSettings settings = ClusterShardingSettings.create(system); final ClusterSharding sharding = ClusterSharding.get(system); final ActorRef shardRegion = sharding.start( "shardedActor", props, settings, new ExtractEntityId() { @Override public String entityId(Object message) { if (message instanceof Command) { return String.valueOf(((Command) message).getShardKey()); } return null; } @Override public String shardId(Object message) { int numberOfShards = 100; if (message instanceof Command) { long shardKey = ((Command) message).getShardKey(); return String.valueOf(shardKey % numberOfShards); } return null; } } ); for (int i = 0; i < 10000; i++) { shardRegion.tell(new Command(i, "message"), getRef()); } expectNoMessage(); } }; } } conf akka { actor { default-dispatcher { fork-join-executor { parallelism-factor = 2.0 parallelism-max = 64 } throughput = 100 } } }


上一篇:
下一篇:
切换中文