def testSendingReceivingMessages(self): debug = getUtility(IReceptionQueue, name="debug") sender = Sender(TEST_URL, [debug, ]) sender.send(Message('debug', data="BLA")) assert len(MESSAGES) == 0 poller(TEST_URL) assert len(MESSAGES) == 1 assert MESSAGES[0].delivery_info == {'priority': 0, 'routing_key': 'debug', 'exchange': 'messages'}
class MQDataManagerTests(unittest.TestCase): def read(self, body, message): self.received.append(body) message.ack() def setUp(self): gr.globalSiteManager = gr.BaseGlobalComponents('test1') self.received = [] self.dm = MQDataManager(url=TEST_URL, queues=QUEUES) self.message = Message('info', data="BLA") self.receive = receiver(TEST_URL, self.read) def tearDown(self): gr.globalSiteManager = gr.base def testEmptyDM(self): self.assertEqual(len(self.dm.messages), 0) def testCreateMessage(self): self.dm.createMessage(self.message) self.assertEqual(len(self.dm.messages), 1) self.assertEqual(list(self.dm.messages.keys()), [self.message.__hash__()]) def testFailIfCreateMessageTwice(self): self.dm.createMessage(self.message) self.assertRaises(ValueError, self.dm.createMessage, self.message) def testAbortMessages(self): self.dm.createMessage(self.message) self.assertEqual(len(self.dm.messages), 1) self.dm.abort(None) self.assertEqual(len(self.dm.messages), 0) def testSendingMessage(self): import transaction xmlconfig(StringIO(ZCML)) tr = transaction.begin() tr.join(self.dm) self.dm.createMessage(self.message) transaction.commit() self.receive() assert self.received == [{u'data': u'BLA'}] self.received = [] def test_abortion(self): import transaction tr = transaction.begin() tr.join(self.dm) self.dm.createMessage(self.message) transaction.abort() with pytest.raises(socket.timeout): self.receive() assert self.received == []
def sender(url): """Used for test purposes. """ gsm = getGlobalSiteManager() queues = dict(getUtilitiesFor(IEmissionQueue)) sender = Sender(url, queues.values) gsm.registerUtility(sender, ISender, name="info") test_sender = getUtility(ISender, name="info") message = Message('info', **{'message': 'ping'}) test_sender.send(message)
def setUp(self): gr.globalSiteManager = gr.BaseGlobalComponents('test1') self.received = [] self.dm = MQDataManager(url=TEST_URL, queues=QUEUES) self.message = Message('info', data="BLA") self.receive = receiver(TEST_URL, self.read)