Search in sources :

Example 1 with ActionStartedEvent

use of arez.spy.ActionStartedEvent 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 2 with ActionStartedEvent

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

the class ArezContext method action.

private <T> T action(@Nullable final String name, @Nullable final TransactionMode mode, @Nonnull final Function<T> action, @Nullable final Observer tracker, @Nonnull final Object... parameters) throws Throwable {
    final boolean tracked = null != tracker;
    Throwable t = null;
    boolean completed = false;
    long startedAt = 0L;
    T result;
    try {
        if (willPropagateSpyEvents()) {
            startedAt = System.currentTimeMillis();
            assert null != name;
            getSpy().reportSpyEvent(new ActionStartedEvent(name, tracked, parameters));
        }
        final Transaction transaction = Transaction.begin(this, generateNodeName("Transaction", name), mode, tracker);
        try {
            result = action.call();
        } finally {
            Transaction.commit(transaction);
        }
        if (willPropagateSpyEvents()) {
            completed = true;
            final long duration = System.currentTimeMillis() - startedAt;
            assert null != name;
            getSpy().reportSpyEvent(new ActionCompletedEvent(name, tracked, parameters, true, result, null, duration));
        }
        return result;
    } catch (final Throwable e) {
        t = e;
        throw e;
    } finally {
        if (willPropagateSpyEvents()) {
            if (!completed) {
                final long duration = System.currentTimeMillis() - startedAt;
                assert null != name;
                getSpy().reportSpyEvent(new ActionCompletedEvent(name, tracked, parameters, true, null, t, duration));
            }
        }
        triggerScheduler();
    }
}
Also used : ActionCompletedEvent(arez.spy.ActionCompletedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent)

Example 3 with ActionStartedEvent

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

the class ArezContext method action.

void action(@Nullable final String name, @Nullable final TransactionMode mode, @Nonnull final Procedure action, final boolean reportAction, @Nullable final Observer tracker, @Nonnull final Object... parameters) throws Throwable {
    final boolean tracked = null != tracker;
    Throwable t = null;
    boolean completed = false;
    long startedAt = 0L;
    try {
        if (willPropagateSpyEvents() && reportAction) {
            startedAt = System.currentTimeMillis();
            assert null != name;
            getSpy().reportSpyEvent(new ActionStartedEvent(name, tracked, parameters));
        }
        final Transaction transaction = Transaction.begin(this, generateNodeName("Transaction", name), mode, tracker);
        try {
            action.call();
        } finally {
            Transaction.commit(transaction);
        }
        if (willPropagateSpyEvents() && reportAction) {
            completed = true;
            final long duration = System.currentTimeMillis() - startedAt;
            assert null != name;
            getSpy().reportSpyEvent(new ActionCompletedEvent(name, tracked, parameters, false, null, null, duration));
        }
    } catch (final Throwable e) {
        t = e;
        throw e;
    } finally {
        if (willPropagateSpyEvents() && reportAction) {
            if (!completed) {
                final long duration = System.currentTimeMillis() - startedAt;
                assert null != name;
                getSpy().reportSpyEvent(new ActionCompletedEvent(name, tracked, parameters, false, null, t, duration));
            }
        }
        triggerScheduler();
    }
}
Also used : ActionCompletedEvent(arez.spy.ActionCompletedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent)

Example 4 with ActionStartedEvent

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

the class ArezContext method safeAction.

void safeAction(@Nullable final String name, @Nullable final TransactionMode mode, @Nonnull final SafeProcedure action, @Nullable final Observer tracker, @Nonnull final Object... parameters) {
    final boolean tracked = null != tracker;
    Throwable t = null;
    boolean completed = false;
    long startedAt = 0L;
    try {
        if (willPropagateSpyEvents()) {
            startedAt = System.currentTimeMillis();
            assert null != name;
            getSpy().reportSpyEvent(new ActionStartedEvent(name, tracked, parameters));
        }
        final Transaction transaction = Transaction.begin(this, generateNodeName("Transaction", name), mode, tracker);
        try {
            action.call();
        } finally {
            Transaction.commit(transaction);
        }
        if (willPropagateSpyEvents()) {
            completed = true;
            final long duration = System.currentTimeMillis() - startedAt;
            assert null != name;
            getSpy().reportSpyEvent(new ActionCompletedEvent(name, tracked, parameters, false, null, null, duration));
        }
    } catch (final Throwable e) {
        t = e;
        throw e;
    } finally {
        if (willPropagateSpyEvents()) {
            if (!completed) {
                final long duration = System.currentTimeMillis() - startedAt;
                assert null != name;
                getSpy().reportSpyEvent(new ActionCompletedEvent(name, tracked, parameters, false, null, t, duration));
            }
        }
        triggerScheduler();
    }
}
Also used : ActionCompletedEvent(arez.spy.ActionCompletedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent)

Example 5 with ActionStartedEvent

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

the class ArezContext method performDispose.

/**
 * Run a "dispose" action with specified name.
 *
 * @param name   the name of action/transaction.
 * @param action the action to run.
 */
private void performDispose(@Nullable final String name, @Nonnull final SafeProcedure action) {
    Throwable t = null;
    boolean completed = false;
    long startedAt = 0L;
    try {
        if (willPropagateSpyEvents()) {
            startedAt = System.currentTimeMillis();
            assert null != name;
            getSpy().reportSpyEvent(new ActionStartedEvent(name, false, new Object[0]));
        }
        final Transaction transaction = Transaction.begin(this, generateNodeName("Transaction", name), Arez.shouldEnforceTransactionType() ? TransactionMode.DISPOSE : null, null);
        try {
            action.call();
        } finally {
            Transaction.commit(transaction);
        }
        if (willPropagateSpyEvents()) {
            completed = true;
            final long duration = System.currentTimeMillis() - startedAt;
            assert null != name;
            getSpy().reportSpyEvent(new ActionCompletedEvent(name, false, new Object[0], false, null, null, duration));
        }
    } catch (final Throwable e) {
        t = e;
        throw e;
    } finally {
        if (willPropagateSpyEvents()) {
            if (!completed) {
                final long duration = System.currentTimeMillis() - startedAt;
                assert null != name;
                getSpy().reportSpyEvent(new ActionCompletedEvent(name, false, new Object[0], false, null, t, duration));
            }
        }
        triggerScheduler();
    }
}
Also used : ActionCompletedEvent(arez.spy.ActionCompletedEvent) ActionStartedEvent(arez.spy.ActionStartedEvent)

Aggregations

ActionCompletedEvent (arez.spy.ActionCompletedEvent)10 ActionStartedEvent (arez.spy.ActionStartedEvent)10 TransactionCompletedEvent (arez.spy.TransactionCompletedEvent)5 TransactionStartedEvent (arez.spy.TransactionStartedEvent)5 Test (org.testng.annotations.Test)5 IOException (java.io.IOException)2 AccessControlException (java.security.AccessControlException)2 ObserverInfo (arez.spy.ObserverInfo)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1