test.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. # Ensure msgpack-python and cbor are installed first, using:
  6. # pip install --user msgpack-python
  7. # pip install --user cbor
  8. import cbor, msgpack, msgpackrpc, sys, os, threading
  9. def 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. u"someday",
  29. u"",
  30. u"bytestring",
  31. 1328176922000002000,
  32. -2206187877999998000,
  33. 270,
  34. -2013855847999995777,
  35. #-6795364578871345152,
  36. ]
  37. l1 = [
  38. { "true": True,
  39. "false": False },
  40. { "true": "True",
  41. "false": False,
  42. "uint16(1616)": 1616 },
  43. { "list": [1616, 32323232, True, -3232.0, {"TRUE":True, "FALSE":False}, [True, False] ],
  44. "int32":32323232, "bool": True,
  45. "LONG STRING": "123456789012345678901234567890123456789012345678901234567890",
  46. "SHORT STRING": "1234567890" },
  47. { True: "true", 8: False, "false": 0 }
  48. ]
  49. l = []
  50. l.extend(l0)
  51. l.append(l0)
  52. l.extend(l1)
  53. return l
  54. def build_test_data(destdir):
  55. l = get_test_data_list()
  56. for i in range(len(l)):
  57. # packer = msgpack.Packer()
  58. serialized = msgpack.dumps(l[i])
  59. f = open(os.path.join(destdir, str(i) + '.msgpack.golden'), 'wb')
  60. f.write(serialized)
  61. f.close()
  62. serialized = cbor.dumps(l[i])
  63. f = open(os.path.join(destdir, str(i) + '.cbor.golden'), 'wb')
  64. f.write(serialized)
  65. f.close()
  66. def doRpcServer(port, stopTimeSec):
  67. class EchoHandler(object):
  68. def Echo123(self, msg1, msg2, msg3):
  69. return ("1:%s 2:%s 3:%s" % (msg1, msg2, msg3))
  70. def EchoStruct(self, msg):
  71. return ("%s" % msg)
  72. addr = msgpackrpc.Address('localhost', port)
  73. server = msgpackrpc.Server(EchoHandler())
  74. server.listen(addr)
  75. # run thread to stop it after stopTimeSec seconds if > 0
  76. if stopTimeSec > 0:
  77. def myStopRpcServer():
  78. server.stop()
  79. t = threading.Timer(stopTimeSec, myStopRpcServer)
  80. t.start()
  81. server.start()
  82. def 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. def doRpcClientToGoSvc(port):
  88. # print ">>>> port: ", port, " <<<<<"
  89. address = msgpackrpc.Address('localhost', port)
  90. client = msgpackrpc.Client(address, unpack_encoding='utf-8')
  91. print client.call("TestRpcInt.Echo123", ["A1", "B2", "C3"])
  92. print client.call("TestRpcInt.EchoStruct", {"A" :"Aa", "B":"Bb", "C":"Cc"})
  93. def doMain(args):
  94. if len(args) == 2 and args[0] == "testdata":
  95. build_test_data(args[1])
  96. elif len(args) == 3 and args[0] == "rpc-server":
  97. doRpcServer(int(args[1]), int(args[2]))
  98. elif len(args) == 2 and args[0] == "rpc-client-python-service":
  99. doRpcClientToPythonSvc(int(args[1]))
  100. elif len(args) == 2 and args[0] == "rpc-client-go-service":
  101. doRpcClientToGoSvc(int(args[1]))
  102. else:
  103. print("Usage: test.py " +
  104. "[testdata|rpc-server|rpc-client-python-service|rpc-client-go-service] ...")
  105. if __name__ == "__main__":
  106. doMain(sys.argv[1:])