Module: AMQP::SpecHelper

Defined in:
lib/amqp-spec/rspec.rb

Overview

AMQP::SpecHelper module defines #ampq and #em methods that can be safely used inside your specs (examples) to test code running inside AMQP.start or EM.run loop respectively. Each example is running in a separate event loop,you can control for timeouts either with :spec_timeout option given to #amqp/#em method or setting a default timeout using default_timeout(timeout) macro inside describe/context block.

noinspection RubyArgCount

Defined Under Namespace

Modules: GroupMethods

Constant Summary collapse

SpecTimeoutExceededError =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(example_group) ⇒ Object



104
105
106
107
108
# File 'lib/amqp-spec/rspec.rb', line 104

def self.included example_group
  unless example_group.respond_to? :default_timeout
    example_group.extend GroupMethods
  end
end

Instance Method Details

#amqp(opts = {}, &block) ⇒ Object

Yields to a given block inside EM.run and AMQP.start loops. This method takes any option that is accepted by EventMachine::connect. Options for AMQP.start include:

  • :user => String (default ‘guest’) - Username as defined by the AMQP server.

  • :pass => String (default ‘guest’) - Password as defined by the AMQP server.

  • :vhost => String (default ’/’) - Virtual host as defined by the AMQP server.

  • :timeout => Numeric (default nil) - Connection timeout, measured in seconds.

  • :logging => Bool (default false) - Toggle the extremely verbose AMQP logging.

In addition to EM and AMQP options, :spec_timeout option (in seconds) is used to force spec to timeout if something goes wrong and EM/AMQP loop hangs for some reason. SpecTimeoutExceededError is raised if it happens.



134
135
136
137
138
# File 'lib/amqp-spec/rspec.rb', line 134

def amqp opts={}, &block
  opts = default_options.merge opts
  @evented_example = AMQPExample.new(opts, self, &block)
  @evented_example.run
end

#default_optionsObject

Retrieves default options passed in from enclosing example groups



118
119
120
# File 'lib/amqp-spec/rspec.rb', line 118

def default_options
  @em_default_options ||= self.class.default_options.dup rescue {}
end

#done(*args, &block) ⇒ Object

Breaks the event loop and finishes the spec. This should be called after you are reasonably sure that your expectations either succeeded or failed. Done yields to any given block first, then stops EM event loop. For amqp specs, stops AMQP and cleans up AMQP state.

You may pass delay (in seconds) to done. If you do so, please keep in mind that your (default or explicit) spec timeout may fire before your delayed done callback is due, leading to SpecTimeoutExceededError



162
163
164
# File 'lib/amqp-spec/rspec.rb', line 162

def done *args, &block
  @evented_example.done *args, &block
end

#em(opts = {}, &block) ⇒ Object

Yields to block inside EM loop, :spec_timeout option (in seconds) is used to force spec to timeout if something goes wrong and EM/AMQP loop hangs for some reason. SpecTimeoutExceededError is raised if it happens.

For compatibility with EM-Spec API, em method accepts either options Hash or numeric timeout in seconds.



147
148
149
150
151
# File 'lib/amqp-spec/rspec.rb', line 147

def em opts = {}, &block
  opts = default_options.merge(opts.is_a?(Hash) ? opts : {spec_timeout: opts})
  @evented_example = EMExample.new(opts, self, &block)
  @evented_example.run
end

#metadataObject

Retrieves metadata passed in from enclosing example groups



112
113
114
# File 'lib/amqp-spec/rspec.rb', line 112

def 
  @em_metadata ||= self.class..dup rescue {}
end

#timeout(*args) ⇒ Object

Manually sets timeout for currently running example



168
169
170
# File 'lib/amqp-spec/rspec.rb', line 168

def timeout *args
  @evented_example.timeout *args
end