class TestHub(unittest.TestCase): def setUp(self): config = load_config() self.hub = CentralMokshaHub(config=config) self.context = FedMsgContext(**config) # fully qualified self.fq_topic = "org.fedoraproject.dev.unittest.foo" # short version self.topic = "foo" def tearDown(self): self.context.destroy() self.hub.close() 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 fake_register_consumer(self, cons): """ Fake register a consumer, not by entry-point like usual. Normally, consumers are identified by the hub by way of entry-points Ideally, this test would register the TestConsumer on the moksha.consumers entry point, and the hub would pick it up. I'm not sure how to do that, so we're going to fake it and manually add this consumer to the list of consumers of which the Hub is aware. """ self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, []) self.hub.topics[cons.topic].append(cons(self.hub).consume) sleep(sleep_duration) 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. self.context.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_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. """ 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)
class TestHub(TestCase): def setUp(self): config = load_config() self.hub = CentralMokshaHub(config=config) self.context = FedMsgContext(**config) # fully qualified self.fq_topic = "org.fedoraproject.dev.unittest.foo" # short version self.topic = "foo" def tearDown(self): self.context.destroy() self.hub.close() def test_run_hub_get_heartbeat(self): """ Start the heartbeat producer and ensure it emits a message. """ messages_received = [] def callback(json): messages_received.append(fedmsg.json.loads(json.body)) self.hub.subscribe( topic=HeartbeatProducer.topic, callback=callback, ) simulate_reactor(HeartbeatProducer.frequency.seconds*1.1) sleep(HeartbeatProducer.frequency.seconds*1.1) eq_(len(messages_received), 1) 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.json.loads(json.body)) self.hub.subscribe(topic=self.fq_topic, callback=callback) sleep(sleep_duration) self.context.send_message(topic=self.topic, msg=secret) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 1) eq_(messages_received[0]['msg'], secret) def fake_register_consumer(self, cons): """ Fake register a consumer, not by entry-point like usual. Normally, consumers are identified by the hub by way of entry-points Ideally, this test would register the TestConsumer on the moksha.consumers entry point, and the hub would pick it up. I'm not sure how to do that, so we're going to fake it and manually add this consumer to the list of consumers of which the Hub is aware. """ self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, []) self.hub.topics[cons.topic].append(cons(self.hub).consume) sleep(sleep_duration) def test_consumer(self): """ Check that a consumer can get messages. """ obj = {'secret': secret} messages_received = [] class TestConsumer(moksha.api.hub.consumer.Consumer): topic = self.fq_topic 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.send_message(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 1) eq_(messages_received[0], obj) def test_double_consumers(self): """ Check that two consumers can get messages. """ obj = {'secret': secret} messages_received = [] class TestConsumer1(moksha.api.hub.consumer.Consumer): topic = self.fq_topic def consume(self, message): messages_received.append( message['body']['msg'] ) class TestConsumer2(moksha.api.hub.consumer.Consumer): topic = self.fq_topic 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. fedmsg.send_message(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)
class TestHub(unittest.TestCase): def setUp(self): config = load_config() self.hub = CentralMokshaHub(config=config) self.context = FedMsgContext(**config) # fully qualified self.fq_topic = "org.fedoraproject.dev.unittest.foo" # short version self.topic = "foo" def tearDown(self): self.context.destroy() self.hub.close() 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 fake_register_consumer(self, cons): """ Fake register a consumer, not by entry-point like usual. Normally, consumers are identified by the hub by way of entry-points Ideally, this test would register the TestConsumer on the moksha.consumers entry point, and the hub would pick it up. I'm not sure how to do that, so we're going to fake it and manually add this consumer to the list of consumers of which the Hub is aware. """ self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, []) self.hub.topics[cons.topic].append(cons(self.hub).consume) sleep(sleep_duration) 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_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. fedmsg.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. """ 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)
class TestHub(TestCase): def setUp(self): config = load_config() self.hub = CentralMokshaHub(config=config) self.context = FedMsgContext(**config) # fully qualified self.fq_topic = "org.fedoraproject.dev.unittest.foo" # short version self.topic = "foo" def tearDown(self): self.context.destroy() self.hub.close() def test_run_hub_get_heartbeat(self): """ Start the heartbeat producer and ensure it emits a message. """ messages_received = [] def callback(json): messages_received.append(fedmsg.json.loads(json.body)) self.hub.subscribe( topic=HeartbeatProducer.topic, callback=callback, ) simulate_reactor(HeartbeatProducer.frequency.seconds * 1.1) sleep(HeartbeatProducer.frequency.seconds * 1.1) eq_(len(messages_received), 1) 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.json.loads(json.body)) self.hub.subscribe(topic=self.fq_topic, callback=callback) sleep(sleep_duration) self.context.send_message(topic=self.topic, msg=secret) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 1) eq_(messages_received[0]['msg'], secret) def fake_register_consumer(self, cons): """ Fake register a consumer, not by entry-point like usual. Normally, consumers are identified by the hub by way of entry-points Ideally, this test would register the TestConsumer on the moksha.consumers entry point, and the hub would pick it up. I'm not sure how to do that, so we're going to fake it and manually add this consumer to the list of consumers of which the Hub is aware. """ self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, []) self.hub.topics[cons.topic].append(cons(self.hub).consume) sleep(sleep_duration) def test_consumer(self): """ Check that a consumer can get messages. """ obj = {'secret': secret} messages_received = [] class TestConsumer(moksha.api.hub.consumer.Consumer): topic = self.fq_topic 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.send_message(topic=self.topic, msg=obj) simulate_reactor(sleep_duration) sleep(sleep_duration) eq_(len(messages_received), 1) eq_(messages_received[0], obj) def test_double_consumers(self): """ Check that two consumers can get messages. """ obj = {'secret': secret} messages_received = [] class TestConsumer1(moksha.api.hub.consumer.Consumer): topic = self.fq_topic def consume(self, message): messages_received.append(message['body']['msg']) class TestConsumer2(moksha.api.hub.consumer.Consumer): topic = self.fq_topic 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. fedmsg.send_message(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)