def test_message_cache(self): msg = Message(a=5) self.assertEqual(msg.cache, {}) msg.cache["thing"] = "dont_store_me" self.assertEqual(msg.cache, { "thing": "dont_store_me", }) self.assertEqual(msg[Message._CACHE_ATTRIBUTE], { "thing": "dont_store_me", })
def test_validate(self): f = VumiMessage(Message) msg = Message() f.validate(msg) self.assertRaises(ValidationError, f.validate, u'this is not a vumi message') self.assertRaises(ValidationError, f.validate, None)
def mkmsg_dialogueworker_control(self, action, dialogue_obj_id=None, phone_number=None): return Message(action=action, dialogue_obj_id=dialogue_obj_id, phone_number=phone_number)
def test_consume(self): """The consume helper should direct all incoming messages matching the specified routing_key, queue_name & exchange to the given callback""" message = fake_amq_message({"key": "value"}) worker = get_stubbed_worker(Worker) # buffer to check messages consumed log = [Message.from_json(message.content.body)] # consume all messages on the given routing key and append # them to the log worker.consume('test.routing.key', lambda msg: log.append(msg)) # if all works well then the consume method should funnel the test # message straight to the callback, the callback will apend it to the # log and we can test it. worker._amqp_client.broker.basic_publish('vumi', 'test.routing.key', message.content) self.assertEquals(log, [Message(key="value")])
def test_callback_on_disconnect(self): req = yield self.mock_server.queue.get() req.write('%s\n' % (Message(foo='bar').to_json().encode('utf-8'), )) req.finish() message = yield self.messages_received.get() self.assertEqual(message['foo'], 'bar') reason = yield self.disconnects_received.get() # this is the error message we get when a ResponseDone is raised # which happens when the remote server closes the connection. self.assertEqual(reason, 'Response body fully received')
def test_start_publisher(self): """The publisher should publish""" worker = get_stubbed_worker(Worker) publisher = yield worker.publish_to('test.routing.key') self.assertEquals(publisher.routing_key, 'test.routing.key') publisher.publish_message(Message(key="value")) [published_msg] = publisher.channel.broker.get_dispatched( 'vumi', 'test.routing.key') self.assertEquals(published_msg.body, '{"key": "value"}') self.assertEquals(published_msg.properties, {'delivery mode': 2})
def mkmsg_multiworker_control(self, message_type='add_worker', worker_name='m4h', worker_class=None, config=None): if config == None: config = [] return Message(message_type=message_type, worker_name=worker_name, worker_class=worker_class, config=config)
def test_consume_message(self): expected_datapoints = [ ("vumi.test.v1", 1234, 1.0), ("vumi.test.v2", 3456, 2.0), ] datapoints = [] consumer = metrics.MetricsConsumer(lambda *v: datapoints.append(v)) msg = metrics.MetricMessage() msg.extend(expected_datapoints) vumi_msg = Message.from_json(msg.to_json()) consumer.consume_message(vumi_msg) self.assertEqual(datapoints, expected_datapoints)
def test_consume(self): """The consume helper should direct all incoming messages matching the specified routing_key, queue_name & exchange to the given callback""" message = fake_amq_message({"key": "value"}) worker = yield self.worker_helper.get_worker(Worker, {}, start=False) # buffer to check messages consumed log = [] # consume all messages on the given routing key and append # them to the log yield worker.consume('test.routing.key', lambda msg: log.append(msg)) # if all works well then the consume method should funnel the test # message straight to the callback, the callback will apend it to the # log and we can test it. self.worker_helper.broker.basic_publish('vumi', 'test.routing.key', message.content) yield self.worker_helper.broker.wait_delivery() self.assertEquals(log, [Message(key="value")])
def _check_msg(self, broker, metric, values): msgs = broker.get_dispatched("vumi.metrics", "vumi.metrics") if values is None: self.assertEqual(msgs, []) return content = msgs[-1] name = metric.name self.assertEqual(content.properties, {"delivery mode": 2}) msg = Message.from_json(content.body) [datapoint] = msg.payload["datapoints"] self.assertEqual(datapoint[0], name) self.assertEqual(datapoint[1], list(metric.aggs)) # check datapoints within 2s of now -- the truncating of # time.time() to an int for timestamps can cause a 1s # difference by itself now = time.time() self.assertTrue( all(abs(p[0] - now) < 2.0 for p in datapoint[2]), "Not all datapoints near now (%f): %r" % (now, datapoint)) self.assertEqual([p[1] for p in datapoint[2]], values)
def _check_msg(self, broker, metric, values): msgs = broker.get_dispatched("vumi.metrics", "vumi.metrics") if values is None: self.assertEqual(msgs, []) return content = msgs[-1] name = metric.name self.assertEqual(content.properties, {"delivery mode": 2}) msg = Message.from_json(content.body) [datapoint] = msg.payload["datapoints"] self.assertEqual(datapoint[0], name) self.assertEqual(datapoint[1], list(metric.aggs)) # check datapoints within 2s of now -- the truncating of # time.time() to an int for timestamps can cause a 1s # difference by itself now = time.time() self.assertTrue(all(abs(p[0] - now) < 2.0 for p in datapoint[2]), "Not all datapoints near now (%f): %r" % (now, datapoint)) self.assertEqual([p[1] for p in datapoint[2]], values)
def get_messages(self, exchange, rkey): contents = self.get_dispatched(exchange, rkey) messages = [VumiMessage.from_json(content.body) for content in contents] return messages
def r_get_message(self, message_id): json_string = self.r_get_message_json(message_id) if json_string: return Message.from_json(json_string) else: return None
def get_messages(self, exchange, rkey): contents = self.get_dispatched(exchange, rkey) messages = [ VumiMessage.from_json(content.body) for content in contents ] return messages
def test_message_contains(self): self.assertTrue('a' in Message(a=5)) self.assertFalse('a' in Message(b=5))
def test_message_equality(self): self.assertEqual(Message(a=5), Message(a=5)) self.assertNotEqual(Message(a=5), Message(b=5)) self.assertNotEqual(Message(a=5), Message(a=6)) self.assertNotEqual(Message(a=5), {'a': 5})
def recv_datapoints(self, exchange, queue): """Retrieve datapoints from a broker.""" contents = self._broker.get_dispatched(exchange, queue) vumi_msgs = [Message.from_json(content.body) for content in contents] msgs = [MetricMessage.from_dict(vm.payload) for vm in vumi_msgs] return [msg.datapoints() for msg in msgs]
def r_get_message(self, message_id): json_string = yield self.r_get_message_json(message_id) if json_string: returnValue(Message.from_json(json_string)) else: returnValue(None)