@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(); }
@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(); }
@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(); }