Beispiel #1
0
  @Test
  public void failedTransaction2() throws ExecutionException, InterruptedException {
    Stage stage = createStage();
    Bank bank = Actor.getReference(Bank.class, "jimmy");
    Inventory inventory = Actor.getReference(Inventory.class, "jimmy");
    Store store = Actor.getReference(Store.class, "all");
    bank.increment(15).join();

    fakeSync.put("proceed", "ok");

    // this item is invalid but the bank balance is decreased first.
    store.buyItem(bank, inventory, "candy", 10).join();
    store.buyItem(bank, inventory, "chocolate", 1).join();
    try {
      store.buyItem(bank, inventory, "ice cream", 50).join();
      fail("expecting an exception");
    } catch (CompletionException ex) {
      System.out.println(ex.getCause().getMessage());
    }

    // the bank credits must be restored.
    eventually(() -> assertEquals((Integer) 4, bank.getBalance().join()));
    // no items were given
    eventually(() -> assertEquals(2, inventory.getItems().join().size()));
    dumpMessages();
  }
Beispiel #2
0
  @Test
  public void remote() throws ExecutionException, InterruptedException {
    Stage stage = createStage();

    Disjunct jimmy = Actor.getReference(Disjunct.class, "1");
    Disjunct other = Actor.getReference(Disjunct.class, "other");
    assertEquals(3, (int) jimmy.remote(1, other, 2).join());
    assertEquals(1, (int) jimmy.getBalance().join());
    assertEquals(2, (int) other.getBalance().join());
  }
Beispiel #3
0
  @Test
  public void disjuntSuccessful() throws ExecutionException, InterruptedException {
    Stage stage = createStage();

    Disjunct jimmy = Actor.getReference(Disjunct.class, "1");
    assertEquals(3, (int) jimmy.local(1, 2).join());
  }
Beispiel #4
0
 @Test()
 public void singleActorSingleStageTest() throws ExecutionException, InterruptedException {
   Stage stage1 = createStage();
   Hello hello = Actor.getReference(Hello.class, "1");
   assertEquals("bla", hello.sayHello("bla").join());
   dumpMessages();
 }
Beispiel #5
0
  @Test
  public void remoteFailureInTheFirst() throws ExecutionException, InterruptedException {
    Stage stage = createStage();

    Disjunct jimmy = Actor.getReference(Disjunct.class, "1");
    Disjunct other = Actor.getReference(Disjunct.class, "other");
    // a successful transaction
    assertEquals(3, (int) jimmy.remote(1, other, 2).join());

    // the failure
    assertEquals(
        "i1: -1", expectException(() -> jimmy.remote(-1, other, 2).join()).getCause().getMessage());

    eventually(() -> assertEquals(1, (int) jimmy.getBalance().join()));
    assertEquals(2, (int) other.getBalance().join());
    dumpMessages();
  }
Beispiel #6
0
 @Override
 public Task<Void> callRandomB() {
   if (Math.random() > 0.5d) return Task.done(); // some variance to the calls
   String id = Integer.toString((int) (Math.random() * 10));
   ExampleB b = Actor.getReference(ExampleB.class, id);
   b.someWork().join();
   return Task.done();
 }
Beispiel #7
0
  @Test
  public void remoteFailureInTheSecond() throws ExecutionException, InterruptedException {
    Stage stage = createStage();

    Disjunct jimmy = Actor.getReference(Disjunct.class, "1");
    Disjunct other = Actor.getReference(Disjunct.class, "other");
    assertEquals(3, (int) jimmy.remote(1, other, 2).join());
    assertEquals(1, (int) jimmy.getBalance().join());
    assertEquals(2, (int) other.getBalance().join());

    assertEquals(
        "i: -2", expectException(() -> jimmy.remote(1, other, -2).join()).getCause().getMessage());

    // the first transaction is not canceled
    eventually(() -> assertEquals(2, (int) jimmy.getBalance().join()));
    // the second transaction was reverted
    eventually(() -> assertEquals(2, (int) other.getBalance().join()));
  }
Beispiel #8
0
  @Test
  public void successfulTransaction() throws ExecutionException, InterruptedException {
    Stage stage = createStage();
    Bank bank = Actor.getReference(Bank.class, "jimmy");
    Inventory inventory = Actor.getReference(Inventory.class, "jimmy");
    Store store = Actor.getReference(Store.class, "all");
    bank.increment(15).join();

    // this allows the store to proceed without blocking
    fakeSync.put("proceed", "ok");
    store.buyItem(bank, inventory, "candy", 10).join();
    // the balance was decreased
    assertEquals((Integer) 5, bank.getBalance().join());
    // got the item
    assertTrue(inventory.getItems().join().get(0).startsWith("candy:"));
    Thread.sleep(1000);
    dumpMessages();
  }
Beispiel #9
0
  @Test
  public void failedTransaction() throws ExecutionException, InterruptedException {
    Stage stage = createStage();
    Bank bank = Actor.getReference(Bank.class, "jimmy");
    Inventory inventory = Actor.getReference(Inventory.class, "jimmy");
    Store store = Actor.getReference(Store.class, "all");
    bank.increment(15).join();

    fakeSync.put("proceed", "fail");

    // this item is invalid but the bank balance is decreased first.
    expectException(() -> store.buyItem(bank, inventory, "ice cream", 10).join());

    // the bank credits must be restored.
    eventually(() -> assertEquals((Integer) 15, bank.getBalance().join()));
    // no items were given
    assertEquals(0, inventory.getItems().join().size());
    dumpMessages();
  }
Beispiel #10
0
  @Test(timeout = 10_000L)
  public void localFailureInTheFirst() throws ExecutionException, InterruptedException {
    Stage stage = createStage();

    Disjunct jimmy = Actor.getReference(Disjunct.class, "1");
    assertEquals(3, (int) jimmy.local(1, 2).join());

    assertEquals(
        "i1: -1", expectException(() -> jimmy.local(-1, 2).join()).getCause().getMessage());
    // the second transaction never happens
    eventually(() -> assertEquals(3, (int) jimmy.getBalance().join()));
  }
Beispiel #11
0
  @Test(timeout = 10_000L)
  public void localFailureInTheSecond() throws ExecutionException, InterruptedException {
    Stage stage = createStage();

    Disjunct jimmy = Actor.getReference(Disjunct.class, "1");
    assertEquals(6, (int) jimmy.local(1, 5).join());

    assertEquals(
        "i2: -20", expectException(() -> jimmy.local(10, -20).join()).getCause().getMessage());

    // the first transaction is not cancelled
    eventually(() -> assertEquals(16, (int) jimmy.getBalance().join()));
  }
  @Test
  public void testCloneIntegrity() {
    CacheResponse actor = Actor.getReference(CacheResponse.class, UUID.randomUUID().toString());

    // Initialize a dto
    int someIdValue = 3214;
    TestDto1 a = new TestDto1();
    a.setSampleInt(someIdValue);
    a.getSampleIntList().addAll(Arrays.asList(10, 20, 20, 30, 40, 50));
    a.setSampleActor(actor);

    // Place dto in actor
    actor.setDto1(a).join();

    // Get dto from actor, caching it
    TestDto1 a1 = actor.getDto1().join();

    // Modify our copy of the cached dto
    a1.setSampleInt(someIdValue + 10);
    a1.getSampleIntList().remove((Object) 30);
    a1.getSampleIntList().add(60);
    a1.getSampleIntList().add(70);

    // Get dto from cache
    TestDto1 a2 = actor.getDto1().join();

    // Verify that the cached dto is unchanged
    assertEquals(someIdValue, a2.getSampleInt());

    if (stage.getObjectCloner() instanceof JavaSerializationCloner) {
      assertEquals(a1.getId(), a2.getId());
    } else {
      assertSame(a1.getId(), a2.getId());
    }

    assertNotEquals(a1.getSampleIntList().size(), a2.getSampleIntList().size());
    assertTrue(a2.getSampleIntList().contains(30));
    assertFalse(a2.getSampleIntList().contains(60));
    assertFalse(a2.getSampleIntList().contains(70));

    assertNotSame(a1, a2);
  }
  @Test
  public void testCloneCyclicReference() {
    // Set up cyclic reference
    TestDto1 a = new TestDto1();
    TestDto2 b = new TestDto2();

    a.setDto2(b);
    b.setDto1(a);

    assertSame(a, b.getDto1());

    // Send to actor
    CacheResponse actor = Actor.getReference(CacheResponse.class, UUID.randomUUID().toString());
    actor.setDto1(a).join();

    // Get dto from cache and test cyclic reference
    TestDto1 a1 = actor.getDto1().join();

    assertSame(a1, a1.getDto2().getDto1());
    assertNotSame(a, a1);
  }