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-get install python-dev
9 # sudo apt-get install python-pip
10 # pip install --user msgpack-python msgpack-rpc-python cbor
12 # Ensure all "string" keys are utf strings (else encoded as bytes)
14 import cbor, msgpack, msgpackrpc, sys, os, threading
16 def get_test_data_list():
17 # get list with all primitive types, and a combo type
45 #-6795364578871345152,
52 "uint16(1616)": 1616 },
53 { "list": [1616, 32323232, True, -3232.0, {"TRUE":True, "FALSE":False}, [True, False] ],
54 "int32":32323232, "bool": True,
55 "LONG STRING": u"123456789012345678901234567890123456789012345678901234567890",
56 "SHORT STRING": u"1234567890" },
57 { True: "true", 138: False, "false": 200 }
67 def build_test_data(destdir):
68 l = get_test_data_list()
69 for i in range(len(l)):
70 # packer = msgpack.Packer()
71 serialized = msgpack.dumps(l[i])
72 f = open(os.path.join(destdir, str(i) + '.msgpack.golden'), 'wb')
75 serialized = cbor.dumps(l[i])
76 f = open(os.path.join(destdir, str(i) + '.cbor.golden'), 'wb')
80 def doRpcServer(port, stopTimeSec):
81 class EchoHandler(object):
82 def Echo123(self, msg1, msg2, msg3):
83 return ("1:%s 2:%s 3:%s" % (msg1, msg2, msg3))
84 def EchoStruct(self, msg):
87 addr = msgpackrpc.Address('127.0.0.1', port)
88 server = msgpackrpc.Server(EchoHandler())
90 # run thread to stop it after stopTimeSec seconds if > 0
92 def myStopRpcServer():
94 t = threading.Timer(stopTimeSec, myStopRpcServer)
98 def doRpcClientToPythonSvc(port):
99 address = msgpackrpc.Address('127.0.0.1', port)
100 client = msgpackrpc.Client(address, unpack_encoding='utf-8')
101 print client.call("Echo123", "A1", "B2", "C3")
102 print client.call("EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"})
104 def doRpcClientToGoSvc(port):
105 # print ">>>> port: ", port, " <<<<<"
106 address = msgpackrpc.Address('127.0.0.1', port)
107 client = msgpackrpc.Client(address, unpack_encoding='utf-8')
108 print client.call("TestRpcInt.Echo123", ["A1", "B2", "C3"])
109 print client.call("TestRpcInt.EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"})
112 if len(args) == 2 and args[0] == "testdata":
113 build_test_data(args[1])
114 elif len(args) == 3 and args[0] == "rpc-server":
115 doRpcServer(int(args[1]), int(args[2]))
116 elif len(args) == 2 and args[0] == "rpc-client-python-service":
117 doRpcClientToPythonSvc(int(args[1]))
118 elif len(args) == 2 and args[0] == "rpc-client-go-service":
119 doRpcClientToGoSvc(int(args[1]))
121 print("Usage: test.py " +
122 "[testdata|rpc-server|rpc-client-python-service|rpc-client-go-service] ...")
124 if __name__ == "__main__":