@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 testTimedPayment() throws AccountDoesNotExistException { // The amounts before any ticks and payments int preTransferAmountHans = testAccount.getBalance().getAmount(); int preTransferAmountAlice = SweBank.getBalance("Alice"); // Set up the payment and tick once testAccount.addTimedPayment("Test", 1, 1, new Money(100, SEK), SweBank, "Alice"); testAccount.tick(); // There should be the same amount on each account assertEquals(preTransferAmountHans, testAccount.getBalance().getAmount().intValue()); assertEquals(preTransferAmountAlice, SweBank.getBalance("Alice").intValue()); testAccount.tick(); // 100 withdrawn from testAccount. Alice gets deposit of 100 assertEquals(preTransferAmountHans - 100, testAccount.getBalance().getAmount().intValue()); assertEquals(preTransferAmountAlice + 100, SweBank.getBalance("Alice").intValue()); // Tick for another payment testAccount.tick(); testAccount.tick(); // Another 100 withdrawn/deposit assertEquals(preTransferAmountHans - 200, testAccount.getBalance().getAmount().intValue()); assertEquals(preTransferAmountAlice + 200, SweBank.getBalance("Alice").intValue()); // Remove payment testAccount.removeTimedPayment("Test"); // Tick some times, and afterwards check if the amounts are still the same testAccount.tick(); testAccount.tick(); testAccount.tick(); testAccount.tick(); assertEquals(preTransferAmountHans - 200, testAccount.getBalance().getAmount().intValue()); assertEquals(preTransferAmountAlice + 200, SweBank.getBalance("Alice").intValue()); }
@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(); }