File: read_only_consumer_spec.rb

package info (click to toggle)
ruby-bunny 2.23.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,644 kB
  • sloc: ruby: 10,256; sh: 70; makefile: 8
file content (60 lines) | stat: -rw-r--r-- 1,842 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
require "spec_helper"

describe Bunny::Queue, "#subscribe" do
  let(:publisher_connection) do
    c = Bunny.new(username: "bunny_gem", password: "bunny_password", vhost: "bunny_testbed")
    c.start
    c
  end

  let(:consumer_connection) do
    c = Bunny.new(username: "bunny_reader", password: "reader_password", vhost: "bunny_testbed")
    c.start
    c
  end

  after :each do
    publisher_connection.close if publisher_connection.open?
    consumer_connection.close  if consumer_connection.open?
  end

  context "with automatic acknowledgement mode" do
    let(:queue_name) { "bunny.basic_consume#{rand}" }

    it "registers the consumer" do
      delivered_keys = []
      delivered_data = []

      ch = publisher_connection.create_channel
      # declare the queue because the read-only user won't be able to issue
      # queue.declare
      q  = ch.queue(queue_name, auto_delete: true, durable: false)

      t = Thread.new do
        # give the main thread a bit of time to declare the queue
        sleep 0.5
        ch = consumer_connection.create_channel
        # this connection is read only, use passive declare to only get
        # a reference to the queue
        q  = ch.queue(queue_name, auto_delete: true, durable: false, passive: true)
        q.subscribe(exclusive: false) do |delivery_info, properties, payload|
          delivered_keys << delivery_info.routing_key
          delivered_data << payload
        end
      end
      t.abort_on_exception = true
      sleep 0.5

      x  = ch.default_exchange
      x.publish("hello", routing_key: queue_name)

      sleep 0.7
      expect(delivered_keys).to include(queue_name)
      expect(delivered_data).to include("hello")

      expect(ch.queue(queue_name, auto_delete: true, durable: false).message_count).to eq 0

      ch.close
    end
  end
end # describe