Search in sources :

Example 6 with TransactionCompletedEvent

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);
    }
}
Also used : ActionCompletedEvent(arez.spy.ActionCompletedEvent) TransactionStartedEvent(arez.spy.TransactionStartedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent) TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) IOException(java.io.IOException) Test(org.testng.annotations.Test)

Example 7 with TransactionCompletedEvent

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);
}
Also used : TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) TransactionStartedEvent(arez.spy.TransactionStartedEvent) Assert(org.testng.Assert) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractArezExtrasTest(arez.extras.AbstractArezExtrasTest) BiConsumer(java.util.function.BiConsumer) ValueUtil(org.realityforge.guiceyloops.shared.ValueUtil) Test(org.testng.annotations.Test) ObserverCreatedEvent(arez.spy.ObserverCreatedEvent) Nonnull(javax.annotation.Nonnull) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) AbstractArezExtrasTest(arez.extras.AbstractArezExtrasTest) Test(org.testng.annotations.Test)

Example 8 with TransactionCompletedEvent

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();
}
Also used : TransactionCompletedEvent(arez.spy.TransactionCompletedEvent)

Aggregations

TransactionCompletedEvent (arez.spy.TransactionCompletedEvent)8 Test (org.testng.annotations.Test)7 TransactionStartedEvent (arez.spy.TransactionStartedEvent)6 ActionCompletedEvent (arez.spy.ActionCompletedEvent)5 ActionStartedEvent (arez.spy.ActionStartedEvent)5 IOException (java.io.IOException)2 AccessControlException (java.security.AccessControlException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AbstractArezExtrasTest (arez.extras.AbstractArezExtrasTest)1 ObserverCreatedEvent (arez.spy.ObserverCreatedEvent)1 ObserverInfo (arez.spy.ObserverInfo)1 BiConsumer (java.util.function.BiConsumer)1 Nonnull (javax.annotation.Nonnull)1 ValueUtil (org.realityforge.guiceyloops.shared.ValueUtil)1 Assert (org.testng.Assert)1