@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(); }
/** * Asserts that the execution of consumer throws a completion exception wrapping an exception of * the specific class. * * @param consumer The consumer. * @param exceptionClass The expected exception class. */ public static void assertThrowsCompletionException( final Consumer<Void> consumer, final Class<?> exceptionClass) { try { consumer.accept(null); } catch (final CompletionException completionEx) { final Throwable ex = completionEx.getCause(); if (ex.getClass() == exceptionClass) { return; } Assert.fail(String.format("unexpected exception of type %s was thrown", ex.getClass())); } Assert.fail(String.format("expected exception of type %s was not thrown", exceptionClass)); }
@Test public void testNestedTransaction() throws Exception { TransactionManager transactionManager = createTestTransactionManager(); Session session = sessionBuilder() .setTransactionId(TransactionId.create()) .setClientTransactionSupport() .build(); QueryStateMachine stateMachine = QueryStateMachine.begin( new QueryId("query"), "START TRANSACTION", session, URI.create("fake://uri"), true, transactionManager, executor); try { try { new StartTransactionTask() .execute( new StartTransaction(ImmutableList.of()), transactionManager, metadata, new AllowAllAccessControl(), stateMachine) .join(); Assert.fail(); } catch (CompletionException e) { throw Throwables.propagate(e.getCause()); } } catch (PrestoException e) { Assert.assertEquals(e.getErrorCode(), NOT_SUPPORTED.toErrorCode()); } Assert.assertTrue(transactionManager.getAllTransactionInfos().isEmpty()); Assert.assertFalse(stateMachine.getQueryInfoWithoutDetails().isClearTransactionId()); Assert.assertFalse( stateMachine.getQueryInfoWithoutDetails().getStartedTransactionId().isPresent()); }
@Test public void testStartTransactionTooManyIsolationLevels() throws Exception { Session session = sessionBuilder().setClientTransactionSupport().build(); TransactionManager transactionManager = createTestTransactionManager(); QueryStateMachine stateMachine = QueryStateMachine.begin( new QueryId("query"), "START TRANSACTION", session, URI.create("fake://uri"), true, transactionManager, executor); Assert.assertFalse(stateMachine.getSession().getTransactionId().isPresent()); try { try { new StartTransactionTask() .execute( new StartTransaction( ImmutableList.of( new Isolation(Isolation.Level.READ_COMMITTED), new Isolation(Isolation.Level.READ_COMMITTED))), transactionManager, metadata, new AllowAllAccessControl(), stateMachine) .join(); Assert.fail(); } catch (CompletionException e) { throw Throwables.propagate(e.getCause()); } } catch (SemanticException e) { Assert.assertEquals(e.getCode(), INVALID_TRANSACTION_MODE); } Assert.assertTrue(transactionManager.getAllTransactionInfos().isEmpty()); Assert.assertFalse(stateMachine.getQueryInfoWithoutDetails().isClearTransactionId()); Assert.assertFalse( stateMachine.getQueryInfoWithoutDetails().getStartedTransactionId().isPresent()); }