Class TestSignalEnvironment
SignalEnvironment that records submitted tasks instead of
executing them asynchronously. This allows unit tests to deterministically
drive and observe Signal processing by explicitly flushing the task queue.
How it works:
getEffectDispatcher()returns an executor that enqueues tasks into an internal queue.getResultNotifier()returnsnullso that result notifications fall through to the next environment or run immediately.- Tests call
runPendingTasks(long, TimeUnit)to dequeue and run all pending tasks on the calling thread. Every simulated round trip also runs the tasks that are already queued, seerunQueuedTasks(). - If the current thread holds a
VaadinSessionlock, the lock is temporarily released so that background threads have a chance of locking the session.
Shared signals (SharedValueSignal, SharedListSignal and
friends) back onto a
com.vaadin.flow.signals.shared.impl.LocalAsynchronousSignalTree,
which confirms submitted commands through the effect dispatcher. Their writes
are therefore visible immediately, while the SignalOperation returned
by the write is only completed once the queued confirmation task is executed
by runPendingTasks(long, TimeUnit) or by the next round trip, see
runQueuedTasks().
Usage:
TestSignalEnvironment env = TestSignalEnvironment.register();
try {
// trigger signals here
env.waitForTasksCompletion(100, TimeUnit.MILLISECONDS);
} finally {
env.unregister();
}
For internal use only. May be renamed or removed in a future release.
- Since:
- 1.0
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected Executorprotected Executorprotected booleanisActive()static TestSignalEnvironmentregister()Registers this test environment as activeSignalEnvironmentand returns the created instance.booleanrunPendingTasks(long maxWaitTime, TimeUnit unit) Executes pending tasks from the queue, continuously polling for new tasks until the timeout expires with no new task arriving.static voidRuns, on the calling thread, the tasks that are queued in every registered test environment, without waiting for more to arrive, as part of a simulated round trip.voidRuns the tasks that are still pending on the calling thread, and then unregisters this test environment if it was registered.Methods inherited from class com.vaadin.flow.signals.SignalEnvironment
getCurrentResultNotifier, getDefaultEffectDispatcher, register, registerFirst
-
Constructor Details
-
TestSignalEnvironment
protected TestSignalEnvironment()Creates the environment; useregister()instead.
-
-
Method Details
-
register
Registers this test environment as activeSignalEnvironmentand returns the created instance.Use together with
unregister()in a try/finally block to ensure proper cleanup.- Returns:
- a new registered
TestSignalEnvironmentinstance
-
unregister
public void unregister()Runs the tasks that are still pending on the calling thread, and then unregisters this test environment if it was registered.A shared signal often outlives the test that wrote to it, for example when it is held by an application-scoped bean or a static field. If the confirmation of such a write were discarded with the queue, the signal would keep the write unconfirmed for good, and every later conditional update of it, such as
SharedValueSignal.update(...), would be rejected and retried forever. Running the pending tasks confirms those writes. Pending effects of UIs that are still open run as well, which is why teardown also runs the pending tasks before it closes the UIs. The environment stays registered while the tasks run, so tasks they submit are queued and run here too, on the calling thread, up to a limit of 10000 tasks; tasks beyond it are discarded with a warning. A task that fails is logged and skipped, so that the remaining tasks still run and teardown completes. Only aVirtualMachineError, such as running out of memory, is passed on, after the environment has been unregistered.The tasks run one after another on the calling thread, so a pending task must not block waiting for another one. For example, an effect that calls
signal.update(...).result().join()waits for a confirmation queued behind it and hangs teardown, which test timeouts do not cover.This method is idempotent and safe to call multiple times. If a cleanup action was provided by
SignalEnvironment.register(SignalEnvironment), it will be invoked. -
runQueuedTasks
public static void runQueuedTasks()Runs, on the calling thread, the tasks that are queued in every registered test environment, without waiting for more to arrive, as part of a simulated round trip.This lets a signal changed on another thread or from another window update the components bound to it when the test next looks at the UI, as
UI.access()does in a running application. Each task runs in its own hold of the current session lock, followed byMockVaadin.runUIQueue(), so that the task reads up-to-date signal values instead of the snapshot of the current lock hold, and so that aUI.access()command it queues runs right away and has its failure rethrown. The calling thread must therefore hold the lock of the current session exactly once. Tasks queued by the tasks that run, such as the effects of a confirmed shared signal write, are run too, up to a limit of 10000 tasks; the rest stay queued and a warning is logged, so that a task that keeps queuing new ones cannot hang the test and is still noticed. A task that fails throws its exception to the caller as is, and the tasks after it stay queued.For internal use only. May be renamed or removed in a future release.
-
isActive
protected boolean isActive()- Specified by:
isActivein classcom.vaadin.flow.signals.SignalEnvironment
-
getResultNotifier
- Specified by:
getResultNotifierin classcom.vaadin.flow.signals.SignalEnvironment
-
getEffectDispatcher
- Specified by:
getEffectDispatcherin classcom.vaadin.flow.signals.SignalEnvironment
-
runPendingTasks
Executes pending tasks from the queue, continuously polling for new tasks until the timeout expires with no new task arriving.If a
VaadinSessionlock is held by the current thread, it is temporarily released while polling for tasks, allowing background threads to acquire the lock and enqueue tasks. The lock is reacquired before running each task and released again before the next poll.If the current thread is interrupted while waiting for tasks, the method restores the interrupt status and fails with an
AssertionError.- Parameters:
maxWaitTime- the maximum time to wait for the next task to arrive in the given time unit. If <= 0, returns immediately if no tasks are available.unit- the time unit of the timeout value- Returns:
trueif any pending Signals tasks were processed.
-