Class: BunnyMock::Session

Inherits:
Object
  • Object
show all
Defined in:
lib/bunny_mock/session.rb

Overview

Mocks Bunny::Session

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSession

Creates a new BunnyMock::Session instance



22
23
24
25
26
27
28
29
30
31
32
# File 'lib/bunny_mock/session.rb', line 22

def initialize(*)
  # not connected until {BunnyMock::Session#start} is called
  @status = :not_connected

  # create channel hash
  @channels = {}

  # create storage for queues and exchanges
  @queues    = {}
  @exchanges = {}
end

Instance Attribute Details

#exchangesHash<String, BunnyMock::Exchange> (readonly)

Returns Exchanges created by this channel.

Returns:



13
14
15
# File 'lib/bunny_mock/session.rb', line 13

def exchanges
  @exchanges
end

#queuesHash<String, BunnyMock::Queue> (readonly)

Returns Queues created by this channel.

Returns:



16
17
18
# File 'lib/bunny_mock/session.rb', line 16

def queues
  @queues
end

#statusSymbol (readonly)

Returns Current session status.

Returns:

  • (Symbol)

    Current session status



10
11
12
# File 'lib/bunny_mock/session.rb', line 10

def status
  @status
end

Instance Method Details

#closed?Boolean

Tests if connection is closed

Returns:

  • (Boolean)

    true if status is closed, false otherwise



70
71
72
# File 'lib/bunny_mock/session.rb', line 70

def closed?
  @status == :closed
end

#closing?Boolean

Tests if connection is closing

Returns:

  • (Boolean)

    true if status is closing, false otherwise



79
80
81
# File 'lib/bunny_mock/session.rb', line 79

def closing?
  @status == :closing
end

#create_channel(n = nil, _pool_size = 1, *_args) ⇒ BunnyMock::Channel Also known as: channel

Creates a new Channel instance

Parameters:

  • n (Integer) (defaults to: nil)

    Channel identifier

  • pool_size (Integer)

    Work pool size (insignificant)

Returns:

Raises:

  • (ArgumentError)


91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/bunny_mock/session.rb', line 91

def create_channel(n = nil, _pool_size = 1, *_args)
  # raise same error as {Bunny::Session#create_channel}
  raise ArgumentError, 'channel number 0 is reserved in the protocol and cannot be used' if n && n.zero?

  # return cached channel if exists
  return @channels[n] if n && @channels.key?(n)

  # create and open channel
  channel = Channel.new self, n
  channel.open

  # return channel
  @channels[n] = channel
end

#exchange_exists?(name) ⇒ Boolean

Test if exchange exists in channel cache

Parameters:

  • name (String)

    Name of exchange

Returns:

  • (Boolean)

    true if exchange exists, false otherwise



146
147
148
# File 'lib/bunny_mock/session.rb', line 146

def exchange_exists?(name)
  !find_exchange(name).nil?
end

#open?Boolean Also known as: connected?

Tests if connection is available

Returns:

  • (Boolean)

    true if status is connected, false otherwise



60
61
62
# File 'lib/bunny_mock/session.rb', line 60

def open?
  @status == :connected
end

#queue_exists?(name) ⇒ Boolean

Test if queue exists in channel cache

Parameters:

  • name (String)

    Name of queue

Returns:

  • (Boolean)

    true if queue exists, false otherwise



134
135
136
# File 'lib/bunny_mock/session.rb', line 134

def queue_exists?(name)
  !find_queue(name).nil?
end

#startBunnyMock::Session

Sets status to connected

Returns:



39
40
41
42
# File 'lib/bunny_mock/session.rb', line 39

def start
  @status = :connected
  self
end

#stopBunnyMock::Session Also known as: close

Sets status to closed

Returns:



49
50
51
52
# File 'lib/bunny_mock/session.rb', line 49

def stop
  @status = :closed
  self
end

#with_channel(n = nil) ⇒ BunnyMock::Session

Creates a temporary Channel instance, yields it to the block given, then closes it

Parameters:

  • n (Integer) (defaults to: nil)

    Channel identifier

Returns:



115
116
117
118
119
120
121
122
123
124
# File 'lib/bunny_mock/session.rb', line 115

def with_channel(n = nil)
  ch = create_channel(n)
  begin
    yield ch
  ensure
    ch.close if ch.open?
  end

  self
end