Class TestSignalEnvironment

  • All Implemented Interfaces:

    
    public class TestSignalEnvironment
    extends SignalEnvironment
                        

    Test-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 null so 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

    • Nested Class Summary

      Nested Classes 
      Modifier and Type Class Description
    • Field Summary

      Fields 
      Modifier and Type Field Description
    • Constructor Summary

      Constructors 
      Constructor Description
    • Enum Constant Summary

      Enum Constants 
      Enum Constant Description
    • Method Summary

      Modifier and Type Method Description
      static TestSignalEnvironment register() Registers this test environment as active SignalEnvironment and returns the created instance.
      void unregister() Unregisters this test environment if it was registered.
      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.
      • Methods inherited from class com.vaadin.flow.signals.SignalEnvironment

        getCurrentResultNotifier, getDefaultEffectDispatcher, register, registerFirst
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

    • Method Detail

      • 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:

        true if any pending Signals tasks were processed.