3
# This will create golden files in a directory passed to it.
4
# A Test calls this internally to create the golden files
5
# So it can process them (so we don't have to checkin the files).
7
# Ensure msgpack-python and cbor are installed first, using:
8
# sudo apt install python-dev (may not be necessary)
9
# sudo apt install python-pip # or python3-pip
10
# pip install --user msgpack-python msgpack-rpc-python cbor
12
# Ensure all "string" keys are utf strings (else encoded as bytes)
14
from __future__ import print_function
15
import cbor, msgpack, msgpackrpc, sys, os, threading
17
mylocaladdr="127.0.0.1" # localhost.localdomain localhost 127.0.0.1
19
def get_test_data_list():
20
# get list with all primitive types, and a combo type
50
#-6795364578871345152,
57
"uint16(1616)": 1616 },
58
{ "list": [1616, 32323232, True, -3232.0, {"TRUE":True, "FALSE":False}, [True, False] ],
59
"int32":32323232, "bool": True,
60
"LONG STRING": u"123456789012345678901234567890123456789012345678901234567890",
61
"SHORT STRING": u"1234567890" },
62
{ True: "true", 138: False, "false": 200 }
72
def build_test_data(destdir):
73
l = get_test_data_list()
74
for i in range(len(l)):
75
# packer = msgpack.Packer()
76
serialized = msgpack.dumps(l[i])
77
with open(os.path.join(destdir, str(i) + '.msgpack.golden'), 'wb') as f:
79
serialized = cbor.dumps(l[i])
80
with open(os.path.join(destdir, str(i) + '.cbor.golden'), 'wb') as f:
83
def doRpcServer(port, stopTimeSec):
84
class EchoHandler(object):
85
def Echo123(self, msg1, msg2, msg3):
86
return ("1:%s 2:%s 3:%s" % (msg1.decode("utf-8"), msg2.decode("utf-8"), msg3.decode("utf-8")))
87
def EchoStruct(self, msg):
90
addr = msgpackrpc.Address(mylocaladdr, port)
91
server = msgpackrpc.Server(EchoHandler())
93
# run thread to stop it after stopTimeSec seconds if > 0
95
def myStopRpcServer():
97
t = threading.Timer(stopTimeSec, myStopRpcServer)
101
def doRpcClientToPythonSvc(port):
102
address = msgpackrpc.Address(mylocaladdr, port)
103
client = msgpackrpc.Client(address, unpack_encoding='utf-8')
104
print(client.call("Echo123", "A1", "B2", "C3"))
105
print(client.call("EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"}))
107
# def doCheckSocket(port):
108
# print(">>>> port: ", port, " <<<<<")
109
# sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
110
# result = sock.connect_ex(('127.0.0.1', port))
112
# print("\t>>>> Port is open")
114
# print("\t>>>> Port is not open")
117
def doRpcClientToGoSvc(port):
118
# doCheckSocket(port)
119
address = msgpackrpc.Address(mylocaladdr, port)
120
client = msgpackrpc.Client(address, unpack_encoding='utf-8')
121
print(client.call("TestRpcInt.Echo123", ["A1", "B2", "C3"]))
122
print(client.call("TestRpcInt.EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"}))
125
if len(args) == 2 and args[0] == "testdata":
126
build_test_data(args[1])
127
elif len(args) == 3 and args[0] == "rpc-server":
128
doRpcServer(int(args[1]), int(args[2]))
129
elif len(args) == 2 and args[0] == "rpc-client-python-service":
130
doRpcClientToPythonSvc(int(args[1]))
131
elif len(args) == 2 and args[0] == "rpc-client-go-service":
132
doRpcClientToGoSvc(int(args[1]))
134
print("Usage: test.py " +
135
"[testdata|rpc-server|rpc-client-python-service|rpc-client-go-service] ...")
137
if __name__ == "__main__":