Module: AMQP

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

Overview

You can include one of the following modules into your example groups: AMQP::SpecHelper, AMQP::Spec, AMQP::EMSpec.

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.

If you include AMQP::Spec module into your example group, each example of this group will run inside AMQP.start loop without the need to explicitly call ‘amqp’. In order to provide options to AMQP loop, default_options(opts) macro is defined.

Including AMQP::EMSpec module into your example group, each example of this group will run inside EM.run loop without the need to explicitly call ‘em’.

In order to stop AMQP/EM loop, you should call ‘done’ AFTER you are sure that your example is finished and your expectations executed. For example if you are using subscribe block that tests expectations on messages, ‘done’ should be probably called at the end of this block.

Defined Under Namespace

Modules: EMSpec, Spec, SpecHelper

Class Method Summary collapse

Class Method Details

.cleanup_stateObject

Cleans up AMQP state after AMQP connection closes



24
25
26
27
28
29
30
# File 'lib/amqp-spec/amqp.rb', line 24

def self.cleanup_state
#   MQ.reset ?
  Thread.list.each { |thread| thread[:mq] = nil }
  Thread.list.each { |thread| thread[:mq_id] = nil }
  @conn    = nil
  @closing = false
end

.start_connection(opts = {}, &block) ⇒ Object

Initializes new AMQP client/connection without starting another EM loop



5
6
7
8
9
# File 'lib/amqp-spec/amqp.rb', line 5

def self.start_connection(opts={}, &block)
#    puts "!!!!!!!!! Existing connection: #{@conn}" if @conn
  @conn = connect opts
  @conn.callback(&block) if block
end

.stop_connectionObject

Closes AMQP connection gracefully



12
13
14
15
16
17
18
19
20
21
# File 'lib/amqp-spec/amqp.rb', line 12

def self.stop_connection
  if AMQP.conn and not AMQP.closing
#   MQ.reset ?
    @closing = true
    @conn.close {
      yield if block_given?
      cleanup_state
    }
  end
end