use of arez.spy.TransactionCompletedEvent in project arez by arez.
the class ArezContextTest method action_function_throwsException.
@Test
public void action_function_throwsException() throws Throwable {
final ArezContext context = new ArezContext();
assertFalse(context.isTransactionActive());
assertThrowsWithMessage(context::getTransaction, "Arez-0117: Attempting to get current transaction but no transaction is active.");
final String name = ValueUtil.randomString();
final IOException ioException = new IOException();
final String param1 = "";
final Object param2 = null;
final int param3 = 3;
final TestSpyEventHandler handler = new TestSpyEventHandler();
context.getSpy().addSpyEventHandler(handler);
final boolean mutation = false;
assertThrows(IOException.class, () -> context.action(name, mutation, () -> {
throw ioException;
}, param1, param2, param3));
assertFalse(context.isTransactionActive());
handler.assertEventCount(4);
{
final ActionStartedEvent e = handler.assertEvent(ActionStartedEvent.class, 0);
assertEquals(e.getName(), name);
assertEquals(e.isTracked(), false);
final Object[] parameters = e.getParameters();
assertEquals(parameters.length, 3);
assertEquals(parameters[0], param1);
assertEquals(parameters[1], param2);
assertEquals(parameters[2], param3);
}
{
final TransactionStartedEvent e = handler.assertEvent(TransactionStartedEvent.class, 1);
assertEquals(e.getName(), name);
assertEquals(e.isMutation(), mutation);
assertEquals(e.getTracker(), null);
}
{
final TransactionCompletedEvent e = handler.assertEvent(TransactionCompletedEvent.class, 2);
assertEquals(e.getName(), name);
assertEquals(e.isMutation(), mutation);
assertEquals(e.getTracker(), null);
}
{
final ActionCompletedEvent e = handler.assertEvent(ActionCompletedEvent.class, 3);
assertEquals(e.getName(), name);
assertEquals(e.getThrowable(), ioException);
assertEquals(e.returnsResult(), true);
assertEquals(e.getResult(), null);
assertEquals(e.isTracked(), false);
final Object[] parameters = e.getParameters();
assertEquals(parameters.length, 3);
assertEquals(parameters[0], param1);
assertEquals(parameters[1], param2);
assertEquals(parameters[2], param3);
}
}
use of arez.spy.TransactionCompletedEvent in project arez by arez.
the class SpyEventProcessorTest method decreaseNesting.
@Test
public void decreaseNesting() throws Throwable {
final TestSpyEventProcessor processor = new TestSpyEventProcessor();
final TransactionCompletedEvent event = new TransactionCompletedEvent(ValueUtil.randomString(), ValueUtil.randomBoolean(), null, Math.min(1, Math.abs(ValueUtil.randomLong())));
final AtomicInteger handleCallCount = new AtomicInteger();
final BiConsumer<SpyUtil.NestingDelta, TransactionCompletedEvent> handler = (delta, e) -> {
handleCallCount.incrementAndGet();
assertEquals(delta, SpyUtil.NestingDelta.DECREASE);
assertEquals(e, event);
};
processor.on(TransactionCompletedEvent.class, handler);
assertEquals(processor.getNestingDelta(event), SpyUtil.NestingDelta.DECREASE);
assertEquals(handleCallCount.get(), 0);
assertEquals(processor.getNestingLevel(), 0);
assertEquals(processor._handleUnhandledEventCallCount, 0);
processor.onSpyEvent(event);
assertEquals(handleCallCount.get(), 1);
assertEquals(processor.getNestingLevel(), -1);
assertEquals(processor._handleUnhandledEventCallCount, 0);
}
use of arez.spy.TransactionCompletedEvent in project arez by arez.
the class Transaction method commit.
/**
* Commit the supplied transaction.
*
* This method verifies that the transaction active is the supplied transaction before committing
* the transaction and restoring the prior transaction if any.
*
* @param transaction the transaction.
*/
static void commit(@Nonnull final Transaction transaction) {
if (Arez.shouldCheckInvariants()) {
invariant(() -> null != c_transaction, () -> "Arez-0122: Attempting to commit transaction named '" + transaction.getName() + "' but no transaction is active.");
assert null != c_transaction;
invariant(() -> c_transaction == transaction, () -> "Arez-0123: Attempting to commit transaction named '" + transaction.getName() + "' but this does not match existing transaction named '" + c_transaction.getName() + "'.");
invariant(() -> !c_suspended, () -> "Arez-0124: Attempting to commit transaction named '" + transaction.getName() + "' transaction is suspended.");
}
assert null != c_transaction;
c_transaction.commit();
if (c_transaction.getContext().willPropagateSpyEvents()) {
final String name = c_transaction.getName();
final boolean mutation = !Arez.shouldEnforceTransactionType() || TransactionMode.READ_WRITE == c_transaction.getMode();
final Observer tracker = c_transaction.getTracker();
final ObserverInfoImpl trackerInfo = null != tracker ? new ObserverInfoImpl(c_transaction.getContext().getSpy(), tracker) : null;
final long duration = System.currentTimeMillis() - c_transaction.getStartedAt();
c_transaction.getContext().getSpy().reportSpyEvent(new TransactionCompletedEvent(name, mutation, trackerInfo, duration));
}
final Transaction previousInSameContext = Arez.areZonesEnabled() ? c_transaction.getPreviousInSameContext() : c_transaction.getPrevious();
if (null == previousInSameContext) {
c_transaction.getContext().enableScheduler();
}
c_transaction = c_transaction.getPrevious();
}
Aggregations