Java Code Examples for io.vlingo.actors.testkit.AccessSafely#readFromExpecting()

The following examples show how to use io.vlingo.actors.testkit.AccessSafely#readFromExpecting() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: InMemoryStateStoreTest.java    From vlingo-symbio with Mozilla Public License 2.0 6 votes vote down vote up
@Test
public void testThatAllOfTypeStreams() {
  for (int count = 1; count <= 200; ++count) {
    final Entity1 entity1 = new Entity1("" + count, count);
    store.write(entity1.id, entity1, 1, interest);
  }

  final Stream all = store.streamAllOf(Entity1.class).await();

  final AccessSafely access = AccessSafely.afterCompleting(200);

  access.writingWith("stateCounter", (state) -> { totalStates.incrementAndGet(); });
  access.readingWith("stateCount", () -> totalStates.get());

  all.flowInto(new ConsumerSink<>((state) -> access.writeUsing("stateCounter", 1)), 10);

  final int stateCount = access.readFromExpecting("stateCount", 200);

  Assert.assertEquals(totalStates.get(), 200);
  Assert.assertEquals(totalStates.get(), stateCount);
}
 
Example 2
Source File: DefaultSupervisorOverrideTest.java    From vlingo-actors with Mozilla Public License 2.0 5 votes vote down vote up
@Test
public void testOverride() {
  final FailureControlTestResults testResults = new FailureControlTestResults();
  
  final TestActor<FailureControl> failure =
          testWorld.actorFor(
                  FailureControl.class,
                  Definition.has(FailureControlActor.class, Definition.parameters(testResults), "failure-for-stop"));
  
  AccessSafely access = testResults.afterCompleting(40);

  for (int idx = 1; idx <= 20; ++idx) {
    failure.actor().failNow();
    failure.actor().afterFailure();
  }

  access.readFromExpecting("beforeResume", 20);
  assertEquals(20, (int) access.readFrom("beforeResume"));
  assertEquals(20, (int) access.readFrom("failNowCount"));
  assertEquals(20, (int) access.readFrom("afterFailureCount"));

  access = testResults.afterCompleting(40);
  
  for (int idx = 1; idx <= 20; ++idx) {
    failure.actor().failNow();
    failure.actor().afterFailure();
  }
  
  access.readFromExpecting("beforeResume", 40);
  assertEquals(40, (int) access.readFrom("failNowCount"));
  assertEquals(40, (int) access.readFrom("afterFailureCount"));
  assertFalse(failure.actorInside().isStopped());
}
 
Example 3
Source File: ClientTest.java    From vlingo-http with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void testThatRoundRobinClientDelivers() throws Exception {
  final TestResponseConsumer safely = new TestResponseConsumer();
  final AccessSafely access = safely.afterCompleting(10);
  final UnknownResponseConsumer unknown = new UnknownResponseConsumer(access);
  final KnownResponseConsumer known = new KnownResponseConsumer(access);
  final Address address = Address.from(Host.of("localhost"), portToUse, AddressType.NONE);

  final Configuration config = Client.Configuration.defaultedKeepAliveExceptFor(world.stage(), address, unknown);
  config.testInfo(true);

  final Client client =
          Client.using(
                  config,
                  Client.ClientConsumerType.RoundRobin,
                  5);

  for (int count = 0; count < 100; ++count) {
    final String user = count % 2 == 0 ? uniqueJohnDoe() : uniqueJaneDoe();
    client.requestWith(
            Request
              .has(POST)
              .and(URI.create("/users"))
              .and(host("localhost"))
              .and(contentLength(user))
              .and(keepAlive())
              .and(Body.from(user)))
          .andFinallyConsume(response -> known.consume(response) );
  }

  final int responseCount = access.readFromExpecting("responseCount", 100, 2000);
  final int total = access.readFrom("totalAllResponseCount");
  final int unknownResponseCount = access.readFrom("unknownResponseCount");
  final Map<String,Integer> clientCounts = access.readFrom("responseClientCounts");

  assertEquals(100, total);
  assertEquals(100, responseCount);
  assertEquals(0, unknownResponseCount);

  for (final String id : clientCounts.keySet()) {
    final int clientCound = clientCounts.get(id);
    assertEquals(20, clientCound);
  }
}
 
Example 4
Source File: ClientTest.java    From vlingo-http with Mozilla Public License 2.0 4 votes vote down vote up
@Test
public void testThatLoadBalancingClientDelivers() throws Exception {
  final TestResponseConsumer safely = new TestResponseConsumer();
  final AccessSafely access = safely.afterCompleting(100);
  final UnknownResponseConsumer unknown = new UnknownResponseConsumer(access);
  final KnownResponseConsumer known = new KnownResponseConsumer(access);
  final Address address = Address.from(Host.of("localhost"), portToUse, AddressType.NONE);

  final Configuration config = Client.Configuration.defaultedKeepAliveExceptFor(world.stage(), address, unknown);
  config.testInfo(true);

  final Client client =
          Client.using(
                  config,
                  Client.ClientConsumerType.LoadBalancing,
                  5);

  for (int count = 0; count < 100; ++count) {
    final String user = count % 2 == 0 ? uniqueJohnDoe() : uniqueJaneDoe();
    client.requestWith(
            Request
              .has(POST)
              .and(URI.create("/users"))
              .and(host("localhost"))
              .and(contentLength(user))
              .and(keepAlive())
              .and(Body.from(user)))
          .andFinallyConsume(known::consume);
  }

  final int responseCount = access.readFromExpecting("responseCount", 100, 2000);
  final int total = access.readFrom("totalAllResponseCount");
  final int unknownResponseCount = access.readFrom("unknownResponseCount");
  final Map<String,Integer> clientCounts = access.readFrom("responseClientCounts");

  assertEquals(100, total);
  assertEquals(100, responseCount);
  assertEquals(0, unknownResponseCount);

  int totalClientCounts = 0;
  for (final String id : clientCounts.keySet()) {
    final int clientCound = clientCounts.get(id);
    totalClientCounts += clientCound;
  }
  assertEquals(100, totalClientCounts);
}
 
Example 5
Source File: InMemoryEventJournalActorTest.java    From vlingo-symbio with Mozilla Public License 2.0 3 votes vote down vote up
@Test
public void testThatJournalReaderStreams() {
  final int limit = 1000;

  dispatcher.afterCompleting(0);
  interest.afterCompleting(0);

  for (int count = 0; count < limit; ++count) {
    journal.append("123-" + count, 1, new Test1Source(count), interest, object);
  }

  final AccessSafely access = AccessSafely.afterCompleting(limit);

  access.writingWith("sourcesCounter", (state) -> { totalSources.incrementAndGet(); });
  access.readingWith("sourcesCount", () -> totalSources.get());

  final Stream all = journal.journalReader("test").andThenTo(reader -> reader.streamAll()).await();

  final Consumer<EntryBundle> bundles = (bundle) -> access.writeUsing("sourcesCounter", 1);

  sink = new ConsumerSink<>(bundles);

  all.flowInto(sink, 100);

  final int sourcesCount = access.readFromExpecting("sourcesCount", limit);

  Assert.assertEquals(limit, totalSources.get());
  Assert.assertEquals(totalSources.get(), sourcesCount);
}
 
Example 6
Source File: ActorStopTest.java    From vlingo-actors with Mozilla Public License 2.0 2 votes vote down vote up
public void testStopActors() throws Exception {
  final TestResults results = new TestResults();

  final AccessSafely beforeStartCountAccess = results.beforeStartCountAccessCompletes(12);

  world.defaultLogger().debug("Test: testStopActors: starting actors");

  final ChildCreatingStoppable[] stoppables = setUpActors(world, results);

  for (int idx = 0; idx < stoppables.length; ++idx) {
    stoppables[idx].createChildren();
  }

  final int beforeStartCount = beforeStartCountAccess.readFrom("value");
  assertEquals(12, beforeStartCount);

  world.defaultLogger().debug("Test: testStopActors: stopping actors");

  results.terminatingAccessCompletes(0).writeUsing("value", false);

  final AccessSafely stopCountAccess = results.stopCountAccessCompletes(12);

  for (int idx = 0; idx < stoppables.length; ++idx) {
    stoppables[idx].stop();
  }

  final int stopCount = stopCountAccess.readFromExpecting("value", 12);
  assertEquals(12, stopCount);

  world.defaultLogger().debug("Test: testStopActors: stopped actors");
  world.defaultLogger().debug("Test: testStopActors: terminating world");

  results.terminatingStopCountAccessCompletes(0);

  results.terminatingAccessCompletes(0).writeUsing("value", true);
  world.terminate();

  final int terminatingStopCount = results.terminatingStopCountAccess.readFrom("value");

  assertEquals(0, terminatingStopCount);
}