Class: JetstreamBridge::TestHelpers::MockNats::MockConnection
- Inherits:
-
Object
- Object
- JetstreamBridge::TestHelpers::MockNats::MockConnection
- Defined in:
- lib/jetstream_bridge/test_helpers/mock_nats.rb
Instance Attribute Summary collapse
-
#callbacks ⇒ Object
readonly
Returns the value of attribute callbacks.
-
#connected_at ⇒ Object
readonly
Returns the value of attribute connected_at.
Instance Method Summary collapse
- #close ⇒ Object
- #connect(_urls = nil, **_options) ⇒ Object
- #connected? ⇒ Boolean
-
#initialize ⇒ MockConnection
constructor
A new instance of MockConnection.
- #jetstream ⇒ Object
- #on_disconnect(&block) ⇒ Object
- #on_error(&block) ⇒ Object
- #on_reconnect(&block) ⇒ Object
-
#request(subject, _payload, timeout: 1) ⇒ Object
Simulate JetStream API requests Used by overlap_guard.rb for stream management operations.
- #rtt ⇒ Object
-
#simulate_disconnect! ⇒ Object
Test helpers for simulating connection events.
- #simulate_error!(error) ⇒ Object
- #simulate_reconnect! ⇒ Object
Constructor Details
#initialize ⇒ MockConnection
11 12 13 14 15 16 17 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 11 def initialize @connected = false @connected_at = nil @callbacks = { reconnect: [], disconnect: [], error: [] } # Use global storage to ensure persistence across test helper calls @jetstream = MockJetStream.new(MockNats.storage) end |
Instance Attribute Details
#callbacks ⇒ Object (readonly)
Returns the value of attribute callbacks.
9 10 11 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 9 def callbacks @callbacks end |
#connected_at ⇒ Object (readonly)
Returns the value of attribute connected_at.
9 10 11 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 9 def connected_at @connected_at end |
Instance Method Details
#close ⇒ Object
98 99 100 101 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 98 def close @connected = false @callbacks[:disconnect].each(&:call) end |
#connect(_urls = nil, **_options) ⇒ Object
19 20 21 22 23 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 19 def connect(_urls = nil, **) @connected = true @connected_at = Time.now self end |
#connected? ⇒ Boolean
31 32 33 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 31 def connected? @connected end |
#jetstream ⇒ Object
25 26 27 28 29 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 25 def jetstream raise NATS::IO::NoRespondersError, 'JetStream not available' unless @connected @jetstream end |
#on_disconnect(&block) ⇒ Object
39 40 41 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 39 def on_disconnect(&block) @callbacks[:disconnect] << block if block end |
#on_error(&block) ⇒ Object
43 44 45 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 43 def on_error(&block) @callbacks[:error] << block if block end |
#on_reconnect(&block) ⇒ Object
35 36 37 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 35 def on_reconnect(&block) @callbacks[:reconnect] << block if block end |
#request(subject, _payload, timeout: 1) ⇒ Object
Simulate JetStream API requests Used by overlap_guard.rb for stream management operations
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 53 def request(subject, _payload, timeout: 1) raise NATS::IO::NoRespondersError, 'Not connected' unless @connected # Parse the API request response_data = case subject when '$JS.API.STREAM.NAMES' # Return list of stream names stream_names = @jetstream.storage.streams.keys.map { |name| { 'name' => name } } { 'type' => 'io.nats.jetstream.api.v1.stream_names_response', 'total' => stream_names.size, 'offset' => 0, 'limit' => 1024, 'streams' => stream_names } when /^\$JS\.API\.STREAM\.INFO\.(.+)$/ # Return stream info for specific stream stream_name = ::Regexp.last_match(1) stream = @jetstream.storage.find_stream(stream_name) if stream info = stream.info { 'type' => 'io.nats.jetstream.api.v1.stream_info_response', 'config' => { 'name' => info.config.name, 'subjects' => info.config.subjects } } else { 'error' => { 'code' => 404, 'description' => 'stream not found' } } end else # Generic response for unknown API calls { 'type' => 'io.nats.jetstream.api.v1.response' } end # Return a mock message object with the response data MockApiResponse.new(Oj.dump(response_data, mode: :compat)) end |
#rtt ⇒ Object
47 48 49 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 47 def rtt 0.001 # 1ms simulated round-trip time end |
#simulate_disconnect! ⇒ Object
Test helpers for simulating connection events
104 105 106 107 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 104 def simulate_disconnect! @connected = false @callbacks[:disconnect].each(&:call) end |
#simulate_error!(error) ⇒ Object
114 115 116 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 114 def simulate_error!(error) @callbacks[:error].each { |cb| cb.call(error) } end |
#simulate_reconnect! ⇒ Object
109 110 111 112 |
# File 'lib/jetstream_bridge/test_helpers/mock_nats.rb', line 109 def simulate_reconnect! @connected = true @callbacks[:reconnect].each(&:call) end |