Added receipted unsubscribe tests and check for send after unsubscribe properly.

This commit is contained in:
Steve Powell 2011-01-18 12:46:28 +00:00
parent 6f8e904f9b
commit 55f2e7eb95
3 changed files with 37 additions and 115 deletions

View File

@ -203,7 +203,7 @@ handle_frame(Command, _Frame, State) ->
%% Internal helpers for processing frames callbacks %% Internal helpers for processing frames callbacks
%%---------------------------------------------------------------------------- %%----------------------------------------------------------------------------
cancel_subscription(missing, State) -> cancel_subscription(ConsumerTag, State) when ConsumerTag == missing ->
error("Missing destination or id", error("Missing destination or id",
"UNSUBSCRIBE must include a 'destination' or 'id' header\n", "UNSUBSCRIBE must include a 'destination' or 'id' header\n",
State); State);

View File

@ -48,7 +48,7 @@ class BaseTest(unittest.TestCase):
self.assertEquals("foo", msg['message']) self.assertEquals("foo", msg['message'])
self.assertEquals(dest, msg['headers']['destination']) self.assertEquals(dest, msg['headers']['destination'])
def assertListener(self, errMsg, numMsgs=0, numErrs=0, numRcts=0, timeout=3): def assertListener(self, errMsg, numMsgs=0, numErrs=0, numRcts=0, timeout=1):
if numMsgs + numErrs + numRcts > 0: if numMsgs + numErrs + numRcts > 0:
self.assertTrue(self.listener.await(timeout), errMsg + " (#awaiting)") self.assertTrue(self.listener.await(timeout), errMsg + " (#awaiting)")
else: else:
@ -57,7 +57,7 @@ class BaseTest(unittest.TestCase):
self.assertEquals(numErrs, len(self.listener.errors), errMsg + " (#errors)") self.assertEquals(numErrs, len(self.listener.errors), errMsg + " (#errors)")
self.assertEquals(numRcts, len(self.listener.receipts), errMsg + " (#receipts)") self.assertEquals(numRcts, len(self.listener.receipts), errMsg + " (#receipts)")
def assertListenerAfter(self, verb, errMsg="", numMsgs=0, numErrs=0, numRcts=0, timeout=3): def assertListenerAfter(self, verb, errMsg="", numMsgs=0, numErrs=0, numRcts=0, timeout=1):
num = numMsgs + numErrs + numRcts num = numMsgs + numErrs + numRcts
self.listener.reset(num if num>0 else 1) self.listener.reset(num if num>0 else 1)
verb() verb()

View File

@ -7,152 +7,74 @@ class TestLifecycle(base.BaseTest):
def test_unsubscribe_exchange_destination(self): def test_unsubscribe_exchange_destination(self):
''' Test UNSUBSCRIBE command with exchange''' ''' Test UNSUBSCRIBE command with exchange'''
self.unsub_test(self.sub_and_send("/exchange/amq.fanout")) d = "/exchange/amq.fanout"
self.unsub_test(d, self.sub_and_send(d))
def test_unsubscribe_exchange_destination_with_receipt(self): def test_unsubscribe_exchange_destination_with_receipt(self):
''' Test receipted UNSUBSCRIBE command with exchange''' ''' Test receipted UNSUBSCRIBE command with exchange'''
d = "/exchange/amq.fanout" d = "/exchange/amq.fanout"
self.unsub_test(d, self.sub_and_send(d, receipt="unsub.rct"), numRcts=1)
# subscribe and send message
self.listener.reset()
self.conn.subscribe(destination=d)
self.conn.send("test", destination=d)
self.assertTrue(self.listener.await())
self.assertEquals(1, len(self.listener.messages))
self.assertEquals(0, len(self.listener.errors))
# unsubscribe and send with RECEIPTs
self.listener.reset()
self.conn.unsubscribe(destination=d, receipt="unsub.receipt")
self.assertTrue(self.listener.await(1),
"No RECEIPT received on UNSUBSCRIBE")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on UNSUBSCRIBE")
self.listener.reset(2) # RECEIPT and possibly message
self.conn.send("test", destination=d, receipt="send.receipt")
self.assertFalse(self.listener.await(3),
"UNSUBSCRIBE exchange failed, still receiving messages")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on SEND")
def test_unsubscribe_queue_destination(self): def test_unsubscribe_queue_destination(self):
''' Test UNSUBSCRIBE command with queue''' ''' Test UNSUBSCRIBE command with queue'''
self.unsub_test(self.sub_and_send("/queue/unsub01")) d = "/queue/unsub01"
self.unsub_test(d, self.sub_and_send(d))
def test_unsubscribe_queue_destination_with_receipt(self): def test_unsubscribe_queue_destination_with_receipt(self):
''' Test receipted UNSUBSCRIBE command with queue''' ''' Test receipted UNSUBSCRIBE command with queue'''
d = "/queue/unsub03" d = "/queue/unsub02"
self.unsub_test(d, self.sub_and_send(d, receipt="unsub.rct"), numRcts=1)
# subscribe and send message
self.listener.reset()
self.conn.subscribe(destination=d)
self.conn.send("test", destination=d)
self.assertTrue(self.listener.await())
self.assertEquals(1, len(self.listener.messages))
self.assertEquals(0, len(self.listener.errors))
# unsubscribe and send with RECEIPTs
self.listener.reset()
self.conn.unsubscribe(destination=d, receipt="unsub.receipt")
self.assertTrue(self.listener.await(1),
"No RECEIPT received on UNSUBSCRIBE")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on UNSUBSCRIBE")
self.listener.reset(2) # RECEIPT and possibly message
self.conn.send("test", destination=d, receipt="send.receipt")
self.assertFalse(self.listener.await(3),
"UNSUBSCRIBE queue failed, still receiving messages")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on SEND")
def test_unsubscribe_exchange_id(self): def test_unsubscribe_exchange_id(self):
''' Test UNSUBSCRIBE command with exchange by id''' ''' Test UNSUBSCRIBE command with exchange by id'''
self.unsub_test(self.subid_and_send("/exchange/amq.fanout", "exchid")) d = "/exchange/amq.fanout"
self.unsub_test(d, self.sub_and_send(d, subid="exchid"))
def test_unsubscribe_exchange_id_with_receipt(self): def test_unsubscribe_exchange_id_with_receipt(self):
''' Test receipted UNSUBSCRIBE command with exchange by id''' ''' Test receipted UNSUBSCRIBE command with exchange by id'''
d = "/exchange/amq.fanout" d = "/exchange/amq.fanout"
self.unsub_test(d, self.sub_and_send(d, subid="exchid", receipt="unsub.rct"), numRcts=1)
# subscribe and send message
self.listener.reset()
self.conn.subscribe(destination=d, id="exchid")
self.conn.send("test", destination=d)
self.assertTrue(self.listener.await())
self.assertEquals(1, len(self.listener.messages))
self.assertEquals(0, len(self.listener.errors))
# unsubscribe and send with RECEIPTs
self.listener.reset()
self.conn.unsubscribe(id="exchid", receipt="unsub.receipt")
self.assertTrue(self.listener.await(1),
"No RECEIPT received on UNSUBSCRIBE")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on UNSUBSCRIBE")
self.listener.reset(2) # RECEIPT and possibly message
self.conn.send("test", destination=d, receipt="send.receipt")
self.assertFalse(self.listener.await(3),
"UNSUBSCRIBE exchange failed, still receiving messages")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on SEND")
def test_unsubscribe_queue_id(self): def test_unsubscribe_queue_id(self):
''' Test UNSUBSCRIBE command with queue by id''' ''' Test UNSUBSCRIBE command with queue by id'''
self.unsub_test(self.subid_and_send("/queue/unsub02", "queid")) d = "/queue/unsub03"
self.unsub_test(d, self.sub_and_send(d, subid="queid"))
def test_unsubscribe_queue_id_with_receipt(self): def test_unsubscribe_queue_id_with_receipt(self):
''' Test receipted UNSUBSCRIBE command with queue by id''' ''' Test receipted UNSUBSCRIBE command with queue by id'''
d = "/queue/unsub04" d = "/queue/unsub04"
self.unsub_test(d, self.sub_and_send(d, subid="queid", receipt="unsub.rct"), numRcts=1)
# subscribe and send message
self.listener.reset()
self.conn.subscribe(destination=d, id="queid")
self.conn.send("test", destination=d)
self.assertTrue(self.listener.await())
self.assertEquals(1, len(self.listener.messages))
self.assertEquals(0, len(self.listener.errors))
# unsubscribe and send with RECEIPTs
self.listener.reset()
self.conn.unsubscribe(id="queid", receipt="unsub.receipt")
self.assertTrue(self.listener.await(1),
"No RECEIPT received on UNSUBSCRIBE")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on UNSUBSCRIBE")
self.listener.reset(2) # RECEIPT and possibly message
self.conn.send("test", destination=d, receipt="send.receipt")
self.assertFalse(self.listener.await(3),
"UNSUBSCRIBE queue by id failed, still receiving messages")
self.assertEquals(1, len(self.listener.receipts),
"Expected a receipt on SEND")
def test_disconnect(self): def test_disconnect(self):
''' Run DISCONNECT command ''' ''' Run DISCONNECT command '''
self.conn.disconnect() self.conn.disconnect()
self.assertFalse(self.conn.is_connected()) self.assertFalse(self.conn.is_connected())
def unsub_test(self, verbs): def unsub_test(self, dest, verbs, numRcts=0):
def afterfun():
self.conn.send("after-test", destination=dest)
subverb, unsubverb = verbs subverb, unsubverb = verbs
self.assertListenerAfter(subverb, self.assertListenerAfter(subverb, numMsgs=1,
numMsgs=1, errMsg="FAILED to subscribe and send") errMsg="FAILED to subscribe and send")
self.assertListenerAfter(unsubverb, self.assertListenerAfter(unsubverb, numRcts=numRcts,
errMsg="Incorrect responses from UNSUBSCRIBE")
self.assertListenerAfter(afterfun,
errMsg="Still receiving messages") errMsg="Still receiving messages")
def subid_and_send(self, dest, subid): def sub_and_send(self, dest, subid="", receipt=""):
def subfun(): def subfun():
self.conn.subscribe(destination=dest, id=subid) if subid=="":
self.conn.subscribe(destination=dest)
else:
self.conn.subscribe(destination=dest, id=subid)
self.conn.send("test", destination=dest) self.conn.send("test", destination=dest)
def unsubfun(): def unsubfun():
self.conn.unsubscribe(id=subid) if subid=="" and receipt=="":
return subfun, unsubfun self.conn.unsubscribe(destination=dest)
elif receipt=="":
def sub_and_send(self, dest): self.conn.unsubscribe(id=subid)
def subfun(): elif subid=="":
self.conn.subscribe(destination=dest) self.conn.unsubscribe(destination=dest, receipt=receipt)
self.conn.send("test", destination=dest) else:
def unsubfun(): self.conn.unsubscribe(id=subid, receipt=receipt)
self.conn.unsubscribe(destination=dest)
return subfun, unsubfun return subfun, unsubfun