2019-01-22 18:49:43 +01:00
|
|
|
import asyncio
|
|
|
|
import unittest
|
2018-11-07 21:15:05 +01:00
|
|
|
from lbrynet.utils import generate_id
|
2019-01-22 18:49:43 +01:00
|
|
|
from lbrynet.dht.peer import PeerManager
|
|
|
|
from torba.testcase import AsyncioTestCase
|
2016-10-19 23:21:24 +02:00
|
|
|
|
|
|
|
|
2019-01-22 18:49:43 +01:00
|
|
|
class PeerTest(AsyncioTestCase):
|
2016-10-19 23:21:24 +02:00
|
|
|
def setUp(self):
|
2019-01-22 18:49:43 +01:00
|
|
|
self.loop = asyncio.get_event_loop()
|
|
|
|
self.peer_manager = PeerManager(self.loop)
|
2018-05-24 00:28:22 +02:00
|
|
|
self.node_ids = [generate_id(), generate_id(), generate_id()]
|
2019-01-22 18:49:43 +01:00
|
|
|
self.first_contact = self.peer_manager.get_kademlia_peer(self.node_ids[1], '127.0.0.1', udp_port=1000)
|
|
|
|
self.second_contact = self.peer_manager.get_kademlia_peer(self.node_ids[0], '192.168.0.1', udp_port=1000)
|
2018-05-24 00:28:22 +02:00
|
|
|
|
2018-07-31 19:20:25 +02:00
|
|
|
def test_make_contact_error_cases(self):
|
2019-01-22 18:49:43 +01:00
|
|
|
self.assertRaises(ValueError, self.peer_manager.get_kademlia_peer, self.node_ids[1], '192.168.1.20', 100000)
|
|
|
|
self.assertRaises(ValueError, self.peer_manager.get_kademlia_peer, self.node_ids[1], '192.168.1.20.1', 1000)
|
|
|
|
self.assertRaises(ValueError, self.peer_manager.get_kademlia_peer, self.node_ids[1], 'this is not an ip', 1000)
|
|
|
|
self.assertRaises(ValueError, self.peer_manager.get_kademlia_peer, self.node_ids[1], '192.168.1.20', -1000)
|
|
|
|
self.assertRaises(ValueError, self.peer_manager.get_kademlia_peer, b'not valid node id', '192.168.1.20', 1000)
|
2018-07-31 19:20:25 +02:00
|
|
|
|
|
|
|
def test_boolean(self):
|
2019-01-22 18:49:43 +01:00
|
|
|
self.assertNotEqual(self.first_contact, self.second_contact)
|
|
|
|
self.assertEqual(
|
|
|
|
self.second_contact, self.peer_manager.get_kademlia_peer(self.node_ids[0], '192.168.0.1', udp_port=1000)
|
2018-08-22 06:12:46 +02:00
|
|
|
)
|
2016-10-19 23:21:24 +02:00
|
|
|
|
2018-07-31 19:20:25 +02:00
|
|
|
def test_compact_ip(self):
|
|
|
|
self.assertEqual(self.first_contact.compact_ip(), b'\x7f\x00\x00\x01')
|
|
|
|
self.assertEqual(self.second_contact.compact_ip(), b'\xc0\xa8\x00\x01')
|
|
|
|
|
2018-05-24 00:31:31 +02:00
|
|
|
|
2019-01-22 18:49:43 +01:00
|
|
|
@unittest.SkipTest
|
2018-05-24 00:31:31 +02:00
|
|
|
class TestContactLastReplied(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.clock = task.Clock()
|
|
|
|
self.contact_manager = ContactManager(self.clock.seconds)
|
|
|
|
self.contact = self.contact_manager.make_contact(generate_id(), "127.0.0.1", 4444, None)
|
|
|
|
self.clock.advance(3600)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(self.contact.contact_is_good)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_stale_replied_to_us(self):
|
|
|
|
self.contact.update_last_replied()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_stale_requested_from_us(self):
|
|
|
|
self.contact.update_last_requested()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(self.contact.contact_is_good)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_stale_then_fail(self):
|
|
|
|
self.contact.update_last_failed()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(self.contact.contact_is_good)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
self.contact.update_last_failed()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_good_turned_stale(self):
|
|
|
|
self.contact.update_last_replied()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-29 22:22:30 +02:00
|
|
|
self.clock.advance(constants.checkRefreshInterval - 1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(self.contact.contact_is_good)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_good_then_fail(self):
|
|
|
|
self.contact.update_last_replied()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
self.contact.update_last_failed()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(59)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.contact.update_last_failed()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|
2018-05-24 00:31:31 +02:00
|
|
|
for _ in range(7200):
|
|
|
|
self.clock.advance(60)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_good_then_fail_then_good(self):
|
|
|
|
# it replies
|
|
|
|
self.contact.update_last_replied()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
|
|
|
|
# it fails twice in a row
|
|
|
|
self.contact.update_last_failed()
|
|
|
|
self.clock.advance(1)
|
|
|
|
self.contact.update_last_failed()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
|
|
|
|
# it replies
|
|
|
|
self.contact.update_last_replied()
|
|
|
|
self.clock.advance(1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
# it goes stale
|
2018-05-29 22:22:30 +02:00
|
|
|
self.clock.advance(constants.checkRefreshInterval - 2)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(self.contact.contact_is_good)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
|
2019-01-22 18:49:43 +01:00
|
|
|
@unittest.SkipTest
|
2018-05-24 00:31:31 +02:00
|
|
|
class TestContactLastRequested(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.clock = task.Clock()
|
|
|
|
self.contact_manager = ContactManager(self.clock.seconds)
|
|
|
|
self.contact = self.contact_manager.make_contact(generate_id(), "127.0.0.1", 4444, None)
|
|
|
|
self.clock.advance(1)
|
|
|
|
self.contact.update_last_replied()
|
|
|
|
self.clock.advance(3600)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(self.contact.contact_is_good)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_previous_replied_then_requested(self):
|
|
|
|
# it requests
|
|
|
|
self.contact.update_last_requested()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
# it goes stale
|
2018-05-29 22:22:30 +02:00
|
|
|
self.clock.advance(constants.checkRefreshInterval - 1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIsNone(self.contact.contact_is_good)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
def test_previous_replied_then_requested_then_failed(self):
|
|
|
|
# it requests
|
|
|
|
self.contact.update_last_requested()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, True)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
|
|
|
|
# it fails twice in a row
|
|
|
|
self.contact.update_last_failed()
|
|
|
|
self.clock.advance(1)
|
|
|
|
self.contact.update_last_failed()
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
|
|
|
|
|
|
|
# it requests
|
|
|
|
self.contact.update_last_requested()
|
|
|
|
self.clock.advance(1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|
2018-05-24 00:31:31 +02:00
|
|
|
|
|
|
|
# it goes stale
|
|
|
|
self.clock.advance((constants.refreshTimeout / 4) - 2)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|
2018-05-24 00:31:31 +02:00
|
|
|
self.clock.advance(1)
|
2018-10-18 13:41:33 +02:00
|
|
|
self.assertIs(self.contact.contact_is_good, False)
|