Class: RubyReactor::RSpec::TestSubject
- Inherits:
-
Object
- Object
- RubyReactor::RSpec::TestSubject
- Includes:
- RSpec::Mocks::ExampleMethods
- Defined in:
- lib/ruby_reactor/rspec/test_subject.rb
Overview
rubocop:disable Metrics/ClassLength
Defined Under Namespace
Classes: StepProxy
Instance Attribute Summary collapse
-
#reactor_instance ⇒ Object
readonly
Returns the value of attribute reactor_instance.
-
#run_result ⇒ Object
readonly
Returns the value of attribute run_result.
Instance Method Summary collapse
-
#async_reactor(step_name) ⇒ Object
The child execution a dispatched
async_reactorcreated, as its own TestSubject — the same drill-down#composedgives for an inline child. -
#async_step(step_name, key = nil) ⇒ Object
The durable record for a dispatched
async_step, or nil if the step was never dispatched (e.g. underasync: false, where it ran inline and its value is an ordinarystep_result). -
#composed(step_name) ⇒ Object
(also: #compose)
Fluent API for mocking nested compose steps.
- #composed_entry(step_name) ⇒ Object
-
#current_step ⇒ Symbol?
Get the current step where the reactor is paused (interrupt step) Note: When multiple interrupts are ready, this returns just one of them.
- #ensure_executed! ⇒ Object
- #error ⇒ Object
-
#failing_at(step_name, *nested_steps, element_index: nil, &block) ⇒ Object
--- Configuration DSL ---.
- #failure? ⇒ Boolean
-
#halted_result(ctx) ⇒ Object
A clean halt: either a
with_periodgate or a step returningRubyReactor.Halt(...). -
#initialize(reactor_class:, inputs:, context: {}, async: nil, process_jobs: true) ⇒ TestSubject
constructor
A new instance of TestSubject.
-
#map(step_name) ⇒ Object
Fluent API for mocking nested map steps.
- #map_element(step_name, index: 0) ⇒ Object
-
#map_elements(step_name) ⇒ Object
--- Traversal ---.
-
#mock_step(step_name, *nested_steps, element_index: nil) {|args, context, original_impl| ... } ⇒ Object
Intercept a step and provide a custom implementation.
-
#paused? ⇒ Boolean
Check if the reactor is paused at an interrupt.
-
#ready_interrupt_steps ⇒ Array<Symbol>
Get all ready interrupt steps (steps that can be resumed) This is useful when multiple interrupts are waiting concurrently.
-
#result ⇒ Object
--- Introspection (Auto-Run) ---.
-
#resume(payload: {}, step: nil) ⇒ TestSubject
Resume a paused reactor with the given payload.
-
#run ⇒ Object
--- Execution ---.
- #run_async(boolean) ⇒ Object
- #step_result(name) ⇒ Object
- #success? ⇒ Boolean
Constructor Details
#initialize(reactor_class:, inputs:, context: {}, async: nil, process_jobs: true) ⇒ TestSubject
Returns a new instance of TestSubject.
11 12 13 14 15 16 17 18 19 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 11 def initialize(reactor_class:, inputs:, context: {}, async: nil, process_jobs: true) @reactor_class = reactor_class @inputs = inputs @context_data = context @async = async @process_jobs = process_jobs @interceptors = [] @executed = false end |
Instance Attribute Details
#reactor_instance ⇒ Object (readonly)
Returns the value of attribute reactor_instance.
9 10 11 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 9 def reactor_instance @reactor_instance end |
#run_result ⇒ Object (readonly)
Returns the value of attribute run_result.
9 10 11 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 9 def run_result @run_result end |
Instance Method Details
#async_reactor(step_name) ⇒ Object
The child execution a dispatched async_reactor created, as its own
TestSubject — the same drill-down #composed gives for an inline child.
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 156 def async_reactor(step_name) ensure_executed! entry = composed_entry(step_name) return nil unless entry && entry[:type] == :async_reactor_ref child_class = RubyReactor::Context.resolve_reactor_class(entry[:reactor_class_name]) return nil unless child_class child_instance = child_class.find(entry[:execution_id]) self.class.new( reactor_class: child_instance.class, inputs: child_instance.context.inputs, context: child_instance.context, async: @async, process_jobs: @process_jobs ).tap do |s| s.instance_variable_set(:@executed, true) s.instance_variable_set(:@reactor_instance, child_instance) end end |
#async_step(step_name, key = nil) ⇒ Object
The durable record for a dispatched async_step, or nil if the step was
never dispatched (e.g. under async: false, where it ran inline and its
value is an ordinary step_result). Mirrors #composed / #map in
reading the reference off composed_contexts.
subject.async_step(:send_email) # => the raw record hash
subject.async_step(:send_email, :status) # => "dispatched" / "completed"
140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 140 def async_step(step_name, key = nil) ensure_executed! entry = composed_entry(step_name) return nil unless entry && entry[:type] == :async_step_ref record = RubyReactor.configuration.storage_adapter.retrieve_step_result( entry[:context_id] || @reactor_instance.context.context_id, step_name, RubyReactor.reactor_storage_name(@reactor_instance.class) ) key && record ? record[key.to_s] : record end |
#composed(step_name) ⇒ Object Also known as: compose
Fluent API for mocking nested compose steps
61 62 63 64 65 66 67 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 61 def composed(step_name) # If already executed, return the traversed subject return traverse_composed(step_name) if @executed # Otherwise return a configuration proxy StepProxy.new(self, step_name) end |
#composed_entry(step_name) ⇒ Object
178 179 180 181 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 178 def composed_entry(step_name) contexts = @reactor_instance.context.composed_contexts contexts[step_name] || contexts[step_name.to_s] || contexts[step_name.to_sym] end |
#current_step ⇒ Symbol?
Get the current step where the reactor is paused (interrupt step)
Note: When multiple interrupts are ready, this returns just one of them.
Use ready_interrupt_steps to get all ready interrupt steps.
364 365 366 367 368 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 364 def current_step ensure_executed! step = @reactor_instance.context.current_step step&.to_sym end |
#ensure_executed! ⇒ Object
475 476 477 478 479 480 481 482 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 475 def ensure_executed! run unless @executed # Process jobs if status is running and processing is enabled return unless @process_jobs && @reactor_instance.context.status.to_s == "running" process_pending_jobs end |
#error ⇒ Object
470 471 472 473 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 470 def error res = result res.respond_to?(:error) ? res.error : nil end |
#failing_at(step_name, *nested_steps, element_index: nil, &block) ⇒ Object
--- Configuration DSL ---
23 24 25 26 27 28 29 30 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 23 def failing_at(step_name, *nested_steps, element_index: nil, &block) @interceptors << { type: :failure, step_path: [step_name, *nested_steps], conditions: { element_index: element_index, block: block } } self end |
#failure? ⇒ Boolean
344 345 346 347 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 344 def failure? ensure_executed! @reactor_instance.context.status.to_s == "failed" end |
#halted_result(ctx) ⇒ Object
A clean halt: either a with_period gate or a step returning
RubyReactor.Halt(...). The sync run already produced the exact
Halt (reason/step intact) — surface it. For async runs the worker
swallows the return value, so rebuild from the trace.
332 333 334 335 336 337 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 332 def halted_result(ctx) return @run_result if @run_result.is_a?(RubyReactor::Halt) entry = ctx.execution_trace.reverse.find { |t| t[:type].to_s == "halt" } RubyReactor::Halt.new(reason: entry&.dig(:reason), step_name: entry&.dig(:step)) end |
#map(step_name) ⇒ Object
Fluent API for mocking nested map steps
54 55 56 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 54 def map(step_name) StepProxy.new(self, step_name) end |
#map_element(step_name, index: 0) ⇒ Object
128 129 130 131 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 128 def map_element(step_name, index: 0) elements = map_elements(step_name) elements[index] end |
#map_elements(step_name) ⇒ Object
--- Traversal ---
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 94 def map_elements(step_name) ensure_executed! # Check composed_contexts entry = @reactor_instance.context.composed_contexts[step_name] || @reactor_instance.context.composed_contexts[step_name.to_s] || @reactor_instance.context.composed_contexts[step_name.to_sym] return [] unless entry && entry[:type] == :map_ref map_id = entry[:map_id] storage = RubyReactor.configuration.storage_adapter # This requires the storage adapter to implement retrieval of map element context IDs # If it's not implemented in MemoryAdapter (if used), we might need to fallback? # But tests use Redis. child_ids = storage.retrieve_map_element_context_ids(map_id, @reactor_instance.class.name) child_ids.map do |id| klass = RubyReactor::Context.resolve_reactor_class(entry[:element_reactor_class]) child_instance = klass.find(id) self.class.new( reactor_class: child_instance.class, inputs: child_instance.context.inputs, context: child_instance.context, async: @async, process_jobs: @process_jobs ).tap do |s| s.instance_variable_set(:@executed, true) s.instance_variable_set(:@reactor_instance, child_instance) end end end |
#mock_step(step_name, *nested_steps, element_index: nil) {|args, context, original_impl| ... } ⇒ Object
Intercept a step and provide a custom implementation
42 43 44 45 46 47 48 49 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 42 def mock_step(step_name, *nested_steps, element_index: nil, &block) @interceptors << { type: :mock, step_path: [step_name, *nested_steps], conditions: { element_index: element_index, block: block } } self end |
#paused? ⇒ Boolean
Check if the reactor is paused at an interrupt
354 355 356 357 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 354 def paused? ensure_executed! @reactor_instance.context.status.to_s == "paused" end |
#ready_interrupt_steps ⇒ Array<Symbol>
Get all ready interrupt steps (steps that can be resumed) This is useful when multiple interrupts are waiting concurrently.
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 374 def ready_interrupt_steps ensure_executed! return [] unless paused? # Build the dependency graph and get ready steps graph = RubyReactor::DependencyGraph.new graph_manager = RubyReactor::Executor::GraphManager.new( @reactor_class, graph, @reactor_instance.context ) graph_manager.build_and_validate! graph_manager.mark_completed_steps_from_context # Filter to only interrupt steps (using interrupt? predicate method) ready = graph_manager.dependency_graph.ready_steps ready.select { |step_config| step_config.respond_to?(:interrupt?) && step_config.interrupt? } .map { |step_config| step_config.name.to_sym } end |
#result ⇒ Object
--- Introspection (Auto-Run) ---
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 277 def result ensure_executed! ctx = @reactor_instance.context status = ctx.status.to_s case status when "failed" return ctx.failure_reason if ctx.failure_reason.is_a?(RubyReactor::Failure) RubyReactor::Failure.new(ctx.failure_reason || {}) when "completed" # Determine the success value val = if @reactor_class.return_step ctx.intermediate_results[@reactor_class.return_step.to_sym] else # Return result of the last executed step # Execution trace contains: { step: name, ... } # Trace does not strictly contain result, so we look up in intermediate_results last_trace = ctx.execution_trace.last if last_trace step_name = last_trace[:step] # Handle symbol/string mismatch ctx.intermediate_results[step_name.to_sym] || ctx.intermediate_results[step_name.to_s] end end RubyReactor::Success.new(val) when "halted", "skipped" # "skipped" is the legacy name for a halt halted_result(ctx) when "running" # Try to determine if it is truly running or if we just missed the completion if @process_jobs && AsyncTestHelpers.active? # Force one more check process_pending_jobs # Reload status @reactor_instance = @reactor_class.find(@reactor_instance.context.context_id) return result unless @reactor_instance.context.status.to_s == "running" end # If still running, return a Pending/Running result instead of nil # This allows matchers to report "expected success but was running" RubyReactor::Failure("Reactor is still running (Async operations pending?)", retryable: true) when "paused" RubyReactor::InterruptResult.new( execution_id: ctx.context_id, intermediate_results: ctx.intermediate_results # We assume no error if paused normally ) end end |
#resume(payload: {}, step: nil) ⇒ TestSubject
Resume a paused reactor with the given payload
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 400 def resume(payload: {}, step: nil) ensure_executed! unless paused? raise RubyReactor::Error::ValidationError, "Cannot resume: reactor is not paused (status: #{@reactor_instance.context.status})" end step_name = determine_resume_step(step) # Use the reactor's continue method @reactor_instance.continue(payload: payload, step_name: step_name) # Process any pending async jobs process_pending_jobs if @process_jobs && AsyncTestHelpers.active? # Reload the reactor instance to get updated state @reactor_instance = @reactor_class.find(@reactor_instance.context.context_id) # Return self for chaining and introspection self end |
#run ⇒ Object
--- Execution ---
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 221 def run return self if @executed # 1. Apply Interceptors (Dynamic Subclassing) execution_class = prepare_execution_class # 2. Capture Context ID captured_context_id = nil allow(RubyReactor::Context).to receive(:new).and_wrap_original do |m, *args| ctx = m.call(*args) captured_context_id ||= ctx.context_id ctx end # 3. Native Run if @async == false allow(execution_class).to receive(:async?).and_return(false) elsif @async == true allow(execution_class).to receive(:async?).and_return(true) end @run_result = nil if @process_jobs && AsyncTestHelpers.sidekiq_testing? # Ensure the Sidekiq router is used to capture jobs in fake mode allow(RubyReactor.configuration).to receive(:async_router).and_return(RubyReactor::Adapters::Sidekiq::Router) # Avoid nesting error which happens in Sidekiq 7+ if a mode is already set begin Sidekiq::Testing.fake! do @run_result = execution_class.run(@inputs) end rescue Sidekiq::Testing::TestModeAlreadySetError @run_result = execution_class.run(@inputs) end elsif @process_jobs && AsyncTestHelpers.active_job_testing? # Ensure the ActiveJob router is used to capture jobs in the :test adapter allow(RubyReactor.configuration).to receive(:async_router).and_return(RubyReactor::Adapters::ActiveJob::Router) @run_result = execution_class.run(@inputs) else @run_result = execution_class.run(@inputs) end # 4. Reload raise "Could not capture context ID during execution" unless captured_context_id # Reload using the execution class (which might be the mocked subclass with unique name) @reactor_instance = execution_class.find(captured_context_id) # Update our reference to the class so future reloads (e.g. in result introspection) work @reactor_class = execution_class @executed = true self end |
#run_async(boolean) ⇒ Object
214 215 216 217 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 214 def run_async(boolean) @async = boolean self end |
#step_result(name) ⇒ Object
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 452 def step_result(name) ensure_executed! # Prefer intermediate_results as it is the data store # Logic to handle symbol vs string mismatch key_sym = name.to_sym key_str = name.to_s if @reactor_instance.context.intermediate_results.key?(key_sym) @reactor_instance.context.intermediate_results[key_sym] elsif @reactor_instance.context.intermediate_results.key?(key_str) @reactor_instance.context.intermediate_results[key_str] else # Fallback to execution trace if available (e.g. for inspection) entry = @reactor_instance.context.execution_trace.find { |t| t[:step].to_s == key_str } entry ? entry[:result] : nil end end |
#success? ⇒ Boolean
339 340 341 342 |
# File 'lib/ruby_reactor/rspec/test_subject.rb', line 339 def success? ensure_executed! @reactor_instance.context.status.to_s == "completed" end |