Liking cljdoc? Tell your friends :D

com.fulcrologic.fulcro.headless.timers

Timer control for deterministic timeout testing.

This namespace provides utilities for mocking and controlling timers in Fulcro tests. This is particularly useful for testing:

  • UI State Machine timeouts
  • Dynamic routing delay/error timers
  • Debounced operations
  • Any code that uses com.fulcrologic.fulcro.algorithms.scheduling/defer

Example:

(with-mock-timers
  (uism/begin! app LoginMachine ::login {:actor/form LoginForm} {})
  (uism/trigger! app ::login :submit {:username "test"})

  ;; Advance time to trigger timeout
  (advance-time! 5001)

  ;; Timer callbacks have now fired
  (is (= :error (uism/get-active-state app ::login))))
Timer control for deterministic timeout testing.

This namespace provides utilities for mocking and controlling timers
in Fulcro tests. This is particularly useful for testing:

- UI State Machine timeouts
- Dynamic routing delay/error timers
- Debounced operations
- Any code that uses `com.fulcrologic.fulcro.algorithms.scheduling/defer`

Example:
```clojure
(with-mock-timers
  (uism/begin! app LoginMachine ::login {:actor/form LoginForm} {})
  (uism/trigger! app ::login :submit {:username "test"})

  ;; Advance time to trigger timeout
  (advance-time! 5001)

  ;; Timer callbacks have now fired
  (is (= :error (uism/get-active-state app ::login))))
```
raw docstring

*mock-time*clj/s

When bound, holds the current mock time in milliseconds.

When bound, holds the current mock time in milliseconds.
sourceraw docstring

*pending-timers*clj/s

When bound, holds an atom containing pending timer entries. Each entry: {:id unique-id :fire-at ms :callback fn :active volatile}

When bound, holds an atom containing pending timer entries.
Each entry: {:id unique-id :fire-at ms :callback fn :active volatile}
sourceraw docstring

*timer-id-counter*clj/s

Counter for generating unique timer IDs.

Counter for generating unique timer IDs.
sourceraw docstring

advance-time!clj/s

(advance-time! ms)

Advance the mock time by the given number of milliseconds. Fires any timers whose time has come.

Returns the new mock time.

Advance the mock time by the given number of milliseconds.
Fires any timers whose time has come.

Returns the new mock time.
sourceraw docstring

advance-to-next-timer!clj/s

(advance-to-next-timer!)

Advance time to the next pending timer and fire it. Returns the new mock time, or nil if no timers pending.

Advance time to the next pending timer and fire it.
Returns the new mock time, or nil if no timers pending.
sourceraw docstring

clear-timers!clj/s

(clear-timers!)

Cancel and remove all pending timers without firing them.

Returns the number of timers cleared.

Cancel and remove all pending timers without firing them.

Returns the number of timers cleared.
sourceraw docstring

fire-all-timers!clj/s

(fire-all-timers!)

Fire all pending timers immediately, regardless of their scheduled time. Timers are fired in order of their scheduled time.

Returns the number of timers fired.

Fire all pending timers immediately, regardless of their scheduled time.
Timers are fired in order of their scheduled time.

Returns the number of timers fired.
sourceraw docstring

has-timer-with-delay?clj/s

(has-timer-with-delay? delay-ms & {:keys [tolerance] :or {tolerance 10}})

Check if there's a pending timer with approximately the given delay. Useful for verifying the correct timeout was scheduled.

Check if there's a pending timer with approximately the given delay.
Useful for verifying the correct timeout was scheduled.
sourceraw docstring

mock-timeclj/s

(mock-time)

Get the current mock time, or real time if not mocking.

Get the current mock time, or real time if not mocking.
sourceraw docstring

next-timer-atclj/s

(next-timer-at)

Get the fire-at time of the next pending timer, or nil if none.

Get the fire-at time of the next pending timer, or nil if none.
sourceraw docstring

pending-timer-countclj/s

(pending-timer-count)

Get the count of pending timers.

Get the count of pending timers.
sourceraw docstring

pending-timersclj/s

(pending-timers)

Get all pending timers when in mock mode. Returns nil if not in mock mode.

Get all pending timers when in mock mode.
Returns nil if not in mock mode.
sourceraw docstring

set-time!clj/s

(set-time! ms)

Set the mock time to an absolute value. Fires any timers whose time has come.

Returns the new mock time.

Set the mock time to an absolute value.
Fires any timers whose time has come.

Returns the new mock time.
sourceraw docstring

timer-infoclj/s

(timer-info)

Get information about pending timers. Returns a sequence of maps with :delay-ms and :fire-at keys.

Get information about pending timers.
Returns a sequence of maps with :delay-ms and :fire-at keys.
sourceraw docstring

with-mock-timersclj/smacro

(with-mock-timers & body)

Execute body with mocked timers.

Within this block:

  • All calls to sched/defer are captured instead of actually scheduling
  • Use advance-time! to move time forward and trigger timers
  • Use fire-all-timers! to fire all pending timers immediately
  • Use pending-timers to inspect what's scheduled

Example:

(with-mock-timers
  ;; Start something that sets a timeout
  (uism/trigger! app ::login :submit {})

  ;; Check that a timer was scheduled
  (is (= 1 (pending-timer-count)))

  ;; Advance time to fire the timer
  (advance-time! 5000)

  ;; Timer callback has now executed
  (is (= :timeout-state (uism/get-active-state app ::login))))
Execute body with mocked timers.

Within this block:
- All calls to `sched/defer` are captured instead of actually scheduling
- Use `advance-time!` to move time forward and trigger timers
- Use `fire-all-timers!` to fire all pending timers immediately
- Use `pending-timers` to inspect what's scheduled

Example:
```clojure
(with-mock-timers
  ;; Start something that sets a timeout
  (uism/trigger! app ::login :submit {})

  ;; Check that a timer was scheduled
  (is (= 1 (pending-timer-count)))

  ;; Advance time to fire the timer
  (advance-time! 5000)

  ;; Timer callback has now executed
  (is (= :timeout-state (uism/get-active-state app ::login))))
```
sourceraw docstring

with-mock-timers-fromclj/smacro

(with-mock-timers-from start-time & body)

Like with-mock-timers but starts at a specific time.

Example:

(with-mock-timers-from 1000
  (advance-time! 500)
  (is (= 1500 (mock-time))))
Like `with-mock-timers` but starts at a specific time.

Example:
```clojure
(with-mock-timers-from 1000
  (advance-time! 500)
  (is (= 1500 (mock-time))))
```
sourceraw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close