1#!/usr/bin/env python 2 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). 6 7import msgpack, msgpackrpc, sys, os, threading 8 9def get_test_data_list(): 10 # get list with all primitive types, and a combo type 11 l0 = [ 12 -8, 13 -1616, 14 -32323232, 15 -6464646464646464, 16 192, 17 1616, 18 32323232, 19 6464646464646464, 20 192, 21 -3232.0, 22 -6464646464.0, 23 3232.0, 24 6464646464.0, 25 False, 26 True, 27 None, 28 "someday", 29 "", 30 "bytestring", 31 1328176922000002000, 32 -2206187877999998000, 33 0, 34 -6795364578871345152 35 ] 36 l1 = [ 37 { "true": True, 38 "false": False }, 39 { "true": "True", 40 "false": False, 41 "uint16(1616)": 1616 }, 42 { "list": [1616, 32323232, True, -3232.0, {"TRUE":True, "FALSE":False}, [True, False] ], 43 "int32":32323232, "bool": True, 44 "LONG STRING": "123456789012345678901234567890123456789012345678901234567890", 45 "SHORT STRING": "1234567890" }, 46 { True: "true", 8: False, "false": 0 } 47 ] 48 49 l = [] 50 l.extend(l0) 51 l.append(l0) 52 l.extend(l1) 53 return l 54 55def build_test_data(destdir): 56 l = get_test_data_list() 57 for i in range(len(l)): 58 packer = msgpack.Packer() 59 serialized = packer.pack(l[i]) 60 f = open(os.path.join(destdir, str(i) + '.golden'), 'wb') 61 f.write(serialized) 62 f.close() 63 64def doRpcServer(port, stopTimeSec): 65 class EchoHandler(object): 66 def Echo123(self, msg1, msg2, msg3): 67 return ("1:%s 2:%s 3:%s" % (msg1, msg2, msg3)) 68 def EchoStruct(self, msg): 69 return ("%s" % msg) 70 71 addr = msgpackrpc.Address('localhost', port) 72 server = msgpackrpc.Server(EchoHandler()) 73 server.listen(addr) 74 # run thread to stop it after stopTimeSec seconds if > 0 75 if stopTimeSec > 0: 76 def myStopRpcServer(): 77 server.stop() 78 t = threading.Timer(stopTimeSec, myStopRpcServer) 79 t.start() 80 server.start() 81 82def doRpcClientToPythonSvc(port): 83 address = msgpackrpc.Address('localhost', port) 84 client = msgpackrpc.Client(address, unpack_encoding='utf-8') 85 print client.call("Echo123", "A1", "B2", "C3") 86 print client.call("EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"}) 87 88def doRpcClientToGoSvc(port): 89 # print ">>>> port: ", port, " <<<<<" 90 address = msgpackrpc.Address('localhost', port) 91 client = msgpackrpc.Client(address, unpack_encoding='utf-8') 92 print client.call("TestRpcInt.Echo123", ["A1", "B2", "C3"]) 93 print client.call("TestRpcInt.EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"}) 94 95def doMain(args): 96 if len(args) == 2 and args[0] == "testdata": 97 build_test_data(args[1]) 98 elif len(args) == 3 and args[0] == "rpc-server": 99 doRpcServer(int(args[1]), int(args[2])) 100 elif len(args) == 2 and args[0] == "rpc-client-python-service": 101 doRpcClientToPythonSvc(int(args[1])) 102 elif len(args) == 2 and args[0] == "rpc-client-go-service": 103 doRpcClientToGoSvc(int(args[1])) 104 else: 105 print("Usage: msgpack_test.py " + 106 "[testdata|rpc-server|rpc-client-python-service|rpc-client-go-service] ...") 107 108if __name__ == "__main__": 109 doMain(sys.argv[1:]) 110 111