summaryrefslogtreecommitdiff
path: root/qpid/tests/src/py/qpid_tests/broker_0_10/new_api.py
blob: 4e9439512101b25b1dc00442af242dfd69765299 (plain)
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
#
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.
#

from qpid.tests.messaging.implementation import *
from qpid.tests.messaging import Base
from qpidtoollibs import BrokerAgent
from time import sleep

#
# Broker tests using the new messaging API
#

class GeneralTests(Base):
    """
    Tests of the API and broker via the new API.
    """

    def assertEqual(self, left, right, text=None):
        if not left == right:
            print "assertEqual failure: %r != %r" % (left, right)
            if text:
                print "  %r" % text
            assert None

    def fail(self, text=None):
        if text:
            print "Fail: %r" % text
        assert None

    def setup_connection(self):
        return Connection.establish(self.broker, **self.connection_options())

    def setup_session(self):
        return self.conn.session()

    def test_not_found(self):
        ssn = self.setup_session()
        try:
            ssn.receiver("does-not-exist")
            self.fail("Expected non-existent node to cause NotFound exception")
        except NotFound, e: None

    def test_qpid_3481_acquired_to_alt_exchange(self):
        """
        Verify that acquired messages are routed to the alternate when the queue is deleted.
        """
        sess1 = self.setup_session()
        sess2 = self.setup_session()

        tx = sess1.sender("amq.direct/key")
        rx_main = sess1.receiver("amq.direct/key;{link:{reliability:at-least-once,x-declare:{alternate-exchange:'amq.fanout'}}}")
        rx_alt  = sess2.receiver("amq.fanout")
        rx_alt.capacity = 10

        tx.send("DATA")
        tx.send("DATA")
        tx.send("DATA")
        tx.send("DATA")
        tx.send("DATA")

        msg = rx_main.fetch()
        msg = rx_main.fetch()
        msg = rx_main.fetch()

        self.assertEqual(rx_alt.available(), 0, "No messages should have been routed to the alt_exchange")

        sess1.close()
        sleep(1)
        self.assertEqual(rx_alt.available(), 5, "All 5 messages should have been routed to the alt_exchange")

        sess2.close()

    def test_qpid_3481_acquired_to_alt_exchange_2_consumers(self):
        """
        Verify that acquired messages are routed to the alternate when the queue is deleted.
        """
        sess1 = self.setup_session()
        sess2 = self.setup_session()
        sess3 = self.setup_session()
        sess4 = self.setup_session()

        tx = sess1.sender("test_acquired;{create:always,delete:always,node:{x-declare:{alternate-exchange:'amq.fanout'}}}")
        rx_main1 = sess2.receiver("test_acquired")
        rx_main2 = sess3.receiver("test_acquired")
        rx_alt   = sess4.receiver("amq.fanout")
        rx_alt.capacity = 10

        tx.send("DATA")
        tx.send("DATA")
        tx.send("DATA")
        tx.send("DATA")
        tx.send("DATA")

        msg = rx_main1.fetch()
        msg = rx_main1.fetch()
        msg = rx_main1.fetch()

        self.assertEqual(rx_alt.available(), 0, "No messages should have been routed to the alt_exchange")

        # Close sess1; This will cause the queue to be deleted and all its messages (including those acquired) to be reouted to the alternate exchange
        sess1.close()
        sleep(1)
        self.assertEqual(rx_alt.available(), 5, "All the messages should have been routed to the alt_exchange")

        # Close sess2; This will cause the acquired messages to be requeued and routed to the alternate
        sess2.close()
        for i in range(5):
            try:
                m = rx_alt.fetch(0)
            except:
                self.fail("failed to receive all 5 messages via alternate exchange")

        sess3.close()
        self.assertEqual(rx_alt.available(), 0, "No further messages should be received via the alternate exchange")

        sess4.close()

    def test_next_receiver(self):
        keys = ["a", "b", "c"]
        receivers = [self.ssn.receiver("amq.direct/%s" % k) for k in keys]
        for r in receivers:
            r.capacity = 10

        snd = self.ssn.sender("amq.direct")

        for k in keys:
            snd.send(Message(subject=k, content=k))

        expected = keys
        while len(expected):
            rcv = self.ssn.next_receiver(timeout=self.delay())
            c = rcv.fetch().content
            assert c in expected
            expected.remove(c)
        self.ssn.acknowledge()

    def test_nolocal_rerouted(self):
        conn2 = Connection.establish(self.broker, **self.connection_options())
        ssn2 = conn2.session()

        s1 = self.ssn.sender("holding_q; {create:always, delete:always, node:{x-declare:{alternate-exchange:'amq.fanout'}}}");
        s2 = ssn2.sender("holding_q");

        s2.send(Message("a"));
        s1.send(Message("b"));
        s2.send(Message("c"));

        r = self.ssn.receiver("amq.fanout; {link:{x-declare:{arguments:{'no-local':True}}}}")

        # close connection of one of the publishers
        conn2.close()

        # close sender which should cause the orphaned messages on
        # holding_q to be rerouted through alternate exchange onto the
        # subscription queue of the receiver above
        s1.close()

        received = []
        try:
            while True:
                received.append(r.fetch(0).content)
        except Empty: pass
        self.assertEqual(received, ["a", "c"])

    def _node_disambiguation_test(self, e, q, ambiguous_send=False):
        s1 = self.ssn.sender("ambiguous; {node:{type:topic}}");
        s2 = self.ssn.sender("ambiguous; {node:{type:queue}}");
        s1.send(Message("a"))
        s2.send(Message("b"))
        if ambiguous_send:
            # pure python client defaults to using the queue when the
            # node name is ambiguous and no type is specified; the
            # swigged version treats this as an error
            s3 = self.ssn.sender("ambiguous");
            s3.send(Message("c"))
        self.assertEqual(e.fetch().content, "a")
        self.assertEqual(q.fetch().content, "b")
        if ambiguous_send:
            self.assertEqual(q.fetch().content, "c")
        for r in [e, q]:
            try:
                m = r.fetch(timeout=0)
                self.fail("Found unexpected message %s")
            except Empty: pass

    def _node_disambiguation_precreated(self, ambiguous_send):
        agent = BrokerAgent(self.conn)
        agent.addExchange("fanout", "ambiguous")
        agent.addQueue("ambiguous")
        try:
            r1 = self.ssn.receiver("ambiguous; {node:{type:topic}}")
            r2 = self.ssn.receiver("ambiguous; {node:{type:queue}}")
            self._node_disambiguation_test(r1, r2, ambiguous_send=ambiguous_send)
        finally:
            agent.delExchange("ambiguous")
            agent.delQueue("ambiguous", False, False)

    def test_node_disambiguation_1(self):
        self._node_disambiguation_precreated(False)

    def test_node_disambiguation_2(self):
        self._node_disambiguation_precreated(True)

    def test_ambiguous_create_1(self):
        #create queue first, then exchange
        r1 = self.ssn.receiver("ambiguous; {create:receiver, node:{type:queue}}")
        r2 = self.ssn.receiver("ambiguous; {create:receiver, node:{type:topic}}")
        agent = BrokerAgent(self.conn)
        exchange = agent.getExchange("ambiguous")
        queue = agent.getQueue("ambiguous")
        try:
            assert(exchange)
            assert(queue)
            self._node_disambiguation_test(r2, r1)
        finally:
            if exchange: agent.delExchange("ambiguous")
            if queue: agent.delQueue("ambiguous", False, False)

    def test_ambiguous_create_2(self):
        #create exchange first, then queue
        r1 = self.ssn.receiver("ambiguous; {create:receiver, node:{type:topic}}")
        r2 = self.ssn.receiver("ambiguous; {create:receiver, node:{type:queue}}")
        agent = BrokerAgent(self.conn)
        exchange = agent.getExchange("ambiguous")
        queue = agent.getQueue("ambiguous")
        try:
            assert(exchange)
            assert(queue)
            self._node_disambiguation_test(r1, r2)
        finally:
            if exchange: agent.delExchange("ambiguous")
            if queue: agent.delQueue("ambiguous", False, False)

    def test_ambiguous_delete_1(self):
        agent = BrokerAgent(self.conn)
        agent.addExchange("fanout", "ambiguous")
        agent.addQueue("ambiguous")
        self.ssn.receiver("ambiguous; {delete:receiver, node:{type:topic}}").close()
        exchange = agent.getExchange("ambiguous")
        queue = agent.getQueue("ambiguous")
        try:
            assert(not exchange)
            assert(queue)
        finally:
            if exchange: agent.delExchange("ambiguous")
            if queue: agent.delQueue("ambiguous", False, False)

    def test_ambiguous_delete_2(self):
        agent = BrokerAgent(self.conn)
        agent.addExchange("fanout", "ambiguous")
        agent.addQueue("ambiguous")
        self.ssn.receiver("ambiguous; {delete:receiver, node:{type:queue}}").close()
        exchange = agent.getExchange("ambiguous")
        queue = agent.getQueue("ambiguous")
        try:
            assert(exchange)
            assert(not queue)
        finally:
            if exchange: agent.delExchange("ambiguous")
            if queue: agent.delQueue("ambiguous", False, False)


class SequenceNumberTests(Base):
    """
    Tests of ring queue sequence number
    """

    def fail(self, text=None):
        if text:
            print "Fail: %r" % text
        assert None

    def setup_connection(self):
        return Connection.establish(self.broker, **self.connection_options())

    def setup_session(self):
        return self.conn.session()

    def setup_sender(self, name="ring-sequence-queue", key="qpid.queue_msg_sequence"):
        addr = "%s; {create:sender, node: {x-declare: {auto-delete: True, arguments: {'qpid.queue_msg_sequence':'%s', 'qpid.policy_type':'ring', 'qpid.max_count':4}}}}"  % (name, key)
        sender = self.ssn.sender(addr)
        return sender

    def test_create_sequence_queue(self):
        """
        Test a queue with sequencing can be created
        """

        #setup, declare a queue
        try:
            sender = self.setup_sender()
        except:
            self.fail("Unable to create ring queue with sequencing enabled")

    def test_get_sequence_number(self):
        """
        Test retrieving sequence number for queues 
        """

        key = "k"
        sender = self.setup_sender("ring-sequence-queue2", key=key)

        # send and receive 1 message and test the sequence number
        msg = Message()
        sender.send(msg)

        receiver = self.ssn.receiver("ring-sequence-queue2")
        msg = receiver.fetch(1)
        try:
            seqNo = msg.properties[key]
            if int(seqNo) != 1:
                txt = "Unexpected sequence number. Should be 1. Received (%s)" % seqNo
                self.fail(txt)
        except:
            txt = "Unable to get key (%s) from message properties" % key
            self.fail(txt)
        receiver.close()

    def test_sequence_number_gap(self):
        """
        Test that sequence number for ring queues shows gaps when queue
        messages are overwritten
        """
        key = "qpid.seq"
        sender = self.setup_sender("ring-sequence-queue3", key=key)
        receiver = self.ssn.receiver("ring-sequence-queue3")

        msg = Message()
        sender.send(msg)
        msg = receiver.fetch(1)

        # send 5 more messages to overflow the queue
        for i in range(5):
            sender.send(msg)

        msg = receiver.fetch(1)
        seqNo = msg.properties[key]
        if int(seqNo) != 3:
            txt = "Unexpected sequence number. Should be 3. Received (%s)" % seqNo
            self.fail(txt)
        receiver.close()