Repository: celery/kombu

Issue:
How to use in memory transport for unit tests ?
Hey guys,

I just want to write some functional tests were I produce a message to the queue and see if the message gets correctly handled. Therefore I trying to use the memory transport, but can't find much documentation about it. 

So my question is, would it be possible that you guys add an test example to the docs, or just post one directly here ?



Discussion:
@c4pone Why did you close it? I was just having the same question. I guess the solution is to simply create an in-memory transport, then to check whether a function produces a message, have a consumer in your test function wait until it consumes a message? Even if this is how one should go about it, a couple of examples in the documentation would be very nice.

Hey @tcwalther, I found shortly after I asked this question the following unit test https://github.com/celery/kombu/blob/master/kombu/tests/transport/test_memory.py

also
http://stackoverflow.com/questions/38149888/how-to-properly-use-the-in-memory-transport-for-unit-tests

But I agree with you, a simple example in the documentation would be really nice.

Feel free to add this example in the documentation, you can even edit the files on Github directly and submit a pull request that way :)

What about an example like this?

``` python
"""
Example that use memory transport for in process commuication.
"""
import threading
from time import sleep, time
from kombu import Connection, Exchange, Queue, Consumer
from kombu.utils.debug import setup_logging

media_exchange = Exchange('media', 'direct')
video_queue = Queue('video', exchange=media_exchange, routing_key='video')
task_queues = [video_queue]


class Worker(threading.Thread):

    def run(self):
        connection = Connection("memory:///", transport_options={"polling_interval": 0})
        while True:
            with Consumer(connection, task_queues, callbacks=[self.on_message]):
                connection.drain_events()

    def on_message(self, body, message):
        print("%s RECEIVED MESSAGE: %r" % (time(), body))
        message.ack()


if __name__ == '__main__':
    setup_logging(loglevel='INFO', loggers=[''])

    try:
        worker = Worker()
        worker.start()
    except KeyboardInterrupt:
        print('bye bye')

    while True:
        with Connection('memory:///') as conn:
            producer = conn.Producer(serializer='json')
            producer.publish({"foo": "bar"}, exchange=media_exchange, routing_key='video', declare=task_queues)
        sleep(0.1)
```


Resolving patch:
diff --git a/examples/memory_transport.py b/examples/memory_transport.py
new file mode 100644
--- /dev/null
+++ b/examples/memory_transport.py
@@ -0,0 +1,24 @@
+"""
+Example that use memory transport for message produce.
+"""
+import time
+
+from kombu import Connection, Exchange, Queue, Consumer
+
+media_exchange = Exchange('media', 'direct')
+video_queue = Queue('video', exchange=media_exchange, routing_key='video')
+task_queues = [video_queue]
+
+
+def handle_message(body, message):
+    print("%s RECEIVED MESSAGE: %r" % (time.time(), body))
+    message.ack()
+
+
+connection = Connection("memory:///")
+consumer = Consumer(connection, task_queues, callbacks=[handle_message])
+
+producer = connection.Producer(serializer='json')
+producer.publish({"foo": "bar"}, exchange=media_exchange, routing_key='video', declare=task_queues)
+consumer.consume()
+connection.drain_events()


Test patch: