File: exchange_declare_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 (237 lines) | stat: -rw-r--r-- 5,384 bytes parent folder | download | duplicates (5)
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
require "spec_helper"

describe Bunny::Exchange do
  let(:connection) do
    c = Bunny.new(username: "bunny_gem", password: "bunny_password", vhost: "bunny_testbed")
    c.start
    c
  end

  after :each do
    connection.close
  end

  context "of default type" do
    it "is declared with an empty name" do
      ch = connection.create_channel

      x = Bunny::Exchange.default(ch)

      expect(x.name).to eq ''
    end
  end

  context "of type fanout" do
    context "with a non-predefined name" do
      it "is declared" do
        ch = connection.create_channel

        name = "bunny.tests.exchanges.fanout#{rand}"
        x    = ch.fanout(name)
        expect(x.name).to eq name

        x.delete
        ch.close
      end
    end

    context "with a predefined name" do
      it "is NOT declared" do
        ch = connection.create_channel

        name = "amq.fanout"
        x    = ch.fanout(name)
        expect(x.name).to eq name

        ch.close
      end
    end

    context "with a name prefixed with 'amq.'" do
      it "raises an exception" do
        ch   = connection.create_channel

        expect {
          ch.fanout("amq.test")
        }.to raise_error(Bunny::AccessRefused)

        expect(ch).to be_closed
        expect {
          ch.fanout("amq.test")
        }.to raise_error(Bunny::ChannelAlreadyClosed)
      end
    end

    context "with the durable property" do
      it "is declared as durable" do
        ch = connection.create_channel

        name = "bunny.tests.exchanges.durable"
        x    = ch.fanout(name, durable: true)
        expect(x.name).to eq name
        expect(x).to be_durable
        expect(x).not_to be_auto_delete

        x.delete
        ch.close
      end
    end


    context "with the auto-delete property" do
      it "is declared as auto-delete" do
        ch = connection.create_channel

        name = "bunny.tests.exchanges.auto-delete"
        x    = ch.fanout(name, auto_delete: true)
        expect(x.name).to eq name
        expect(x).not_to be_durable
        expect(x).to be_auto_delete

        ch.exchange(name, type: :fanout, auto_delete: true)

        x.delete
        ch.close
      end
    end


    context "when declared with a different set of attributes" do
      it "raises an exception" do
        ch1   = connection.create_channel
        ch2   = connection.create_channel

        x = ch1.fanout("bunny.tests.exchanges.fanout", auto_delete: true, durable: false)
        expect {
          # force re-declaration
          ch2.exchange_declare("bunny.tests.exchanges.fanout", :direct, auto_delete: false, durable: true)
        }.to raise_error(Bunny::PreconditionFailed)

        expect(ch2).to be_closed
        expect {
          ch2.fanout("bunny.tests.exchanges.fanout", auto_delete: true, durable: false)
        }.to raise_error(Bunny::ChannelAlreadyClosed)
      end
    end
  end

  context "of type direct" do
    context "with a non-predefined name" do
      it "is declared" do
        ch = connection.create_channel

        name = "bunny.tests.exchanges.direct"
        x    = ch.direct(name)
        expect(x.name).to eq name

        ch.exchange(name, type: :direct)

        x.delete
        ch.close
      end
    end

    context "with a predefined name" do
      it "is NOT declared" do
        ch = connection.create_channel

        name = "amq.direct"
        x    = ch.direct(name)
        expect(x.name).to eq name

        ch.close
      end
    end
  end

  context "of type topic" do
    context "with a non-predefined name" do
      it "is declared" do
        ch = connection.create_channel

        name = "bunny.tests.exchanges.topic"
        x    = ch.topic(name)
        expect(x.name).to eq name

        ch.exchange(name, type: :topic)

        x.delete
        ch.close
      end
    end

    context "with a predefined name" do
      it "is NOT declared" do
        ch = connection.create_channel

        name = "amq.topic"
        x    = ch.topic(name)
        expect(x.name).to eq name

        ch.close
      end
    end
  end

  context "of type headers" do
    context "with a non-predefined name" do
      it "is declared" do
        ch = connection.create_channel

        name = "bunny.tests.exchanges.headers"
        x    = ch.headers(name)
        expect(x.name).to eq name

        x.delete
        ch.close
      end
    end

    context "with a predefined name (amq.match)" do
      it "is NOT declared" do
        ch = connection.create_channel

        name = "amq.match"
        x    = ch.headers(name)
        expect(x.name).to eq name

        ch.close
      end
    end

    context "with a predefined name (amq.headers)" do
      it "is NOT declared" do
        ch = connection.create_channel

        name = "amq.headers"
        x    = ch.headers(name)
        expect(x.name).to eq name

        ch.close
      end
    end
  end


  context "that is internal" do
    it "can be declared" do
      ch = connection.create_channel
      x  = ch.fanout("bunny.tests.exchanges.internal", internal: true)
      expect(x).to be_internal
      x.delete

      ch.close
    end
  end

  context "not declared as internal" do
    it "is not internal" do
      ch = connection.create_channel
      x  = ch.fanout("bunny.tests.exchanges.non-internal")
      expect(x).not_to be_internal
      x.delete

      ch.close
    end
  end
end