test.py 4.4 KB

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