Search in sources :

Example 6 with TransactionStartedEvent

use of arez.spy.TransactionStartedEvent 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 TransactionStartedEvent

use of arez.spy.TransactionStartedEvent in project arez by arez.

the class TransactionTest method beginTransaction_generates_spyEvent.

@Test
public void beginTransaction_generates_spyEvent() throws Exception {
    final ArezContext context = Arez.context();
    final TestSpyEventHandler handler = new TestSpyEventHandler();
    context.getSpy().addSpyEventHandler(handler);
    final String name = ValueUtil.randomString();
    Transaction.begin(context, name, TransactionMode.READ_ONLY, null);
    handler.assertEventCount(1);
    final TransactionStartedEvent event = handler.assertEvent(TransactionStartedEvent.class, 0);
    assertEquals(event.getName(), name);
    assertEquals(event.isMutation(), false);
    assertEquals(event.getTracker(), null);
}
Also used : TransactionStartedEvent(arez.spy.TransactionStartedEvent) Test(org.testng.annotations.Test)

Example 8 with TransactionStartedEvent

use of arez.spy.TransactionStartedEvent in project arez by arez.

the class SpyEventProcessorTest method increaseNesting.

@Test
public void increaseNesting() throws Throwable {
    final TestSpyEventProcessor processor = new TestSpyEventProcessor();
    final TransactionStartedEvent event = new TransactionStartedEvent(ValueUtil.randomString(), ValueUtil.randomBoolean(), null);
    final AtomicInteger handleCallCount = new AtomicInteger();
    final BiConsumer<SpyUtil.NestingDelta, TransactionStartedEvent> handler = (delta, e) -> {
        handleCallCount.incrementAndGet();
        assertEquals(delta, SpyUtil.NestingDelta.INCREASE);
        assertEquals(e, event);
    };
    processor.on(TransactionStartedEvent.class, handler);
    assertEquals(processor.getNestingDelta(event), SpyUtil.NestingDelta.INCREASE);
    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) TransactionStartedEvent(arez.spy.TransactionStartedEvent) AbstractArezExtrasTest(arez.extras.AbstractArezExtrasTest) Test(org.testng.annotations.Test)

Example 9 with TransactionStartedEvent

use of arez.spy.TransactionStartedEvent in project arez by arez.

the class Transaction method begin.

/**
 * Create a new transaction.
 *
 * @param context the associated context.
 * @param name    the name of the transaction. Should be non-null if {@link Arez#areNamesEnabled()} is true, false otherwise.
 * @param mode    the transaction mode.
 * @param tracker the observer that is tracking transaction if any.
 * @return the new transaction.
 */
static Transaction begin(@Nonnull final ArezContext context, @Nullable final String name, @Nullable final TransactionMode mode, @Nullable final Observer tracker) {
    if (Arez.shouldCheckApiInvariants()) {
        if (Arez.shouldEnforceTransactionType() && null != c_transaction) {
            apiInvariant(() -> TransactionMode.READ_WRITE != mode || TransactionMode.READ_WRITE == c_transaction.getMode(), () -> "Arez-0119: Attempting to create READ_WRITE transaction named '" + name + "' but it is " + "nested in transaction named '" + c_transaction.getName() + "' with mode " + c_transaction.getMode().name() + " which is not equal to READ_WRITE.");
            apiInvariant(() -> TransactionMode.DISPOSE != c_transaction.getMode() || TransactionMode.DISPOSE == mode, () -> "Arez-0175: Attempting to create transaction named '" + name + "' but it is " + "nested inside a DISPOSE transaction named '" + c_transaction.getName() + "'.");
        }
    }
    if (Arez.shouldCheckInvariants()) {
        if (!Arez.areZonesEnabled() && null != c_transaction) {
            invariant(() -> c_transaction.getContext() == context, () -> "Arez-0120: Zones are not enabled but the transaction named '" + name + "' is " + "nested in a transaction named '" + c_transaction.getName() + "' from a " + "different context.");
        }
        invariant(() -> !c_suspended, () -> "Arez-0121: Attempted to create transaction named '" + name + "' while " + "nested in a suspended transaction named '" + c_transaction.getName() + "'.");
    }
    c_transaction = new Transaction(context, c_transaction, name, mode, tracker);
    context.disableScheduler();
    c_transaction.begin();
    if (context.willPropagateSpyEvents()) {
        assert null != name;
        final ObserverInfoImpl trackerInfo = null != tracker ? new ObserverInfoImpl(c_transaction.getContext().getSpy(), tracker) : null;
        final boolean mutation = !Arez.shouldEnforceTransactionType() || TransactionMode.READ_WRITE == c_transaction.getMode();
        context.getSpy().reportSpyEvent(new TransactionStartedEvent(name, mutation, trackerInfo));
    }
    return c_transaction;
}
Also used : TransactionStartedEvent(arez.spy.TransactionStartedEvent)

Aggregations

TransactionStartedEvent (arez.spy.TransactionStartedEvent)9 Test (org.testng.annotations.Test)8 TransactionCompletedEvent (arez.spy.TransactionCompletedEvent)6 ActionCompletedEvent (arez.spy.ActionCompletedEvent)5 ActionStartedEvent (arez.spy.ActionStartedEvent)5 AbstractArezExtrasTest (arez.extras.AbstractArezExtrasTest)2 IOException (java.io.IOException)2 AccessControlException (java.security.AccessControlException)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 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