File: basic_ack_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 (230 lines) | stat: -rw-r--r-- 7,212 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
require "spec_helper"

describe Bunny::Channel, "#ack" 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 if connection.open?
  end

  context "with a valid (known) delivery tag" do
    it "acknowledges a message" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 1
      delivery_details, properties, content = q.pop(manual_ack: true)

      ch.ack(delivery_details.delivery_tag, true)
      ch.close

      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      expect(q.message_count).to eq 0
      ch.close
    end
  end

  context "with a valid (known) delivery tag (multiple = true)" do
    it "acknowledges a message" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 2
      delivery_details_1, _properties, _content = q.pop(manual_ack: true)
      delivery_details_2, _properties, _content = q.pop(manual_ack: true)

      ch.ack(delivery_details_2.delivery_tag, true)
      ch.close

      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      expect(q.message_count).to eq 0
      ch.close
    end
  end

  context "with a valid (known) delivery tag (multiple = false)" do
    it "acknowledges a message" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 2
      delivery_details_1, _properties, _content = q.pop(manual_ack: true)
      delivery_details_2, _properties, _content = q.pop(manual_ack: true)

      ch.ack(delivery_details_2.delivery_tag, false)
      ch.close

      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      expect(q.message_count).to eq 1
      ch.close
    end
  end

  context "with a valid (known) delivery tag and automatic ack mode" do
    it "results in a channel exception" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      q.subscribe(manual_ack: false) do |delivery_info, properties, payload|
        ch.ack(delivery_info.delivery_tag, false)
      end

      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect do
        q.message_count
      end.to raise_error(Bunny::ChannelAlreadyClosed)
    end
  end

  context "with an invalid (random) delivery tag" do
    it "causes a channel-level error" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.unknown-delivery-tag", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 1
      _, _, content = q.pop(manual_ack: true)

      ch.on_error do |ch, channel_close|
        @channel_close = channel_close
      end
      ch.ack(82, true)
      sleep 0.25

      expect(@channel_close.reply_code).to eq AMQ::Protocol::PreconditionFailed::VALUE
    end
  end

  context "with a valid (known) delivery tag" do
    it "gets a depricated message warning for using :ack" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 1

      orig_stderr = $stderr
      $stderr = StringIO.new

      delivery_details, properties, content = q.pop(ack: true)

      $stderr.rewind
      expect($stderr.string.chomp).to eq("[DEPRECATION] `:ack` is deprecated.  Please use `:manual_ack` instead.\n[DEPRECATION] `:ack` is deprecated.  Please use `:manual_ack` instead.")

      $stderr = orig_stderr

      ch.ack(delivery_details.delivery_tag, true)
      ch.close

      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      expect(q.message_count).to eq 0
      ch.close
    end
  end
end

describe Bunny::Channel, "#basic_ack" 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 if connection.open?
  end

  context "with a valid (known) delivery tag (multiple = true)" do
    it "acknowledges a message" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 2
      delivery_details_1, _properties, _content = q.pop(manual_ack: true)
      delivery_details_2, _properties, _content = q.pop(manual_ack: true)

      ch.basic_ack(delivery_details_2.delivery_tag.to_i, true)
      ch.close

      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      expect(q.message_count).to eq 0
      ch.close
    end
  end

  context "with a valid (known) delivery tag (multiple = false)" do
    it "acknowledges a message" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 2
      delivery_details_1, _properties, _content = q.pop(manual_ack: true)
      delivery_details_2, _properties, _content = q.pop(manual_ack: true)

      ch.basic_ack(delivery_details_2.delivery_tag.to_i, false)
      ch.close

      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      expect(q.message_count).to eq 1
      ch.close
    end
  end

  context "with a valid (known) delivery tag (multiple = default)" do
    it "acknowledges a message" do
      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      x  = ch.default_exchange

      x.publish("bunneth", routing_key: q.name)
      x.publish("bunneth", routing_key: q.name)
      sleep 0.5
      expect(q.message_count).to eq 2
      delivery_details_1, _properties, _content = q.pop(manual_ack: true)
      delivery_details_2, _properties, _content = q.pop(manual_ack: true)

      ch.basic_ack(delivery_details_2.delivery_tag.to_i)
      ch.close

      ch = connection.create_channel
      q  = ch.queue("bunny.basic.ack.manual-acks", exclusive: true)
      expect(q.message_count).to eq 1
      ch.close
    end
  end
end