test_dht_experts.py 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. import random
  2. import uuid
  3. from itertools import chain
  4. import hivemind
  5. from hivemind import LOCALHOST
  6. def test_store_get_experts():
  7. peers = [hivemind.DHT(start=True)]
  8. for i in range(10):
  9. neighbors_i = [f'{LOCALHOST}:{node.port}' for node in random.sample(peers, min(3, len(peers)))]
  10. peers.append(hivemind.DHT(initial_peers=neighbors_i, start=True))
  11. you: hivemind.dht.DHT = random.choice(peers)
  12. theguyshetoldyounottoworryabout: hivemind.dht.DHT = random.choice(peers)
  13. expert_uids = [str(uuid.uuid4()) for _ in range(110)]
  14. batch_size = 10
  15. for batch_start in range(0, len(expert_uids), batch_size):
  16. you.declare_experts(expert_uids[batch_start: batch_start + batch_size], 'localhost', 1234)
  17. found = theguyshetoldyounottoworryabout.get_experts(random.sample(expert_uids, 5) + ['foo', 'bar'])
  18. assert all(res is not None for res in found[:-2]), "Could not find some existing experts"
  19. assert all(res is None for res in found[-2:]), "Found non-existing experts"
  20. that_guys_expert, that_guys_port = str(uuid.uuid4()), random.randint(1000, 9999)
  21. theguyshetoldyounottoworryabout.declare_experts([that_guys_expert], f'that_host:{that_guys_port}')
  22. you_notfound, you_found = you.get_experts(['foobar', that_guys_expert])
  23. assert isinstance(you_found, hivemind.RemoteExpert)
  24. assert you_found.endpoint == f'that_host:{that_guys_port}'
  25. # test first_k_active
  26. assert list(theguyshetoldyounottoworryabout.first_k_active(expert_uids, k=10)) == expert_uids[:10]
  27. some_permuted_experts = random.sample(expert_uids, k=32)
  28. assert list(theguyshetoldyounottoworryabout.first_k_active(some_permuted_experts, k=32)) == some_permuted_experts
  29. assert list(theguyshetoldyounottoworryabout.first_k_active(some_permuted_experts, k=1)) == some_permuted_experts[:1]
  30. fake_and_real_experts = list(chain(*zip(
  31. [str(uuid.uuid4()) for _ in some_permuted_experts], some_permuted_experts)))
  32. assert list(theguyshetoldyounottoworryabout.first_k_active(fake_and_real_experts, k=9)) == some_permuted_experts[:9]
  33. for peer in peers:
  34. peer.shutdown()
  35. def test_first_k_active():
  36. node = hivemind.DHT(start=True)
  37. assert all(node.declare_experts(['e.1.2.3', 'e.1.2.4', 'e.3.4.5'], endpoint=f"{hivemind.LOCALHOST}:1337"))
  38. assert all(node.declare_experts(['e.2.1.1'], endpoint=f"{hivemind.LOCALHOST}:1338"))
  39. results = node.first_k_active(['e.0', 'e.1', 'e.2', 'e.3'], k=2)
  40. assert len(results) == 2 and next(iter(results.keys())) == 'e.1'
  41. assert results['e.1'].uid in ('e.1.2.3', 'e.1.2.4') and results['e.1'].endpoint == f"{hivemind.LOCALHOST}:1337"
  42. assert results['e.2'].uid == 'e.2.1.1' and results['e.2'].endpoint == f"{hivemind.LOCALHOST}:1338"
  43. results = node.first_k_active(['e', 'e.1', 'e.1.2', 'e.1.2.3'], k=10)
  44. assert len(results) == 4
  45. assert 'e' in results
  46. for k in ('e.1', 'e.1.2', 'e.1.2.3'):
  47. assert results[k].uid in ('e.1.2.3', 'e.1.2.4') and results[k].endpoint == f"{hivemind.LOCALHOST}:1337"
  48. def test_dht_single_node():
  49. node = hivemind.DHT(start=True)
  50. assert node.first_k_active(['e3', 'e2'], k=3) == {}
  51. assert node.get_experts(['e3', 'e2']) == [None, None]
  52. assert all(node.declare_experts(['e1', 'e2', 'e3'], f"{hivemind.LOCALHOST}:1337"))
  53. for expert in node.get_experts(['e3', 'e2']):
  54. assert expert.endpoint == f"{hivemind.LOCALHOST}:1337"
  55. active_found = node.first_k_active(['e0', 'e1', 'e3', 'e5', 'e2'], k=2)
  56. assert list(active_found.keys()) == ['e1', 'e3']
  57. assert all(expert.uid.startswith(prefix) for prefix, expert in active_found.items())
  58. assert all(node.declare_experts(['e1', 'e2', 'e3'], f"{hivemind.LOCALHOST}:1337"))