msgpack_test.py 3.4 KB

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