Debian Codegraph @

lang/py3/avro/tests/test_ipc.py

raw
1 #!/usr/bin/env python3
2 # -*- mode: python -*-
3 # -*- coding: utf-8 -*-
4
5 # Licensed to the Apache Software Foundation (ASF) under one
6 # or more contributor license agreements. See the NOTICE file
7 # distributed with this work for additional information
8 # regarding copyright ownership. The ASF licenses this file
9 # to you under the Apache License, Version 2.0 (the
10 # "License"); you may not use this file except in compliance
11 # with the License. You may obtain a copy of the License at
12 #
13 # http://www.apache.org/licenses/LICENSE-2.0
14 #
15 # Unless required by applicable law or agreed to in writing, software
16 # distributed under the License is distributed on an "AS IS" BASIS,
17 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 # See the License for the specific language governing permissions and
19 # limitations under the License.
20 """
21 There are currently no IPC tests within python, in part because there are no
22 servers yet available.
23 """
24
25 import logging
26 import threading
27 import time
28 import unittest
29
30 from avro import ipc
31 from avro import protocol
32 from avro import schema
33
34
35 def NowMS():
36 return int(time.time() * 1000)
37
38
39 ECHO_PROTOCOL_JSON = """
40 {
41 "protocol" : "Echo",
42 "namespace" : "org.apache.avro.ipc.echo",
43 "types" : [ {
44 "type" : "record",
45 "name" : "Ping",
46 "fields" : [ {
47 "name" : "timestamp",
48 "type" : "long",
49 "default" : -1
50 }, {
51 "name" : "text",
52 "type" : "string",
53 "default" : ""
54 } ]
55 }, {
56 "type" : "record",
57 "name" : "Pong",
58 "fields" : [ {
59 "name" : "timestamp",
60 "type" : "long",
61 "default" : -1
62 }, {
63 "name" : "ping",
64 "type" : "Ping"
65 } ]
66 } ],
67 "messages" : {
68 "ping" : {
69 "request" : [ {
70 "name" : "ping",
71 "type" : "Ping"
72 } ],
73 "response" : "Pong"
74 }
75 }
76 }
77 """
78
79
80 ECHO_PROTOCOL = protocol.Parse(ECHO_PROTOCOL_JSON)
81
82
83 class EchoResponder(ipc.Responder):
84 def __init__(self):
85 super(EchoResponder, self).__init__(
86 local_protocol=ECHO_PROTOCOL,
87 )
88
89 def Invoke(self, message, request):
90 logging.info('Message: %s', message)
91 logging.info('Request: %s', request)
92 ping = request['ping']
93 return {'timestamp': NowMS(), 'ping': ping}
94
95
96 class TestIPC(unittest.TestCase):
97
98 def __init__(self, *args, **kwargs):
99 super(TestIPC, self).__init__(*args, **kwargs)
100 # Reference to an Echo RPC over HTTP server:
101 self._server = None
102
103 def StartEchoServer(self):
104 self._server = ipc.AvroIpcHttpServer(
105 interface='localhost',
106 port=0,
107 responder=EchoResponder(),
108 )
109
110 def ServerThread():
111 self._server.serve_forever()
112
113 self._server_thread = threading.Thread(target=ServerThread)
114 self._server_thread.start()
115
116 logging.info(
117 'Echo RPC Server listening on %s:%s',
118 *self._server.server_address)
119 logging.info('RPC socket: %s', self._server.socket)
120
121 def StopEchoServer(self):
122 assert (self._server is not None)
123 self._server.shutdown()
124 self._server_thread.join()
125 self._server.server_close()
126 self._server = None
127
128 def testEchoService(self):
129 """Tests client-side of the Echo service."""
130 self.StartEchoServer()
131 try:
132 (server_host, server_port) = self._server.server_address
133
134 transceiver = ipc.HTTPTransceiver(host=server_host, port=server_port)
135 requestor = ipc.Requestor(
136 local_protocol=ECHO_PROTOCOL,
137 transceiver=transceiver,
138 )
139 response = requestor.Request(
140 message_name='ping',
141 request_datum={'ping': {'timestamp': 31415, 'text': 'hello ping'}},
142 )
143 logging.info('Received echo response: %s', response)
144
145 response = requestor.Request(
146 message_name='ping',
147 request_datum={'ping': {'timestamp': 123456, 'text': 'hello again'}},
148 )
149 logging.info('Received echo response: %s', response)
150
151 transceiver.Close()
152
153 finally:
154 self.StopEchoServer()
155
156
157 if __name__ == '__main__':
158 raise Exception('Use run_tests.py')