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);
}
}
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);
}
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);
}
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;
}
Aggregations