Esempio n. 1
0
    def test_serializer(self):
        class SomeObject(object):
            def __init__(self, entity):
                self.entity = entity

            def __repr__(self):
                return self.entity

        queue = AsyncManager()
        self.addCleanup(queue.close_connection)
        queue.connect(host=HOST)

        counter = {"n": 0}
        def on_message(body):
            self.assertEqual(body, {"message": "Hello, world!"})
            counter["n"] += 1

        queue.start_consuming(
            on_message,
        )

        self.assertRaises(TypeError, lambda: queue.publish({"message": SomeObject("Hello, world!")}, routing_key=queue.name))

        def serializer(o):
            if isinstance(o, SomeObject):
                return repr(o)
            raise TypeError(repr(o) + " is not JSON serializable")

        queue.publish({"message": SomeObject("Hello, world!")}, routing_key=queue.name, serializer=serializer)
        IOLoop.current().add_timeout(time() + 0.2, self.stop)
        self.wait()

        self.assertEqual(counter["n"], 1)
Esempio n. 2
0
 def test_publish_with_not_ready(self):
     queue = AsyncManager(queue="dummy")
     self.addCleanup(queue.close_connection)
     queue.connect(host=HOST)
     queue._ready = False
     try:
         queue.publish(None,None)
     except Exception as e:
         self.fail("This exception is raised: {}".format(e))
Esempio n. 3
0
 def test_publish_with_not_ready(self):
     queue = AsyncManager(queue="dummy")
     self.addCleanup(queue.close_connection)
     queue.connect(host=HOST)
     queue._ready = False
     try:
         queue.publish(None, None)
     except Exception as e:
         self.fail("This exception is raised: {}".format(e))
Esempio n. 4
0
    def test_basis(self):
        queue = AsyncManager()
        self.addCleanup(queue.close_connection)
        queue.connect(host=HOST)

        counter = {"n": 0}

        def on_message(body):
            self.assertEqual(body, {"message": "Hello, world!"})
            counter["n"] += 1

        queue.start_consuming(on_message)

        queue.publish({"message": "Hello, world!"}, routing_key=queue.name)

        IOLoop.current().add_timeout(time() + 0.2, self.stop)
        self.wait()

        self.assertEqual(counter["n"], 1)
Esempio n. 5
0
    def test_basis(self):
        queue = AsyncManager()
        self.addCleanup(queue.close_connection)
        queue.connect(host=HOST)

        counter = {"n": 0}

        def on_message(body):
            self.assertEqual(body, {"message": "Hello, world!"})
            counter["n"] += 1

        queue.start_consuming(on_message, )

        queue.publish({"message": "Hello, world!"}, routing_key=queue.name)

        IOLoop.current().add_timeout(time() + 0.2, self.stop)
        self.wait()

        self.assertEqual(counter["n"], 1)
Esempio n. 6
0
    def test_serializer(self):
        class SomeObject(object):
            def __init__(self, entity):
                self.entity = entity

            def __repr__(self):
                return self.entity

        queue = AsyncManager()
        self.addCleanup(queue.close_connection)
        queue.connect(host=HOST)

        counter = {"n": 0}

        def on_message(body):
            self.assertEqual(body, {"message": "Hello, world!"})
            counter["n"] += 1

        queue.start_consuming(on_message, )

        self.assertRaises(
            TypeError,
            lambda: queue.publish({"message": SomeObject("Hello, world!")},
                                  routing_key=queue.name))

        def serializer(o):
            if isinstance(o, SomeObject):
                return repr(o)
            raise TypeError(repr(o) + " is not JSON serializable")

        queue.publish({"message": SomeObject("Hello, world!")},
                      routing_key=queue.name,
                      serializer=serializer)
        IOLoop.current().add_timeout(time() + 0.2, self.stop)
        self.wait()

        self.assertEqual(counter["n"], 1)