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