Module: Amigo::SpecHelpers

Defined in:
lib/amigo/spec_helpers.rb

Defined Under Namespace

Classes: EventPublishedMatcher, PerformAsyncJobMatcher, ServerCallbackMiddleware

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.all_sidekiq_jobs(q) ⇒ Object



276
277
278
279
280
# File 'lib/amigo/spec_helpers.rb', line 276

module_function def all_sidekiq_jobs(q)
  arr = []
  q.each { |j| arr << j }
  return arr
end

.drain_sidekiq_jobs(q) ⇒ Object



267
268
269
270
271
272
273
274
# File 'lib/amigo/spec_helpers.rb', line 267

module_function def drain_sidekiq_jobs(q)
  all_sidekiq_jobs(q).each do |job|
    klass = job.item.fetch("class")
    klass = Sidekiq::Testing.constantize(klass) if klass.is_a?(String)
    sidekiq_perform_inline(klass, job.item["args"], job.item)
    job.delete
  end
end

.included(context) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/amigo/spec_helpers.rb', line 8

def self.included(context)
  context.before(:each) do |example|
    Amigo.synchronous_mode = true if example.[:async]
  end
  context.after(:each) do |example|
    Amigo.synchronous_mode = false if example.[:async]
  end
  super
end

.sidekiq_perform_inline(klass, args, item = nil) ⇒ Object

Like a Sidekiq worker’s perform_inline, but allows an arbitrary item to be used, rather than just the given class and args. For example, when testing, you may need to assume something like ‘retry_count’ is in the job payload, but that can’t be included with perform_inline. This allows those arbitrary job payload fields to be included when the job is run.



258
259
260
261
262
263
264
265
# File 'lib/amigo/spec_helpers.rb', line 258

module_function def sidekiq_perform_inline(klass, args, item=nil)
  Sidekiq::Worker::Setter.override_item = item
  begin
    klass.perform_inline(*args)
  ensure
    Sidekiq::Worker::Setter.override_item = nil
  end
end

.snapshot_async_state(opts = {}) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/amigo/spec_helpers.rb', line 18

module_function def snapshot_async_state(opts={})
  old_subscribers = Amigo.subscribers.to_a
  old_jobs = Amigo.registered_jobs.to_a
  old_failure = Amigo.on_publish_error

  new_subscribers = opts.fetch(:subscribers, [])
  new_jobs = opts.fetch(:jobs, [])

  @active_snapshots ||= 0
  if @active_snapshots.positive?
    new_subscribers = old_subscribers + new_subscribers
    new_jobs = old_jobs = new_jobs
  end
  begin
    Amigo.on_publish_error = opts[:on_error] if opts.key?(:on_error)
    Amigo.subscribers.replace(new_subscribers) if opts.key?(:subscribers)
    Amigo.registered_jobs.replace(new_jobs) if opts.key?(:jobs)
    @active_snapshots += 1
    yield
  ensure
    @active_snapshots -= 1
    Amigo.on_publish_error = old_failure
    Amigo.subscribers.replace(old_subscribers)
    Amigo.registered_jobs.replace(old_jobs)
  end
end

Instance Method Details

#perform_async_job(job) ⇒ Object



247
248
249
# File 'lib/amigo/spec_helpers.rb', line 247

def perform_async_job(job)
  return PerformAsyncJobMatcher.new(job)
end

#publish(eventname = nil, expected_payload = nil) ⇒ Object

RSpec matcher – set up an expectation that an event will be fired with the specified eventname and optional expected_payload.

expect {
    Myapp::Customer.create( attributes )
}.to publish( 'myapp.customer.create' )

expect {
    Myapp::Customer.create( attributes )
}.to publish( 'myapp.customer.create', [1] )

expect { enter_hatch() }.
    to publish( 'myapp.hatch.entered' ).
    with_payload( [4, 8, 15, 16, 23, 42] )

expect { cook_potatoes() }.
    to publish( 'myapp.potatoes.cook' ).
    with_payload( including( a_hash_containing( taste: 'good' ) ) )


190
191
192
# File 'lib/amigo/spec_helpers.rb', line 190

def publish(eventname=nil, expected_payload=nil)
  return EventPublishedMatcher.new(eventname, expected_payload)
end