Back to index

rabbitmq-server  2.8.4
lifecycle.py
Go to the documentation of this file.
00001 import unittest
00002 import stomp
00003 import base
00004 import time
00005 
00006 class TestLifecycle(base.BaseTest):
00007 
00008     def test_unsubscribe_exchange_destination(self):
00009         ''' Test UNSUBSCRIBE command with exchange'''
00010         d = "/exchange/amq.fanout"
00011         self.unsub_test(d, self.sub_and_send(d))
00012 
00013     def test_unsubscribe_exchange_destination_with_receipt(self):
00014         ''' Test receipted UNSUBSCRIBE command with exchange'''
00015         d = "/exchange/amq.fanout"
00016         self.unsub_test(d, self.sub_and_send(d, receipt="unsub.rct"), numRcts=1)
00017 
00018     def test_unsubscribe_queue_destination(self):
00019         ''' Test UNSUBSCRIBE command with queue'''
00020         d = "/queue/unsub01"
00021         self.unsub_test(d, self.sub_and_send(d))
00022 
00023     def test_unsubscribe_queue_destination_with_receipt(self):
00024         ''' Test receipted UNSUBSCRIBE command with queue'''
00025         d = "/queue/unsub02"
00026         self.unsub_test(d, self.sub_and_send(d, receipt="unsub.rct"), numRcts=1)
00027 
00028     def test_unsubscribe_exchange_id(self):
00029         ''' Test UNSUBSCRIBE command with exchange by id'''
00030         d = "/exchange/amq.fanout"
00031         self.unsub_test(d, self.sub_and_send(d, subid="exchid"))
00032 
00033     def test_unsubscribe_exchange_id_with_receipt(self):
00034         ''' Test receipted UNSUBSCRIBE command with exchange by id'''
00035         d = "/exchange/amq.fanout"
00036         self.unsub_test(d, self.sub_and_send(d, subid="exchid", receipt="unsub.rct"), numRcts=1)
00037 
00038     def test_unsubscribe_queue_id(self):
00039         ''' Test UNSUBSCRIBE command with queue by id'''
00040         d = "/queue/unsub03"
00041         self.unsub_test(d, self.sub_and_send(d, subid="queid"))
00042 
00043     def test_unsubscribe_queue_id_with_receipt(self):
00044         ''' Test receipted UNSUBSCRIBE command with queue by id'''
00045         d = "/queue/unsub04"
00046         self.unsub_test(d, self.sub_and_send(d, subid="queid", receipt="unsub.rct"), numRcts=1)
00047 
00048     def test_connect_version_1_1(self):
00049         ''' Test CONNECT with version 1.1'''
00050         self.conn.disconnect()
00051         new_conn = self.create_connection(version="1.1")
00052         try:
00053             self.assertTrue(new_conn.is_connected())
00054         finally:
00055             new_conn.disconnect()
00056             self.assertFalse(new_conn.is_connected())
00057 
00058     def test_heartbeat_disconnects_client(self):
00059         ''' Test heart-beat disconnection'''
00060         self.conn.disconnect()
00061         new_conn = self.create_connection(heartbeat="1500,0")
00062         try:
00063             self.assertTrue(new_conn.is_connected())
00064             time.sleep(1)
00065             self.assertTrue(new_conn.is_connected())
00066             time.sleep(3)
00067             self.assertFalse(new_conn.is_connected())
00068         finally:
00069             if new_conn.is_connected():
00070                 new_conn.disconnect()
00071 
00072     def test_unsupported_version(self):
00073         ''' Test unsupported version on CONNECT command'''
00074         self.bad_connect(stomp.Connection(user="guest",
00075                                           passcode="guest",
00076                                           version="100.1"),
00077                          "Supported versions are 1.0,1.1\n")
00078 
00079     def test_bad_username(self):
00080         ''' Test bad username'''
00081         self.bad_connect(stomp.Connection(user="gust",
00082                                           passcode="guest"),
00083                          "Authentication failure\n")
00084 
00085     def test_bad_password(self):
00086         ''' Test bad password'''
00087         self.bad_connect(stomp.Connection(user="guest",
00088                                           passcode="gust"),
00089                          "Authentication failure\n")
00090 
00091     def test_bad_vhost(self):
00092         ''' Test bad virtual host'''
00093         self.bad_connect(stomp.Connection(user="guest",
00094                                           passcode="guest",
00095                                           virtual_host="//"),
00096                          "Authentication failure\n")
00097 
00098     def bad_connect(self, new_conn, expected):
00099         self.conn.disconnect()
00100         listener = base.WaitableListener()
00101         new_conn.set_listener('', listener)
00102         try:
00103             new_conn.start()
00104             new_conn.connect()
00105             self.assertTrue(listener.await())
00106             self.assertEquals(expected, listener.errors[0]['message'])
00107         finally:
00108             if new_conn.is_connected():
00109                 new_conn.disconnect()
00110 
00111     def test_bad_header_on_send(self):
00112         ''' Test disallowed header on SEND '''
00113         self.listener.reset(1)
00114         self.conn.send_frame("SEND", {"destination":"a", "message-id":"1"})
00115         self.assertTrue(self.listener.await())
00116         self.assertEquals(1, len(self.listener.errors))
00117         errorReceived = self.listener.errors[0]
00118         self.assertEquals("Invalid header", errorReceived['headers']['message'])
00119         self.assertEquals("'message-id' is not allowed on 'SEND'.\n", errorReceived['message'])
00120 
00121     def test_disconnect(self):
00122         ''' Test DISCONNECT command'''
00123         self.conn.disconnect()
00124         self.assertFalse(self.conn.is_connected())
00125 
00126     def test_disconnect_with_receipt(self):
00127         ''' Test the DISCONNECT command with receipts '''
00128         time.sleep(3)
00129         self.listener.reset(1)
00130         self.conn.send_frame("DISCONNECT", {"receipt": "test"})
00131         self.assertTrue(self.listener.await())
00132         self.assertEquals(1, len(self.listener.receipts))
00133         receiptReceived = self.listener.receipts[0]['headers']['receipt-id']
00134         self.assertEquals("test", receiptReceived
00135                          , "Wrong receipt received: '" + receiptReceived + "'")
00136 
00137     def unsub_test(self, dest, verbs, numRcts=0):
00138         def afterfun():
00139             self.conn.send("after-test", destination=dest)
00140         subverb, unsubverb = verbs
00141         self.assertListenerAfter(subverb, numMsgs=1,
00142                            errMsg="FAILED to subscribe and send")
00143         self.assertListenerAfter(unsubverb, numRcts=numRcts,
00144                            errMsg="Incorrect responses from UNSUBSCRIBE")
00145         self.assertListenerAfter(afterfun,
00146                            errMsg="Still receiving messages")
00147 
00148     def sub_and_send(self, dest, subid="", receipt=""):
00149         def subfun():
00150             if subid=="":
00151                 self.conn.subscribe(destination=dest)
00152             else:
00153                 self.conn.subscribe(destination=dest, id=subid)
00154             self.conn.send("test", destination=dest)
00155         def unsubfun():
00156             if subid=="" and receipt=="":
00157                 self.conn.unsubscribe(destination=dest)
00158             elif receipt=="":
00159                 self.conn.unsubscribe(id=subid)
00160             elif subid=="":
00161                 self.conn.unsubscribe(destination=dest, receipt=receipt)
00162             else:
00163                 self.conn.unsubscribe(id=subid, receipt=receipt)
00164         return subfun, unsubfun