test_cli_scripts.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. import re
  2. from subprocess import PIPE, Popen
  3. from time import sleep
  4. DHT_START_PATTERN = re.compile(r"Running a DHT instance. To connect other peers to this one, use (.+)$")
  5. def test_dht_connection_successful():
  6. dht_refresh_period = 1
  7. dht_proc = Popen(
  8. ["hivemind-dht", "--host_maddrs", "/ip4/127.0.0.1/tcp/0", "--refresh_period", str(dht_refresh_period)],
  9. stderr=PIPE,
  10. text=True,
  11. encoding="utf-8",
  12. )
  13. first_line = dht_proc.stderr.readline()
  14. second_line = dht_proc.stderr.readline()
  15. dht_pattern_match = DHT_START_PATTERN.search(first_line)
  16. assert dht_pattern_match is not None, first_line
  17. assert "Full list of visible multiaddresses:" in second_line, second_line
  18. initial_peers = dht_pattern_match.group(1).split(" ")
  19. dht_client_proc = Popen(
  20. ["hivemind-dht", *initial_peers, "--host_maddrs", "/ip4/127.0.0.1/tcp/0"],
  21. stderr=PIPE,
  22. text=True,
  23. encoding="utf-8",
  24. )
  25. # skip first two lines with connectivity info
  26. for _ in range(2):
  27. dht_client_proc.stderr.readline()
  28. first_report_msg = dht_client_proc.stderr.readline()
  29. assert "2 DHT nodes (including this one) are in the local routing table" in first_report_msg
  30. # ensure we get the output of dht_proc after the start of dht_client_proc
  31. sleep(dht_refresh_period)
  32. # expect that one of the next logging outputs from the first peer shows a new connection
  33. for _ in range(5):
  34. first_report_msg = dht_proc.stderr.readline()
  35. second_report_msg = dht_proc.stderr.readline()
  36. if (
  37. "2 DHT nodes (including this one) are in the local routing table" in first_report_msg
  38. and "Local storage contains 0 keys" in second_report_msg
  39. ):
  40. break
  41. else:
  42. assert (
  43. "2 DHT nodes (including this one) are in the local routing table" in first_report_msg
  44. and "Local storage contains 0 keys" in second_report_msg
  45. )
  46. dht_proc.terminate()
  47. dht_client_proc.terminate()
  48. dht_proc.wait()
  49. dht_client_proc.wait()