def test_send_recv(self): """ Send a message and receive it. Admittedly, this is not a unit test, but an integration test. It tests: - Sending a message. - Receiving a message. - Encoding *and* decoding. """ messages_received = [] def callback(json): messages_received.append(fedmsg.encoding.loads(json.body)) self.hub.subscribe(topic=self.fq_topic, callback=callback) sleep(sleep_duration) self.context.publish(topic=self.topic, msg=secret) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 1) eq_(messages_received[0]['msg'], secret)
def test_multi_threaded(self): """ Send messages from 5 concurrent threads. """ messages_received = [] def callback(json): messages_received.append(fedmsg.encoding.loads(json.body)) self.hub.subscribe(topic=self.fq_topic, callback=callback) sleep(sleep_duration) test_name = "__main__.%s" % socket.gethostname().split('.', 1)[0] self.config['name'] = test_name class Publisher(threading.Thread): def run(shmelf): config = copy.deepcopy(self.config) import fedmsg fedmsg.init(**config) fedmsg.publish(topic=self.topic, msg=secret, modname="threadtest") threads = [Publisher() for i in range(5)] for thread in threads: thread.start() for thread in threads: thread.join() simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 5) eq_(messages_received[0]['msg'], secret)
def test_double_consumers(self): """ Check that two consumers can get messages. """ obj = {'secret': secret} messages_received = [] class TestConsumer1(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def consume(self, message): messages_received.append(message['body']['msg']) class TestConsumer2(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def consume(self, message): messages_received.append(message['body']['msg']) self.fake_register_consumer(TestConsumer1) self.fake_register_consumer(TestConsumer2) # Now, send a generic message to that topic, and see if the consumer # processed it. self.context.publish(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 2) eq_(messages_received[0], obj) eq_(messages_received[1], obj)
def test_consumer(self): """ Check that a consumer can get messages. """ obj = {'secret': secret} messages_received = [] class TestConsumer(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def consume(self, message): messages_received.append( message['body']['msg'] ) self.fake_register_consumer(TestConsumer) # Now, send a generic message to that topic, and see if the consumer # processed it. fedmsg.publish(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 1) eq_(messages_received[0], obj)
def test_multi_threaded(self): """ Send messages from 5 concurrent threads. """ messages_received = [] def callback(json): messages_received.append(fedmsg.encoding.loads(json.body)) self.hub.subscribe(topic=self.fq_topic, callback=callback) sleep(sleep_duration) test_name = "__main__.%s" % socket.gethostname().split('.', 1)[0] self.config['name'] = test_name class Publisher(threading.Thread): def run(shmelf): config = copy.deepcopy(self.config) import fedmsg fedmsg.init(**config) fedmsg.publish(topic=self.topic, msg=secret, modname="unittest") threads = [Publisher() for i in range(5)] for thread in threads: thread.start() for thread in threads: thread.join() simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 5) eq_(messages_received[0]['msg'], secret)
def test_consumer_failed_validation(self): """ Check that a consumer won't consume invalid message. """ # TODO -- now that moksha.hub is doing its internal threading/queueing # behavior, this feature of fedmsg is a bit more difficult to test. raise self.skipTest("Not sure how to test this behavior now.") obj = {"secret": secret} messages_received = [] class TestConsumer(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def _consume(self, message): messages_received.append(message["body"]["msg"]) def validate(self, message): raise RuntimeWarning("Marking message as invalid.") self.fake_register_consumer(TestConsumer) self.context.publish(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) # Verify that we received no message. eq_(len(messages_received), 0)
def test_double_consumers(self): """ Check that two consumers can get messages. """ obj = {"secret": secret} messages_received = [] class TestConsumer1(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def _consume(self, message): messages_received.append(message["body"]["msg"]) class TestConsumer2(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def _consume(self, message): messages_received.append(message["body"]["msg"]) self.fake_register_consumer(TestConsumer1) self.fake_register_consumer(TestConsumer2) # Now, send a generic message to that topic, and see if the consumer # processed it. self.context.publish(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 2) eq_(messages_received[0], obj) eq_(messages_received[1], obj)
def test_consumer_failed_validation(self): """ Check that a consumer won't consume invalid message. """ # TODO -- now that moksha.hub is doing its internal threading/queueing # behavior, this feature of fedmsg is a bit more difficult to test. raise self.skipTest("Not sure how to test this behavior now.") obj = {'secret': secret} messages_received = [] class TestConsumer(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def _consume(self, message): messages_received.append(message['body']['msg']) def validate(self, message): raise RuntimeWarning("Marking message as invalid.") self.fake_register_consumer(TestConsumer) self.context.publish(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) # Verify that we received no message. eq_(len(messages_received), 0)
def test_consumer_failed_validation(self): """ Check that a consumer won't consume invalid message. """ obj = {'secret': secret} messages_received = [] class TestConsumer(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def consume(self, message): messages_received.append(message['body']['msg']) def validate(self, message): raise RuntimeWarning("Marking message as invalid.") self.fake_register_consumer(TestConsumer) self.context.publish(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) # Verify that we received no message. eq_(len(messages_received), 0)
def test_consumer_failed_validation(self): """ Check that a consumer won't consume invalid message. """ obj = {'secret': secret} messages_received = [] class TestConsumer(fedmsg.consumers.FedmsgConsumer): topic = self.fq_topic config_key = "test_consumer_enabled" def consume(self, message): messages_received.append( message['body']['msg'] ) def validate(self, message): raise RuntimeWarning("Marking message as invalid.") self.fake_register_consumer(TestConsumer) fedmsg.publish(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) # Verify that we received no message. eq_(len(messages_received), 0)