File: queue_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 (428 lines) | stat: -rw-r--r-- 11,297 bytes parent folder | download
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
require "spec_helper"

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

  after :each do
    connection.close if connection.open?
  end

  context "when queue name is specified" do
    let(:name) { "a queue declared at #{Bunny::Timestamp.now.to_i}" }

    it "declares a new queue with that name" do
      ch   = connection.create_channel

      q    = ch.queue(name)
      expect(q.name).to eq name

      q.delete
      ch.close
    end

    it "caches that queue" do
      ch   = connection.create_channel

      q = ch.queue(name)
      expect(ch.queue(name).object_id).to eq q.object_id

      q.delete
      ch.close
    end
  end


  context "when queue name is passed as an empty string" do
    it "uses server-assigned queue name" do
      ch   = connection.create_channel

      q = ch.queue("")
      expect(q.name).not_to be_empty
      expect(q.name).to match /^amq.gen.+/
      expect(q).to be_server_named
      q.delete

      ch.close
    end
  end


  context "when a nil is passed for queue name" do
    it "throws an error" do
      ch   = connection.create_channel

      expect {
        ch.queue(nil, durable: true, auto_delete: false)
      }.to raise_error(ArgumentError)
    end
  end


  context "when queue is declared as durable" do
    it "declares it as durable" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.durable", durable: true)
      expect(q).to be_durable
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q.arguments).to be_nil
      q.delete

      ch.close
    end
  end


  context "when queue is declared as exclusive" do
    it "declares it as exclusive" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.exclusive", exclusive: true)
      expect(q).to be_exclusive
      expect(q).not_to be_durable
      q.delete

      ch.close
    end
  end


  context "when queue is declared as auto-delete" do
    it "declares it as auto-delete" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.auto-delete", auto_delete: true)
      expect(q).to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).not_to be_durable
      q.delete

      ch.close
    end
  end

  context "when queue is declared with optional arguments" do
    it "declares it with those arguments" do
      ch   = connection.create_channel

      args = {
        Bunny::Queue::XArgs::MAX_LENGTH => 1000
      }
      q = ch.queue("bunny.tests.queues.x-args.1", durable: true, arguments: args)
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).to be_durable
      expect(q.arguments).to eq(args)
      q.delete

      ch.close
    end
  end

  context "when queue is declared with type using x-args and a literal string" do
    it "declares a queue of that type" do
      ch   = connection.create_channel

      args = {
        Bunny::Queue::XArgs::QUEUE_TYPE => "quorum"
      }
      q = ch.queue("bunny.tests.queues.x-args.2.qq", durable: true, arguments: args)
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).to be_durable
      expect(q.arguments).to eq(args)
      q.delete

      ch.close
    end
  end

  context "when queue is declared with type using x-args and a constant" do
    it "declares a queue of that type" do
      ch   = connection.create_channel

      args = {
        Bunny::Queue::XArgs::QUEUE_TYPE => Bunny::Queue::Types::QUORUM
      }
      q = ch.queue("bunny.tests.queues.x-args.2.qq", durable: true, arguments: args)
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).to be_durable
      expect(q.arguments).to eq(args)
      q.delete

      ch.close
    end
  end

  context "when queue is declared with type using :type and a literal string" do
    it "declares a queue of that type" do
      ch   = connection.create_channel

      args = {
        Bunny::Queue::XArgs::QUEUE_TYPE => "quorum"
      }
      q = ch.queue("bunny.tests.queues.x-args.3.qq", durable: true, type: Bunny::Queue::Types::QUORUM)
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).to be_durable
      expect(q.arguments).to eq(args)
      q.delete

      ch.close
    end
  end

  context "when queue is declared with type using :type and a constant" do
    it "declares a queue of that type" do
      ch   = connection.create_channel

      args = {
        Bunny::Queue::XArgs::QUEUE_TYPE => Bunny::Queue::Types::QUORUM
      }
      q = ch.queue("bunny.tests.queues.x-args.3.qq", durable: true, type: Bunny::Queue::Types::QUORUM)
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).to be_durable
      expect(q.arguments).to eq(args)
      q.delete

      ch.close
    end
  end

  context "when a quorum queue is declared using a helper" do
    it "declares a quorum queue" do
      ch   = connection.create_channel

      args = {
        Bunny::Queue::XArgs::MAX_LENGTH => 1000,
        Bunny::Queue::XArgs::QUEUE_TYPE => Bunny::Queue::Types::QUORUM
      }
      q = ch.quorum_queue("bunny.tests.queues.qq.1", durable: false, exclusive: true, arguments: {
        Bunny::Queue::XArgs::MAX_LENGTH => 1000
      })
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).to be_durable
      expect(q.arguments).to eq(args)
      q.delete

      ch.close
    end
  end

  context "when a stream 'queue' is declared using a helper" do
    it "declares a stream" do
      ch   = connection.create_channel

      args = {
        Bunny::Queue::XArgs::MAX_LENGTH => 1000,
        Bunny::Queue::XArgs::QUEUE_TYPE => Bunny::Queue::Types::STREAM
      }
      q = ch.stream("bunny.tests.queues.sq.1", durable: false, exclusive: true, arguments: {
        Bunny::Queue::XArgs::MAX_LENGTH => 1000
      })
      expect(q).not_to be_auto_delete
      expect(q).not_to be_exclusive
      expect(q).to be_durable
      expect(q.arguments).to eq(args)
      q.delete

      ch.close
    end
  end

  context "when queue is declared with an unsupported :type" do
    it "raises an exception" do
      ch   = connection.create_channel

      expect {
        ch.queue("bunny.tests.queues.unsupported.type", durable: true, type: "super-duper-q")
      }.to raise_error(ArgumentError)

      ch.close
    end
  end

  context "when queue is declared with a mismatching auto-delete property value" do
    it "raises an exception" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.auto-delete", auto_delete: true, durable: false)
      expect {
        # force re-declaration
        ch.queue_declare(q.name, auto_delete: false, durable: false)
      }.to raise_error(Bunny::PreconditionFailed)

      expect(ch).to be_closed

      cleanup_ch = connection.create_channel
      cleanup_ch.queue_delete(q.name)
    end
  end

  context "when queue is declared with a mismatching durable property value" do
    it "raises an exception" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.durable", durable: true)
      expect {
        # force re-declaration
        ch.queue_declare(q.name, durable: false)
      }.to raise_error(Bunny::PreconditionFailed)

      expect(ch).to be_closed

      cleanup_ch = connection.create_channel
      cleanup_ch.queue_delete(q.name)
    end
  end

  context "when queue is declared with a mismatching exclusive property value" do
    it "raises an exception" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.exclusive.#{rand}", exclusive: true)
      # when there's an exclusivity property mismatch, a different error
      # (405 RESOURCE_LOCKED) is used. This is a leaked queue exclusivity/ownership
      # implementation detail that's now basically a feature. MK.
      expect {
        # force re-declaration
        ch.queue_declare(q.name, exclusive: false)
      }.to raise_error(Bunny::ResourceLocked)

      expect(ch).to be_closed
    end
  end

  context "when queue is declared with a set of mismatching values" do
    it "raises an exception" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.proprty-equivalence", auto_delete: true, durable: false)
      expect {
        # force re-declaration
        ch.queue_declare(q.name, auto_delete: false, durable: true)
      }.to raise_error(Bunny::PreconditionFailed)

      expect(ch).to be_closed

      cleanup_ch = connection.create_channel
      cleanup_ch.queue_delete(q.name)
    end
  end


  context "when queue is declared with message TTL" do
    let(:args) do
      # in ms
      {"x-message-ttl" => 1000}
    end

    it "causes all messages in it to have a TTL" do
      ch   = connection.create_channel

      q = ch.queue("bunny.tests.queues.with-arguments.ttl", arguments:  args, exclusive: true)
      expect(q.arguments).to eq args

      q.publish("xyzzy")
      sleep 0.1

      expect(q.message_count).to eq 1
      sleep 1.5
      expect(q.message_count).to eq 0

      ch.close
    end
  end


  context "when queue is declared with priorities" do
    let(:args) do
      {"x-max-priority" => 5}
    end

    it "enables priority implementation" do
      c = Bunny.new(user: "bunny_gem", password: "bunny_password", vhost: "bunny_testbed")
      c.start

      ch   = c.create_channel
      ch.confirm_select

      q = ch.queue("bunny.tests.queues.with-arguments.priority #{rand}", arguments: args, exclusive: true)
      expect(q.arguments).to eq args

      q.publish("xyzzy")
      ch.wait_for_confirms
      sleep 0.1

      # this test only does sanity checking,
      # without trying to actually test prioritisation.
      #
      # added to guard against issues such as
      # https://github.com/rabbitmq/rabbitmq-server/issues/488
      expect(q.message_count).to eq 1

      ch.close
    end
  end


  describe "#queue_exists?" do
    context "when a queue exists" do
      it "returns true" do
        ch = connection.create_channel
        q  = ch.queue("", exlusive: true)

        expect(connection.queue_exists?(q.name)).to eq true
      end
    end

    context "when a queue DOES NOT exist" do
      it "returns false" do
        expect(connection.queue_exists?("suf89u9a4jo3ndnakls##{Bunny::Timestamp.now.to_i}")).to eq false
      end
    end
  end



  unless ENV["CI"]
    # requires RabbitMQ 3.1+
    context "when queue is declared with bounded length" do
      let(:n) { 10 }
      let(:args) do
        # in ms
        {"x-max-length" => n}
      end

      # see http://www.rabbitmq.com/maxlength.html for more info
      it "causes the queue to be bounded" do
        ch   = connection.create_channel

        q = ch.queue("bunny.tests.queues.with-arguments.max-length", arguments:  args, exclusive: true)
        expect(q.arguments).to eq args

        (n * 10).times do
          q.publish("xyzzy")
        end

        expect(q.message_count).to eq n
        (n * 5).times do
          q.publish("xyzzy")
        end

        expect(q.message_count).to eq n
        q.delete

        ch.close
      end
    end
  end
end