TestAnnounce.py 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import time
  2. import copy
  3. import gevent
  4. import pytest
  5. import mock
  6. from AnnounceLocal import AnnounceLocalPlugin
  7. from File import FileServer
  8. from Test import Spy
  9. @pytest.fixture
  10. def announcer(file_server, site):
  11. file_server.sites[site.address] = site
  12. announcer = AnnounceLocalPlugin.LocalAnnouncer(file_server, listen_port=1100)
  13. file_server.local_announcer = announcer
  14. announcer.listen_port = 1100
  15. announcer.sender_info["broadcast_port"] = 1100
  16. announcer.getMyIps = mock.MagicMock(return_value=["127.0.0.1"])
  17. announcer.discover = mock.MagicMock(return_value=False) # Don't send discover requests automatically
  18. gevent.spawn(announcer.start)
  19. time.sleep(0.5)
  20. assert file_server.local_announcer.running
  21. return file_server.local_announcer
  22. @pytest.fixture
  23. def announcer_remote(request, site_temp):
  24. file_server_remote = FileServer("127.0.0.1", 1545)
  25. file_server_remote.sites[site_temp.address] = site_temp
  26. announcer = AnnounceLocalPlugin.LocalAnnouncer(file_server_remote, listen_port=1101)
  27. file_server_remote.local_announcer = announcer
  28. announcer.listen_port = 1101
  29. announcer.sender_info["broadcast_port"] = 1101
  30. announcer.getMyIps = mock.MagicMock(return_value=["127.0.0.1"])
  31. announcer.discover = mock.MagicMock(return_value=False) # Don't send discover requests automatically
  32. gevent.spawn(announcer.start)
  33. time.sleep(0.5)
  34. assert file_server_remote.local_announcer.running
  35. def cleanup():
  36. file_server_remote.stop()
  37. request.addfinalizer(cleanup)
  38. return file_server_remote.local_announcer
  39. @pytest.mark.usefixtures("resetSettings")
  40. @pytest.mark.usefixtures("resetTempSettings")
  41. class TestAnnounce:
  42. def testSenderInfo(self, announcer):
  43. sender_info = announcer.sender_info
  44. assert sender_info["port"] > 0
  45. assert len(sender_info["peer_id"]) == 20
  46. assert sender_info["rev"] > 0
  47. def testIgnoreSelfMessages(self, announcer):
  48. # No response to messages that has same peer_id as server
  49. assert not announcer.handleMessage(("0.0.0.0", 123), {"cmd": "discoverRequest", "sender": announcer.sender_info, "params": {}})[1]
  50. # Response to messages with different peer id
  51. sender_info = copy.copy(announcer.sender_info)
  52. sender_info["peer_id"] += "-"
  53. addr, res = announcer.handleMessage(("0.0.0.0", 123), {"cmd": "discoverRequest", "sender": sender_info, "params": {}})
  54. assert res["params"]["sites_changed"] > 0
  55. def testDiscoverRequest(self, announcer, announcer_remote):
  56. assert len(announcer_remote.known_peers) == 0
  57. with Spy.Spy(announcer_remote, "handleMessage") as responses:
  58. announcer_remote.broadcast({"cmd": "discoverRequest", "params": {}}, port=announcer.listen_port)
  59. time.sleep(0.1)
  60. response_cmds = [response[1]["cmd"] for response in responses]
  61. assert response_cmds == ["discoverResponse", "siteListResponse"]
  62. assert len(responses[-1][1]["params"]["sites"]) == 1
  63. # It should only request siteList if sites_changed value is different from last response
  64. with Spy.Spy(announcer_remote, "handleMessage") as responses:
  65. announcer_remote.broadcast({"cmd": "discoverRequest", "params": {}}, port=announcer.listen_port)
  66. time.sleep(0.1)
  67. response_cmds = [response[1]["cmd"] for response in responses]
  68. assert response_cmds == ["discoverResponse"]
  69. def testPeerDiscover(self, announcer, announcer_remote, site):
  70. assert announcer.server.peer_id != announcer_remote.server.peer_id
  71. assert len(announcer.server.sites.values()[0].peers) == 0
  72. announcer.broadcast({"cmd": "discoverRequest"}, port=announcer_remote.listen_port)
  73. time.sleep(0.1)
  74. assert len(announcer.server.sites.values()[0].peers) == 1
  75. def testRecentPeerList(self, announcer, announcer_remote, site):
  76. assert len(site.peers_recent) == 0
  77. assert len(site.peers) == 0
  78. with Spy.Spy(announcer, "handleMessage") as responses:
  79. announcer.broadcast({"cmd": "discoverRequest", "params": {}}, port=announcer_remote.listen_port)
  80. time.sleep(0.1)
  81. assert [response[1]["cmd"] for response in responses] == ["discoverResponse", "siteListResponse"]
  82. assert len(site.peers_recent) == 1
  83. assert len(site.peers) == 1
  84. # It should update peer without siteListResponse
  85. last_time_found = site.peers.values()[0].time_found
  86. site.peers_recent.clear()
  87. with Spy.Spy(announcer, "handleMessage") as responses:
  88. announcer.broadcast({"cmd": "discoverRequest", "params": {}}, port=announcer_remote.listen_port)
  89. time.sleep(0.1)
  90. assert [response[1]["cmd"] for response in responses] == ["discoverResponse"]
  91. assert len(site.peers_recent) == 1
  92. assert site.peers.values()[0].time_found > last_time_found