nr_shell_test.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. import subprocess
  2. import glob
  3. import os
  4. import sys
  5. import functools
  6. import argparse
  7. def process_testcases(testcases, bin):
  8. key_log_paths = []
  9. log_pattern = os.path.join(testcases, "*.log")
  10. key_log_paths = glob.glob(log_pattern)
  11. test_print = functools.partial(print, "[test log]")
  12. test_print(f"find {len(key_log_paths)} logs:")
  13. for path in key_log_paths:
  14. test_print(f" {path}")
  15. try:
  16. process = subprocess.Popen(
  17. [bin],
  18. stdin=subprocess.PIPE,
  19. stdout=sys.stdout,
  20. stderr=sys.stderr,
  21. text=True,
  22. bufsize=1,
  23. universal_newlines=True
  24. )
  25. for log_path in key_log_paths:
  26. test_print(f"testing {log_path}")
  27. q_num = 0
  28. with open(log_path, 'r') as f:
  29. for line in f.readlines():
  30. line = line.split(" ")
  31. for key_str in line:
  32. if key_str == "":
  33. continue
  34. key = int(key_str, 16)
  35. if key == ord('q'):
  36. if q_num >= 3:
  37. q_num = 0
  38. test_print("skip qqqq")
  39. continue
  40. q_num += 1
  41. if process.stdin and not process.stdin.closed:
  42. process.stdin.write(chr(key))
  43. process.stdin.flush()
  44. else:
  45. test_print("Warning: stdin is closed, skipping input")
  46. test_print("test done")
  47. process.stdin.write('qqqqq')
  48. process.stdin.flush()
  49. process.wait(timeout=2)
  50. except subprocess.TimeoutExpired:
  51. test_print("Error: Process timeout, force killing...")
  52. process.kill()
  53. process.wait()
  54. except Exception as e:
  55. test_print(f"Error: Failed to process testcases: {e}")
  56. if process and process.poll() is None:
  57. process.kill()
  58. process.wait()
  59. exit(1)
  60. finally:
  61. if process:
  62. try:
  63. if process.poll() is None:
  64. process.terminate()
  65. try:
  66. process.wait(timeout=2)
  67. except subprocess.TimeoutExpired:
  68. process.kill()
  69. process.wait()
  70. except:
  71. pass
  72. return
  73. # Usage example
  74. if __name__ == "__main__":
  75. parser = argparse.ArgumentParser(description='Process test cases for nr_shell')
  76. parser.add_argument('testcases', nargs='?', default='./testcases',
  77. help='Path to test cases directory (default: ./testcases)')
  78. parser.add_argument('bin', nargs='?', default='../examples/simulator/out/nr_shell_db',
  79. help='Path to binary executable (default: ../examples/simulator/out/nr_shell_db)')
  80. args = parser.parse_args()
  81. process_testcases(args.testcases, args.bin)