summaryrefslogtreecommitdiff
path: root/python/examples
diff options
context:
space:
mode:
authorJonathan Robie <jonathan@apache.org>2009-03-12 18:59:07 +0000
committerJonathan Robie <jonathan@apache.org>2009-03-12 18:59:07 +0000
commit47db7ebfc533d2dc8a3dfc3b9d1273730b8779e6 (patch)
tree3ccb070ef37776515649778b8d4881d91b4036bf /python/examples
parent1d5ef574d0dc29d1bfce20dcbd091b2a9aaa7809 (diff)
downloadqpid-python-47db7ebfc533d2dc8a3dfc3b9d1273730b8779e6.tar.gz
Tests Unicode and handling of all datatypes in application
headers. Things commented out don't work yet. git-svn-id: https://svn.apache.org/repos/asf/qpid/trunk/qpid@752972 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'python/examples')
-rwxr-xr-xpython/examples/datatypes/client.py122
-rwxr-xr-xpython/examples/datatypes/server.py124
-rw-r--r--python/examples/datatypes/testdata.py180
-rw-r--r--python/examples/datatypes/verify24
4 files changed, 450 insertions, 0 deletions
diff --git a/python/examples/datatypes/client.py b/python/examples/datatypes/client.py
new file mode 100755
index 0000000000..088e529909
--- /dev/null
+++ b/python/examples/datatypes/client.py
@@ -0,0 +1,122 @@
+#!/usr/bin/env python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+"""
+ client.py
+
+ Client for testing use of Unicode and datatypes.
+
+ Both client and server will be written in C++ and Python.
+ Tests can run clients and servers written in different
+ languages, and they can be run on 32-bit and 64-bit architectures.
+
+"""
+
+import qpid
+import sys
+import os
+from qpid.util import connect
+from qpid.connection import Connection
+from qpid.datatypes import Message, RangedSet, uuid4
+from qpid.queue import Empty
+
+import testdata
+
+#----- Initialization --------------------------------------
+
+
+# Set parameters for login
+
+host="127.0.0.1"
+port=5672
+user="guest"
+password="guest"
+
+# If an alternate host or port has been specified, use that instead
+# (this is used in our unit tests)
+if len(sys.argv) > 1 :
+ host=sys.argv[1]
+if len(sys.argv) > 2 :
+ port=int(sys.argv[2])
+
+# Create a connection.
+socket = connect(host, port)
+connection = Connection (sock=socket, username=user, password=password)
+connection.start()
+session = connection.session(str(uuid4()))
+
+
+#----- Main Body -- ----------------------------------------
+
+# Create a response queue for the server to send responses to. Use the
+# same string as the name of the queue and the name of the routing
+# key.
+
+reply_to = "reply_to:" + session.name
+session.queue_declare(queue=reply_to, exclusive=True)
+session.exchange_bind(exchange="amq.direct", queue=reply_to, binding_key=reply_to)
+
+# Create a local queue and subscribe it to the response queue
+
+local_queue_name = "local_queue"
+queue = session.incoming(local_queue_name)
+
+# Call message_subscribe() to tell the broker to deliver messages from
+# the server's reply_to queue to our local client queue. The server
+# will start delivering messages as soon as message credit is
+# available.
+
+session.message_subscribe(queue=reply_to, destination=local_queue_name)
+queue.start()
+
+# Set up the properties. Perhaps a few application headers?
+
+delivery_properties = session.delivery_properties(routing_key="request")
+
+message_properties = session.message_properties()
+
+message_properties.content_encoding="text/plain; charset='utf-8'"
+
+testdata.set_application_headers(message_properties)
+message_properties.reply_to = session.reply_to("amq.direct", reply_to)
+
+# deliver the message - remember to encode the Unicode string!
+request = Message(message_properties, delivery_properties, testdata.String_Greek.encode("utf8"))
+session.message_transfer(destination="amq.direct", message=request)
+
+# Now see what messages the server sent to our reply_to queue
+
+try:
+ response = queue.get(timeout=10)
+ content = response.body
+ session.message_accept(RangedSet(response.id))
+ testdata.check_message(response)
+ print "Response: " + content
+except Empty:
+ print "No more messages!"
+ exit(1)
+except:
+ print "Unexpected exception!"
+ exit(1)
+
+#----- Cleanup ------------------------------------------------
+
+# Clean up before exiting so there are no open threads.
+
+session.close(timeout=10)
diff --git a/python/examples/datatypes/server.py b/python/examples/datatypes/server.py
new file mode 100755
index 0000000000..18e6fa4ad7
--- /dev/null
+++ b/python/examples/datatypes/server.py
@@ -0,0 +1,124 @@
+#!/usr/bin/env python
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+"""
+ server.py
+
+ Server for testing use of Unicode and datatypes.
+
+ Both client and server will be written in C++ and Python.
+ Tests can run clients and servers written in different
+ languages, and they can be run on 32-bit and 64-bit architectures.
+"""
+
+import testdata
+
+import qpid
+import sys
+import os
+from qpid.util import connect
+from qpid.connection import Connection
+from qpid.datatypes import Message, RangedSet, uuid4
+from qpid.queue import Empty
+
+#----- Functions -------------------------------------------
+def respond(session, request):
+
+ # The routing key for the response is the request's reply-to
+ # property. The body for the response is the request's body,
+ # converted to upper case.
+
+ testdata.check_message(request)
+
+ message_properties = request.get("message_properties")
+ reply_to = message_properties.reply_to
+
+ testdata.set_application_headers(message_properties)
+
+ if reply_to == None:
+ raise Exception("This message is missing the 'reply_to' property, which is required")
+
+ delivery_properties = session.delivery_properties(routing_key=reply_to["routing_key"])
+ response = Message(delivery_properties, message_properties, testdata.String_Greek.encode("utf8"))
+ print "Sending response ..."
+ session.message_transfer(destination=reply_to["exchange"], message=response)
+
+#----- Initialization --------------------------------------
+
+
+# Set parameters for login
+
+host="127.0.0.1"
+port=5672
+user="guest"
+password="guest"
+
+# If an alternate host or port has been specified, use that instead
+# (this is used in our unit tests)
+if len(sys.argv) > 1 :
+ host=sys.argv[1]
+if len(sys.argv) > 2 :
+ port=int(sys.argv[2])
+
+socket = connect(host, port)
+connection = Connection (sock=socket, username=user, password=password)
+connection.start()
+session = connection.session(str(uuid4()))
+
+#----- Main Body -- ----------------------------------------
+
+# Create a request queue and subscribe to it
+
+session.queue_declare(queue="request", exclusive=True)
+session.exchange_bind(exchange="amq.direct", queue="request", binding_key="request")
+
+local_queue_name = "local_queue"
+
+session.message_subscribe(queue="request", destination=local_queue_name)
+
+queue = session.incoming(local_queue_name)
+queue.start()
+
+# Remind the user to start the client program
+
+print "Request server running - run your client now."
+print "(Times out after 100 seconds ...)"
+sys.stdout.flush()
+
+# Respond to each request
+
+# If we get a message, send it back to the user (as indicated in the
+# ReplyTo property)
+
+while True:
+ try:
+ request = queue.get(timeout=100)
+ session.message_accept(RangedSet(request.id))
+
+ respond(session, request)
+ except Empty:
+ print "No more messages!"
+ break;
+
+
+#----- Cleanup ------------------------------------------------
+
+# Clean up before exiting so there are no open threads.
+
+session.close(timeout=10)
diff --git a/python/examples/datatypes/testdata.py b/python/examples/datatypes/testdata.py
new file mode 100644
index 0000000000..cdf140d400
--- /dev/null
+++ b/python/examples/datatypes/testdata.py
@@ -0,0 +1,180 @@
+# -*- encoding: utf-8 -*-
+
+from qpid.datatypes import uuid4, timestamp
+
+#----- Some variables to test boundary conditions on various data types
+
+void = None
+boolean_true = True
+boolean_false = False
+Uint8_0 = 0
+Uint8_max = 255
+Uint16_0 = 0
+Uint16_max = 65535
+Uint32_0 = 0
+Uint32_max = 4294967295
+Uint64_0 = 0
+Uint64_max = 18446744073709551615
+Int8_min = -128
+Int8_0 = 0
+Int8_max = 127
+Int16_min = -32768
+Int16_0 = 0
+Int16_max = 32767
+Int32_min = -2147483648
+Int32_0 = 0
+Int32_max = 2147483647
+Int64_min = -9223372036854775808
+Int64_0 = 0
+Int64_max = 9223372036854775807
+
+Float_pi = 3.14159265
+Float_neg = -1E4
+Float_big = 1267.43233E12
+Float_small = 12.78e-12
+Float_neg0 = -0
+Float_pos0 = 0
+Float_INF = float('inf')
+Float_Negative_INF = float('-inf')
+
+Double_pi = 3.1415926535897932384626433832795
+Double_neg = -1E4
+Double_big = 1267.43233E12
+Double_small = 12.78e-2
+Double_neg0 = -0
+Double_pos0 = 0
+Double_INF = float('inf')
+Double_Negative_INF = float('-inf')
+
+char_1byte = u'0024' # $
+char_2byte = u'00A2' # ¢
+char_3byte = u'20AC' # €
+char_4byte = u'10ABCD'
+
+timestamp = timestamp()
+
+UUID = uuid4()
+
+String_Greek = u"ἐξίσταντο δὲ πάντες καὶ διηπόρουν, ἄλλος πρὸς ἄλλον λέγοντες, Τί θέλει τοῦτο εἶναι;"
+
+String_Empty = ""
+
+#----- A few functions ----------------------------------------------------------
+
+def near_enough(float1, float2, delta):
+ return abs(float1-float2) < delta
+
+def set_application_headers(message_properties):
+
+ message_properties.application_headers = {}
+ message_properties.application_headers["void"] = None
+ message_properties.application_headers["boolean_true"] = boolean_true
+ message_properties.application_headers["boolean_false"] = boolean_false
+ message_properties.application_headers["Uint8_0"] = Uint8_0
+ message_properties.application_headers["Uint8_max"] = Uint8_max
+ message_properties.application_headers["Uint16_0"] = Uint16_0
+ message_properties.application_headers["Uint16_max"] = Uint16_max
+ message_properties.application_headers["Uint32_0"] = Uint32_0
+ message_properties.application_headers["Uint32_max"] = Uint32_max
+ message_properties.application_headers["Uint64_0"] = Uint64_0
+# message_properties.application_headers["Uint64_max"] = Uint64_max
+ message_properties.application_headers["Int8_min"] = Int8_min
+ message_properties.application_headers["Int8_0"] = Int8_0
+ message_properties.application_headers["Int8_max"] = Int8_max
+ message_properties.application_headers["Int16_min"] = Int16_min
+ message_properties.application_headers["Int16_0"] = Int16_0
+ message_properties.application_headers["Int16_max"] = Int16_max
+ message_properties.application_headers["Int32_min"] = Int32_min
+ message_properties.application_headers["Int32_0"] = Int32_0
+ message_properties.application_headers["Int32_max"] = Int32_max
+ message_properties.application_headers["Int64_min"] = Int64_min
+ message_properties.application_headers["Int64_0"] = Int64_0
+ message_properties.application_headers["Int64_max"] = Int64_max
+
+ message_properties.application_headers["Float_pi"] = Float_pi
+ message_properties.application_headers["Float_neg"] = Float_neg
+ message_properties.application_headers["Float_big"] = Float_big
+ message_properties.application_headers["Float_small"] = Float_small
+ message_properties.application_headers["Float_neg0"] = Float_neg0
+ message_properties.application_headers["Float_pos0"] = Float_pos0
+ message_properties.application_headers["Float_INF"] = Float_INF
+ message_properties.application_headers["Float_Negative_INF"] = Float_Negative_INF
+
+ message_properties.application_headers["Double_pi"] = Double_pi
+ message_properties.application_headers["Double_neg"] = Double_neg
+ message_properties.application_headers["Double_big"] = Double_big
+ message_properties.application_headers["Double_small"] = Double_small
+ message_properties.application_headers["Double_neg0"] = Double_neg0
+ message_properties.application_headers["Double_pos0"] = Double_pos0
+ message_properties.application_headers["Double_INF"] = Double_INF
+ message_properties.application_headers["Double_Negative_INF"] = Double_Negative_INF
+
+ message_properties.application_headers["char_1byte"] = char_1byte
+ message_properties.application_headers["char_2byte"] = char_2byte
+ message_properties.application_headers["char_3byte"] = char_3byte
+ message_properties.application_headers["char_4byte"] = char_4byte
+
+ message_properties.application_headers["timestamp"] = timestamp
+ message_properties.application_headers["UUID"] = uuid4()
+ message_properties.application_headers["String_Greek"] = String_Greek
+ message_properties.application_headers["String_Empty"] = String_Empty
+
+def check_message(message):
+
+# message_properties = message.message_properties()
+ message_properties = message.get("message_properties")
+ assert message_properties.application_headers["void"] == None
+ assert message_properties.application_headers["boolean_true"] == boolean_true
+ assert message_properties.application_headers["boolean_false"] == boolean_false
+ assert message_properties.application_headers["Uint8_0"] == Uint8_0
+ assert message_properties.application_headers["Uint8_max"] == Uint8_max
+ assert message_properties.application_headers["Uint16_0"] == Uint16_0
+ assert message_properties.application_headers["Uint16_max"] == Uint16_max
+ assert message_properties.application_headers["Uint32_0"] == Uint32_0
+ assert message_properties.application_headers["Uint32_max"] == Uint32_max
+ assert message_properties.application_headers["Uint64_0"] == Uint64_0
+# assert message_properties.application_headers["Uint64_max"] == Uint64_max
+ assert message_properties.application_headers["Int8_min"] == Int8_min
+ assert message_properties.application_headers["Int8_0"] == Int8_0
+ assert message_properties.application_headers["Int8_max"] == Int8_max
+ assert message_properties.application_headers["Int16_min"] == Int16_min
+ assert message_properties.application_headers["Int16_0"] == Int16_0
+ assert message_properties.application_headers["Int16_max"] == Int16_max
+ assert message_properties.application_headers["Int32_min"] == Int32_min
+ assert message_properties.application_headers["Int32_0"] == Int32_0
+ assert message_properties.application_headers["Int32_max"] == Int32_max
+ assert message_properties.application_headers["Int64_min"] == Int64_min
+ assert message_properties.application_headers["Int64_0"] == Int64_0
+ assert message_properties.application_headers["Int64_max"] == Int64_max
+
+# Change floating point comparisons to allow inexactness
+
+ assert near_enough(message_properties.application_headers["Float_pi"], Float_pi, 0.00001)
+ assert near_enough(message_properties.application_headers["Float_neg"], Float_neg, 0.00001)
+ assert near_enough(message_properties.application_headers["Float_big"], Float_big, Float_big/1000000)
+ assert near_enough(message_properties.application_headers["Float_small"], Float_small, 0.00001)
+ assert message_properties.application_headers["Float_neg0"] == Float_neg0
+ assert message_properties.application_headers["Float_pos0"] == Float_pos0
+ assert message_properties.application_headers["Float_INF"] == Float_INF
+ assert message_properties.application_headers["Float_Negative_INF"] == Float_Negative_INF
+
+ assert near_enough(message_properties.application_headers["Double_pi"], Double_pi, 0.00001)
+ assert near_enough(message_properties.application_headers["Double_neg"], Double_neg, 0.00001)
+ assert near_enough(message_properties.application_headers["Double_big"], Double_big, Double_big/1000000)
+ assert near_enough(message_properties.application_headers["Double_small"], Double_small, 0.00001)
+ assert message_properties.application_headers["Double_neg0"] == Double_neg0
+ assert message_properties.application_headers["Double_pos0"] == Double_pos0
+ assert message_properties.application_headers["Double_INF"] == Double_INF
+ assert message_properties.application_headers["Double_Negative_INF"] == Double_Negative_INF
+
+ assert message_properties.application_headers["char_1byte"] == char_1byte
+ assert message_properties.application_headers["char_2byte"] == char_2byte
+ assert message_properties.application_headers["char_3byte"] == char_3byte
+ assert message_properties.application_headers["char_4byte"] == char_4byte
+
+# assert message_properties.application_headers["timestamp"] == timestamp
+# assert message_properties.application_headers["UUID"] == UUID
+ assert message_properties.application_headers["String_Greek"] == String_Greek
+ assert message_properties.application_headers["String_Empty"] == String_Empty
+
+
diff --git a/python/examples/datatypes/verify b/python/examples/datatypes/verify
new file mode 100644
index 0000000000..cdc41b454e
--- /dev/null
+++ b/python/examples/datatypes/verify
@@ -0,0 +1,24 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one
+# or more contributor license agreements. See the NOTICE file
+# distributed with this work for additional information
+# regarding copyright ownership. The ASF licenses this file
+# to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance
+# with the License. You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing,
+# software distributed under the License is distributed on an
+# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+# KIND, either express or implied. See the License for the
+# specific language governing permissions and limitations
+# under the License.
+#
+
+# See https://svn.apache.org/repos/asf/incubator/qpid/trunk/qpid/bin/verify
+background "Request server running" ./server.py
+clients ./client.py
+kill %% # Must kill the server.
+outputs "./client.py.out" " server.py.out"