summaryrefslogtreecommitdiff
path: root/qpid/cpp/src/tests/cluster2_tests.py
blob: c5f6157f34f676bb0a1b931ceaf5be2878ad1978 (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
#!/usr/bin/env python

# 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.
#

import os, signal, sys, time, imp, re, subprocess
from qpid import datatypes, messaging
from brokertest import *
from qpid.harness import Skipped
from qpid.messaging import Message
from qpid.messaging.exceptions import *
from threading import Thread, Lock
from logging import getLogger
from itertools import chain

log = getLogger("qpid.cluster_tests")

class Cluster2Tests(BrokerTest):
    """Tests for new cluster code."""

    def queue_exists(self, queue, connection):
        s = connection.session()
        try:
            s.sender(queue)
            return True
        except qpid.messaging.exceptions.NotFound:
            return False

    # FIXME aconway 2011-06-22: needed to compensate for
    # async wiring in early cluster2 prototype
    def wait_for_queue(self, queue, connections, timeout=10):
        deadline = time.time() + timeout
        for c in connections:
            while not self.queue_exists(queue,c):
                if time.time() >  timeout: fail("Time out in wait_for_queue(%s))"%queue)
                time.sleep(0.01)

    def test_message_enqueue(self):
        """Test basic replication of enqueued messages.
        Verify that fanout messages are replicated correctly.
        """

        cluster = self.cluster(2, cluster2=True)

        sn0 = cluster[0].connect().session()
        s0 = sn0.sender("amq.fanout");
        sn1 = cluster[1].connect().session()

        # Bind queues to amq.fanout
        sn0.receiver("p; {mode:browse, create:always, node:{x-bindings:[{exchange:'amq.fanout', queue:p}]}}");
        sn0.receiver("q; {mode:browse, create:always, node:{x-bindings:[{exchange:'amq.fanout', queue:q}]}}");


        # Send messages on member 0
        content = ["a","b","c"]
        for m in content: s0.send(Message(m))

        # Browse on both members.
        self.assert_browse(sn0, "p", content)
        self.assert_browse(sn0, "q", content)
        self.assert_browse(sn1, "p", content)
        self.assert_browse(sn1, "q", content)

        sn1.connection.close()
        sn0.connection.close()

    def test_message_dequeue(self):
        """Test replication of dequeues"""
        cluster = self.cluster(2, cluster2=True)
        sn0 = cluster[0].connect().session()
        s0 = sn0.sender("q;{create:always,delete:always}")
        r0 = sn0.receiver("q")
        sn1 = cluster[1].connect().session()
        r1 = sn1.receiver("q;{create:always}")

        content = ["a","b","c"]
        for m in content: s0.send(Message(m))
        # Verify enqueued on members 0 and 1
        self.assert_browse(sn0, "q", content)
        self.assert_browse(sn1, "q", content)

        # Dequeue on cluster[0]
        self.assertEqual(r0.fetch(1).content, "a")
        sn0.acknowledge(sync=True)

        # Verify dequeued on cluster[0] and cluster[1]
        self.assert_browse(sn0, "q", ["b", "c"])
        self.assert_browse(sn1, "q", ["b", "c"])

    def test_wiring(self):
        """Test replication of wiring"""
        cluster = self.cluster(2, cluster2=True)
        sn0 = cluster[0].connect().session()
        sn1 = cluster[1].connect().session()

        # Test creation of queue, exchange, binding
        r0ex = sn0.receiver("ex; {create:always, delete:always, node:{type:topic, x-declare:{name:ex, type:'direct'}}}")
        r0q  = sn0.receiver("q;  {create:always, delete:always, link:{x-bindings:[{exchange:ex,queue:q,key:k}]}}")

        # Verify objects were created on member 1
        r1 = sn1.receiver("q")   # Queue
        s1ex = sn1.sender("ex/k; {node:{type:topic}}"); # Exchange
        s1ex.send(Message("x"))  # Binding with key k
        self.assertEqual(r1.fetch(1).content, "x")

        # Test destroy.
        r0q.close()                                    # Delete queue q
        self.assertRaises(NotFound, sn1.receiver, "q")
        r0ex.close()            # Delete exchange ex
        # FIXME aconway 2010-11-05: this does not raise NotFound, sn1 is caching "ex"
        # self.assertRaises(NotFound, sn1.sender, "ex")
        # Have to create a new session.
        self.assertRaises(NotFound, cluster[1].connect().session().receiver, "ex")

        # FIXME aconway 2010-10-29: test unbind, may need to use old API.

    def duration(self):
        d = self.config.defines.get("DURATION")
        if d: return float(d)*60
        else: return 3                  # Default is to be quick


    def test_dequeue_mutex(self):
        """Ensure that one and only one consumer receives each dequeued message."""
        class Receiver(Thread):
            def __init__(self, session):
                self.session = session
                self.receiver = session.receiver("q")
                self.messages = []
                self.error = None
                Thread.__init__(self)

            def run(self):
                try:
                    while True:
                        self.messages.append(self.receiver.fetch(1))
                        self.session.acknowledge()
                except Empty: pass
                except Exception,e: self.error = e

        cluster = self.cluster(3, cluster2=True)
        connections = [ b.connect() for  b in cluster]
        sessions = [ c.session() for c in connections ]
        sender = sessions[0].sender("q;{create:always}")
        self.wait_for_queue("q", connections)

        receivers = [ Receiver(s) for s in sessions ]
        for r in receivers: r.start()

        n = 0
        t = time.time() + self.duration()
        while time.time() < t:
            sender.send(str(n))
            n += 1
        for r in receivers:
            r.join();
            if (r.error): self.fail("Receiver failed: %s" % r.error)
        for r in receivers:
            len(r.messages) > n/6 # Fairness test.
        messages = [int(m.content) for r in receivers for m in r.messages ]
        messages.sort()
        self.assertEqual(range(n), messages)