Search in sources :

Example 1 with TransactionCompletedEvent

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

the class TransactionTest method commitTransaction_generates_spyEvent.

@SuppressWarnings("ConstantConditions")
@Test
public void commitTransaction_generates_spyEvent() throws Exception {
    final ArezContext context = Arez.context();
    final TestSpyEventHandler handler = new TestSpyEventHandler();
    context.getSpy().addSpyEventHandler(handler);
    final String name = ValueUtil.randomString();
    final Transaction transaction = new Transaction(context, null, name, TransactionMode.READ_ONLY, null);
    transaction.begin();
    Transaction.setTransaction(transaction);
    Transaction.commit(transaction);
    handler.assertEventCount(1);
    final TransactionCompletedEvent event = handler.assertEvent(TransactionCompletedEvent.class, 0);
    assertEquals(event.getName(), name);
    assertEquals(event.isMutation(), false);
    assertEquals(event.getTracker(), null);
    assertTrue(event.getDuration() >= 0);
}
Also used : TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) Test(org.testng.annotations.Test)

Example 2 with TransactionCompletedEvent

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

the class ArezContextTest method action_safeFunction_throws_Exception.

@Test
public void action_safeFunction_throws_Exception() throws Exception {
    final ArezContext context = new ArezContext();
    assertFalse(context.isTransactionActive());
    assertThrowsWithMessage(context::getTransaction, "Arez-0117: Attempting to get current transaction but no transaction is active.");
    final AccessControlException secException = new AccessControlException("");
    final String name = ValueUtil.randomString();
    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(AccessControlException.class, () -> context.safeAction(name, mutation, () -> {
        throw secException;
    }, 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(), secException);
        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) AccessControlException(java.security.AccessControlException) ActionStartedEvent(arez.spy.ActionStartedEvent) TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) Test(org.testng.annotations.Test)

Example 3 with TransactionCompletedEvent

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

the class ArezContextTest method action_safeProcedure_throws_Exception.

@Test
public void action_safeProcedure_throws_Exception() throws Exception {
    final ArezContext context = new ArezContext();
    assertFalse(context.isTransactionActive());
    assertThrowsWithMessage(context::getTransaction, "Arez-0117: Attempting to get current transaction but no transaction is active.");
    final AccessControlException secException = new AccessControlException("");
    final String name = ValueUtil.randomString();
    final String param1 = "";
    final Object param2 = null;
    final int param3 = 3;
    final TestSpyEventHandler handler = new TestSpyEventHandler();
    context.getSpy().addSpyEventHandler(handler);
    final boolean mutation = false;
    final SafeProcedure procedure = () -> {
        throw secException;
    };
    assertThrows(AccessControlException.class, () -> context.safeAction(name, mutation, procedure, 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(), secException);
        assertEquals(e.returnsResult(), false);
        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 : AccessControlException(java.security.AccessControlException) ActionCompletedEvent(arez.spy.ActionCompletedEvent) TransactionStartedEvent(arez.spy.TransactionStartedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent) TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) Test(org.testng.annotations.Test)

Example 4 with TransactionCompletedEvent

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

the class ArezContextTest method action_procedure_throwsException.

@Test
public void action_procedure_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;
    final Procedure procedure = () -> {
        throw ioException;
    };
    assertThrows(IOException.class, () -> context.action(name, mutation, procedure, 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(), false);
        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 : IOException(java.io.IOException) ActionCompletedEvent(arez.spy.ActionCompletedEvent) TransactionStartedEvent(arez.spy.TransactionStartedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent) TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) Test(org.testng.annotations.Test)

Example 5 with TransactionCompletedEvent

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

the class ArezContextTest method track_function.

@Test
public void track_function() throws Throwable {
    final ArezContext context = new ArezContext();
    assertFalse(context.isTransactionActive());
    final String expectedValue = ValueUtil.randomString();
    final AtomicInteger callCount = new AtomicInteger();
    final Observer tracker = context.tracker(true, callCount::incrementAndGet);
    final Observable<?> observable = newObservable(context);
    assertEquals(observable.getObservers().size(), 0);
    final int nextNodeId = context.currentNextTransactionId();
    final String param1 = "";
    final Object param2 = null;
    final int param3 = 3;
    final TestSpyEventHandler handler = new TestSpyEventHandler();
    context.getSpy().addSpyEventHandler(handler);
    final String v0 = context.track(tracker, () -> {
        assertTrue(context.isTransactionActive());
        final Transaction transaction = context.getTransaction();
        assertEquals(transaction.getName(), tracker.getName());
        assertEquals(transaction.getMode(), tracker.getMode());
        assertEquals(observable.getObservers().size(), 0);
        assertNotEquals(nextNodeId, observable.getLastTrackerTransactionId());
        observable.reportObserved();
        // Tracking so state updated
        final ArrayList<Observable<?>> observables = transaction.getObservables();
        assertNotNull(observables);
        assertEquals(observables.size(), 1);
        assertEquals(observable.getObservers().size(), 0);
        assertEquals(observable.getLastTrackerTransactionId(), nextNodeId);
        return expectedValue;
    }, param1, param2, param3);
    assertFalse(context.isTransactionActive());
    context.getSpy().removeSpyEventHandler(handler);
    assertEquals(v0, expectedValue);
    assertEquals(observable.getLastTrackerTransactionId(), 0);
    assertEquals(observable.getObservers().size(), 1);
    assertEquals(tracker.getDependencies().size(), 1);
    // Reaction not called as the function sets up initial tracking
    assertEquals(callCount.get(), 0);
    context.action(observable::reportChanged);
    assertEquals(callCount.get(), 1);
    assertEquals(observable.getObservers().size(), 1);
    assertEquals(tracker.getDependencies().size(), 1);
    handler.assertEventCount(4);
    {
        final ActionStartedEvent e = handler.assertEvent(ActionStartedEvent.class, 0);
        assertEquals(e.isTracked(), true);
        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.isMutation(), true);
        final ObserverInfo info = e.getTracker();
        assertNotNull(info);
        assertEquals(info.getName(), tracker.getName());
    }
    {
        final TransactionCompletedEvent e = handler.assertEvent(TransactionCompletedEvent.class, 2);
        assertEquals(e.isMutation(), true);
        final ObserverInfo info = e.getTracker();
        assertNotNull(info);
        assertEquals(info.getName(), tracker.getName());
    }
    {
        final ActionCompletedEvent e = handler.assertEvent(ActionCompletedEvent.class, 3);
        assertEquals(e.getThrowable(), null);
        assertEquals(e.returnsResult(), true);
        assertEquals(e.getResult(), v0);
        assertEquals(e.isTracked(), true);
        final Object[] parameters = e.getParameters();
        assertEquals(parameters.length, 3);
        assertEquals(parameters[0], param1);
        assertEquals(parameters[1], param2);
        assertEquals(parameters[2], param3);
    }
}
Also used : ObserverInfo(arez.spy.ObserverInfo) ActionCompletedEvent(arez.spy.ActionCompletedEvent) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) TransactionStartedEvent(arez.spy.TransactionStartedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent) TransactionCompletedEvent(arez.spy.TransactionCompletedEvent) Test(org.testng.annotations.Test)

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