Module: Wurk::Testing
- Defined in:
- lib/wurk/testing.rb
Overview
Sidekiq::Testing-compatible test harness (aliased to Sidekiq::Testing). Three modes control how ‘Wurk::Client#raw_push` behaves:
:disable — real Redis push (the default; production behavior)
:fake — payloads collected in the in-memory Wurk::Queues store
:inline — jobs executed synchronously the instant they're pushed
A block form switches the mode for the duration of the block on the current thread only (‘fake! { … }`); the no-block form sets it process-globally.
Spec: docs/target/sidekiq-free.md §24.
Defined Under Namespace
Classes: EmptyQueueError, TestModeAlreadySetError
Constant Summary collapse
- THREAD_KEY =
:__wurk_testing_mode
Class Method Summary collapse
-
.__set_test_mode(new_mode, &block) ⇒ Object
Block → thread-local for the block’s duration; no block → global.
- .disable! ⇒ Object
- .disabled? ⇒ Boolean
-
.dispatch_push(payloads) ⇒ Object
Route a push through the active test mode (only called when enabled?).
-
.drain_all ⇒ Object
Run every fake job across all classes until the store is empty.
- .enabled? ⇒ Boolean
- .fake! ⇒ Object
- .fake? ⇒ Boolean
-
.fake_push(payloads) ⇒ Object
Collect payloads into the in-memory store.
- .inline! ⇒ Object
- .inline? ⇒ Boolean
-
.inline_push(payloads) ⇒ Object
Execute each payload immediately through the inline server chain.
-
.mode ⇒ Object
Thread-local override (set by a block) wins over the global mode, so a ‘fake! { … }` block is isolated to the calling thread.
-
.server_middleware {|@server_middleware| ... } ⇒ Object
In-process server-middleware chain used for inline execution.
Class Method Details
.__set_test_mode(new_mode, &block) ⇒ Object
Block → thread-local for the block’s duration; no block → global. Nesting block forms (‘fake! { inline! { … } }`) is rejected, matching Sidekiq 8.
44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/wurk/testing.rb', line 44 def __set_test_mode(new_mode, &block) return @mode = new_mode unless block if ::Thread.current[THREAD_KEY] raise TestModeAlreadySetError, 'Nested Sidekiq::Testing block modes are not allowed' end ::Thread.current[THREAD_KEY] = new_mode begin block.call ensure ::Thread.current[THREAD_KEY] = nil end end |
.disable! ⇒ Object
26 |
# File 'lib/wurk/testing.rb', line 26 def disable!(&) = __set_test_mode(:disable, &) |
.disabled? ⇒ Boolean
30 |
# File 'lib/wurk/testing.rb', line 30 def disabled? = mode == :disable |
.dispatch_push(payloads) ⇒ Object
Route a push through the active test mode (only called when enabled?).
70 71 72 |
# File 'lib/wurk/testing.rb', line 70 def dispatch_push(payloads) inline? ? inline_push(payloads) : fake_push(payloads) end |
.drain_all ⇒ Object
Run every fake job across all classes until the store is empty.
92 93 94 95 96 97 98 99 |
# File 'lib/wurk/testing.rb', line 92 def drain_all count = 0 while (job = ::Wurk::Queues.shift_any) ::Object.const_get(job['class'].to_s).process_job(job) count += 1 end count end |
.enabled? ⇒ Boolean
31 |
# File 'lib/wurk/testing.rb', line 31 def enabled? = !disabled? |
.fake! ⇒ Object
27 |
# File 'lib/wurk/testing.rb', line 27 def fake!(&) = __set_test_mode(:fake, &) |
.fake? ⇒ Boolean
32 |
# File 'lib/wurk/testing.rb', line 32 def fake? = mode == :fake |
.fake_push(payloads) ⇒ Object
Collect payloads into the in-memory store. ‘enqueued_at` is stamped now unless the job is scheduled (`at`), mirroring the real client.
76 77 78 79 80 81 82 83 |
# File 'lib/wurk/testing.rb', line 76 def fake_push(payloads) now = ::Process.clock_gettime(::Process::CLOCK_REALTIME, :millisecond) payloads.each do |payload| payload['enqueued_at'] = now unless payload['at'] ::Wurk::Queues.push(payload['queue'], payload['class'], payload) end payloads.last['jid'] end |
.inline! ⇒ Object
28 |
# File 'lib/wurk/testing.rb', line 28 def inline!(&) = __set_test_mode(:inline, &) |
.inline? ⇒ Boolean
33 |
# File 'lib/wurk/testing.rb', line 33 def inline? = mode == :inline |
.inline_push(payloads) ⇒ Object
Execute each payload immediately through the inline server chain.
86 87 88 89 |
# File 'lib/wurk/testing.rb', line 86 def inline_push(payloads) payloads.each { |payload| ::Object.const_get(payload['class'].to_s).process_job(payload) } payloads.last['jid'] end |
.mode ⇒ Object
Thread-local override (set by a block) wins over the global mode, so a ‘fake! { … }` block is isolated to the calling thread.
37 38 39 |
# File 'lib/wurk/testing.rb', line 37 def mode ::Thread.current[THREAD_KEY] || @mode || :disable end |
.server_middleware {|@server_middleware| ... } ⇒ Object
In-process server-middleware chain used for inline execution. Empty by default — configure with ‘Sidekiq::Testing.server_middleware { |c| … }`.
61 62 63 64 65 |
# File 'lib/wurk/testing.rb', line 61 def server_middleware @server_middleware ||= ::Wurk::Middleware::Chain.new(::Wurk.configuration) yield @server_middleware if block_given? @server_middleware end |