Class: Pikuri::Testing::Gate

Inherits:
Object
  • Object
show all
Defined in:
lib/pikuri/testing.rb

Overview

A latch that holds a fake turn open mid-flight. Pass one to fake_transport; the adapter parks (inside the HTTP call, on the agent's worker thread) at every response while the gate is engaged, so a test can observe the turn-in-progress state — spinner, queued injections, a Ctrl+C landing mid-turn — then let it finish.

gate = Pikuri::Testing::Gate.new
transport = Pikuri::Testing.fake_transport(gate:) { |llm| llm.stream('done') }
gate.block!
session.submit('work')          # the real turn parks in the adapter
# ... assert spinner / press Ctrl+C ...
gate.release!                    # the turn proceeds and completes

A test that engages the gate is responsible for releasing it — #await blocks with no timeout, mirroring the old stubs' Queue#pop.

Thread-safe.

Instance Method Summary collapse

Constructor Details

#initializeGate

Returns a new instance of Gate.



260
261
262
263
264
# File 'lib/pikuri/testing.rb', line 260

def initialize
  @mutex = Mutex.new
  @cv = ConditionVariable.new
  @blocked = false
end

Instance Method Details

#awaitvoid

This method returns an undefined value.

Block the calling thread while the latch is engaged. Called by the adapter before serving each response; a no-op when not blocked.



290
291
292
293
# File 'lib/pikuri/testing.rb', line 290

def await
  @mutex.synchronize { @cv.wait(@mutex) while @blocked }
  nil
end

#block!void

This method returns an undefined value.

Engage the latch: the next (and every subsequent) response parks in #await until #release!.



270
271
272
273
# File 'lib/pikuri/testing.rb', line 270

def block!
  @mutex.synchronize { @blocked = true }
  nil
end

#release!void

This method returns an undefined value.

Disengage the latch and wake every parked response.



278
279
280
281
282
283
284
# File 'lib/pikuri/testing.rb', line 278

def release!
  @mutex.synchronize do
    @blocked = false
    @cv.broadcast
  end
  nil
end