浏览代码

Add new "rabbitmq-basics" test

Tianon Gravi 8 年之前
父节点
当前提交
3f2537be1d

+ 1 - 0
test/config.sh

@@ -162,6 +162,7 @@ imageTests+=(
 		python-sqlite3
 	'
 	[rabbitmq]='
+		rabbitmq-basics
 	'
 	[r-base]='
 	'

+ 19 - 0
test/tests/rabbitmq-basics/receive.py

@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+import pika
+
+# https://www.rabbitmq.com/tutorials/tutorial-one-python.html
+
+connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq'))
+channel = connection.channel()
+
+channel.queue_declare(queue='hello')
+
+def callback(ch, method, properties, body):
+    print(body.decode('utf-8'))
+    connection.close()
+
+channel.basic_consume(callback,
+                      queue='hello',
+                      no_ack=True)
+
+channel.start_consuming()

+ 39 - 0
test/tests/rabbitmq-basics/run.sh

@@ -0,0 +1,39 @@
+#!/usr/bin/env bash
+set -Eeuo pipefail
+
+# https://www.rabbitmq.com/tutorials/tutorial-one-python.html
+
+dir="$(dirname "$(readlink -f "$BASH_SOURCE")")"
+
+serverImage="$1"
+
+clientImage="$("$dir/../image-name.sh" librarytest/rabbitmq-basics "$serverImage")"
+"$dir/../docker-build.sh" "$dir" "$clientImage" <<EOD
+FROM python:3.6-slim
+RUN pip install pika==0.11.0
+COPY dir/*.py /usr/local/bin/
+EOD
+
+cname="rabbitmq-container-$RANDOM-$RANDOM"
+cid="$(docker run -d --name "$cname" "$serverImage")"
+trap "docker rm -vf $cid > /dev/null" EXIT
+
+client() {
+	docker run -i --rm --link "$cname":rabbitmq "$clientImage" "$@"
+}
+
+. "$dir/../../retry.sh" 'client testconn.py'
+
+test-send-recv() {
+	local payload="$1"; shift
+	client send.py "$payload"
+	response="$(client receive.py)"
+	if [ "$payload" != "$response" ]; then
+		echo >&2 "error: expected '$payload' but got '$response' instead"
+		return 1
+	fi
+}
+
+test-send-recv 'hello'
+test-send-recv "$RANDOM"
+test-send-recv $'a\nb\nc\td'

+ 15 - 0
test/tests/rabbitmq-basics/send.py

@@ -0,0 +1,15 @@
+#!/usr/bin/env python3
+import pika, sys
+
+# https://www.rabbitmq.com/tutorials/tutorial-one-python.html
+
+connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq'))
+channel = connection.channel()
+
+channel.queue_declare(queue='hello')
+
+channel.basic_publish(exchange='',
+                      routing_key='hello',
+                      body=sys.argv[1])
+
+connection.close()

+ 11 - 0
test/tests/rabbitmq-basics/testconn.py

@@ -0,0 +1,11 @@
+#!/usr/bin/env python3
+import pika
+
+# https://www.rabbitmq.com/tutorials/tutorial-one-python.html
+
+connection = pika.BlockingConnection(pika.ConnectionParameters('rabbitmq'))
+channel = connection.channel()
+
+channel.queue_declare(queue='hello')
+
+connection.close()