Class: Polyn::Testing::MockPullSubscription

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

Overview

Mock Pull Subscription for applications to use in testing

Instance Method Summary collapse

Constructor Details

#initialize(mock_nats, **opts) ⇒ MockPullSubscription

Returns a new instance of MockPullSubscription.



8
9
10
11
12
13
14
15
16
# File 'lib/polyn/testing/mock_pull_subscription.rb', line 8

def initialize(mock_nats, **opts)
  @mock_nats       = mock_nats
  @real_nats       = mock_nats.nats
  @subject         = opts.fetch(:subject)
  @consumer_name   = opts.fetch(:consumer_name)
  @stream          = update_stream
  @delivery_cursor = 0
  @mock_nats.add_consumer(self)
end

Instance Method Details

#fetch(batch = 1, **_params) ⇒ Object



18
19
20
21
22
23
# File 'lib/polyn/testing/mock_pull_subscription.rb', line 18

def fetch(batch = 1, **_params)
  start_pos        = @delivery_cursor
  end_pos          = start_pos + batch - 1
  update_cursor(end_pos)
  @stream[start_pos..end_pos]
end

#update_cursor(end_pos) ⇒ Object



32
33
34
35
36
37
38
39
40
# File 'lib/polyn/testing/mock_pull_subscription.rb', line 32

def update_cursor(end_pos)
  next_pos = end_pos + 1

  @delivery_cursor = if @stream[next_pos]
                       next_pos
                     else
                       @stream.length
                     end
end

#update_streamObject



25
26
27
28
29
30
# File 'lib/polyn/testing/mock_pull_subscription.rb', line 25

def update_stream
  @stream = @mock_nats.messages.filter do |message|
    Polyn::Naming.subject_matches?(message.subject, @subject)
  end
  @stream
end