test.py 4.0 KB

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