Module: AMQP::SpecHelper

Defined in:
lib/amqp-spec/rspec.rb,
lib/amqp-spec/evented_example.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 Classes: AMQPExample, EMExample, EventedExample

Constant Summary collapse

SpecTimeoutExceededError =
Class.new(RuntimeError)

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(example_group) ⇒ Object



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

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.



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

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



120
121
122
# File 'lib/amqp-spec/rspec.rb', line 120

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



164
165
166
# File 'lib/amqp-spec/rspec.rb', line 164

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.



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

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



114
115
116
# File 'lib/amqp-spec/rspec.rb', line 114

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

#timeout(*args) ⇒ Object

Manually sets timeout for currently running example



170
171
172
# File 'lib/amqp-spec/rspec.rb', line 170

def timeout(*args)
  @evented_example.timeout *args
end