test.py 3.8 KB

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