Class: Beacon::Testing::FakeTransport

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

Overview

Test transport that records every batch and lets the test decide what response to return next. No real HTTP, no sockets.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFakeTransport

Returns a new instance of FakeTransport.



42
43
44
45
46
# File 'lib/beacon/testing.rb', line 42

def initialize
  @batches  = []
  @mutex    = Mutex.new
  @next     = []  # queued Beacon::Transport::Result objects
end

Instance Attribute Details

#batchesObject (readonly)

Returns the value of attribute batches.



40
41
42
# File 'lib/beacon/testing.rb', line 40

def batches
  @batches
end

Instance Method Details

#post(body, idempotency_key:) ⇒ Object



48
49
50
51
52
53
54
55
56
# File 'lib/beacon/testing.rb', line 48

def post(body, idempotency_key:)
  @mutex.synchronize do
    @batches << { body: body, idempotency_key: idempotency_key }
    if (planned = @next.shift)
      return planned
    end
  end
  Beacon::Transport::Result.new(status: 202, retry_after: nil, error: nil)
end

#reset!Object



67
68
69
70
71
72
# File 'lib/beacon/testing.rb', line 67

def reset!
  @mutex.synchronize do
    @batches.clear
    @next.clear
  end
end

#respond_with(status: 202, retry_after: nil, error: nil) ⇒ Object

Queue a planned result for the next post call.



59
60
61
62
63
64
65
# File 'lib/beacon/testing.rb', line 59

def respond_with(status: 202, retry_after: nil, error: nil)
  @mutex.synchronize do
    @next << Beacon::Transport::Result.new(
      status: status, retry_after: retry_after, error: error,
    )
  end
end