Class TestSignalEnvironment
-
- All Implemented Interfaces:
public class TestSignalEnvironment extends SignalEnvironmentTest-only 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 returns
nullso that result notifications fall through to the next environment or run immediately. - Tests call runPendingTasks to dequeue and run all pending tasks on the calling thread.
- If the current thread holds a VaadinSession lock, the lock is temporarily released so that background threads have a chance of locking the session.
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
-
-
Method Summary
Modifier and Type Method Description static TestSignalEnvironmentregister()Registers this test environment as active SignalEnvironment and returns the created instance. voidunregister()Unregisters this test environment if it was registered. 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. -
-
Method Detail
-
register
static TestSignalEnvironment register()
Registers this test environment as active SignalEnvironment and returns the created instance.
Use together with unregister in a try/finally block to ensure proper cleanup.
- Returns:
a new registered TestSignalEnvironment instance
-
unregister
void unregister()
Unregisters this test environment if it was registered.
This method is idempotent and safe to call multiple times. If a cleanup action was provided by register, it will be invoked.
-
runPendingTasks
boolean runPendingTasks(long maxWaitTime, TimeUnit unit)
Executes pending tasks from the queue, continuously polling for new tasks until the timeout expires with no new task arriving.
If a VaadinSession lock 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.unit- the time unit of the timeout value- Returns:
trueif any pending Signals tasks were processed.
-
-
-
-