1
0

test_khashmir.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # The contents of this file are subject to the BitTorrent Open Source License
  2. # Version 1.1 (the License). You may not copy or use this file, in either
  3. # source code or executable form, except in compliance with the License. You
  4. # may obtain a copy of the License at http://www.bittorrent.com/license/.
  5. #
  6. # Software distributed under the License is distributed on an AS IS basis,
  7. # WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  8. # for the specific language governing rights and limitations under the
  9. # License.
  10. from unittest import *
  11. from BitTorrent import RawServer_twisted
  12. from khashmir import *
  13. import khash
  14. from copy import copy
  15. from random import randrange
  16. from krpc import KRPC
  17. KRPC.noisy=0
  18. import os
  19. if __name__ =="__main__":
  20. tests = defaultTestLoader.loadTestsFromNames([sys.argv[0][:-3]])
  21. result = TextTestRunner().run(tests)
  22. class MultiTest(TestCase):
  23. num = 25
  24. def _done(self, val):
  25. self.done = 1
  26. def setUp(self):
  27. self.l = []
  28. self.startport = 10088
  29. d = dict([(x[0],x[1]) for x in common_options + rare_options])
  30. self.r = RawServer(d)
  31. for i in range(self.num):
  32. self.l.append(Khashmir('127.0.0.1', self.startport + i, '/tmp/%s.test' % (self.startport + i), self.r))
  33. self.r.listen_once(1)
  34. self.r.listen_once(1)
  35. for i in self.l:
  36. try:
  37. i.addContact('127.0.0.1', self.l[randrange(0,self.num)].port)
  38. except:
  39. pass
  40. try:
  41. i.addContact('127.0.0.1', self.l[randrange(0,self.num)].port)
  42. except:
  43. pass
  44. try:
  45. i.addContact('127.0.0.1', self.l[randrange(0,self.num)].port)
  46. except:
  47. pass
  48. self.r.listen_once(1)
  49. self.r.listen_once(1)
  50. self.r.listen_once(1)
  51. for i in self.l:
  52. self.done = 0
  53. i.findCloseNodes(self._done)
  54. while not self.done:
  55. self.r.listen_once(1)
  56. for i in self.l:
  57. self.done = 0
  58. i.findCloseNodes(self._done)
  59. while not self.done:
  60. self.r.listen_once(1)
  61. def tearDown(self):
  62. for i in self.l:
  63. self.r.stop_listening_udp(i.socket)
  64. i.socket.close()
  65. self.r.listen_once(1)
  66. def testStoreRetrieve(self):
  67. for i in range(10):
  68. K = khash.newID()
  69. V = khash.newID()
  70. for a in range(3):
  71. self.done = 0
  72. def _scb(val):
  73. self.done = 1
  74. self.l[randrange(0, self.num)].storeValueForKey(K, V, _scb)
  75. while not self.done:
  76. self.r.listen_once(1)
  77. def _rcb(val):
  78. if not val:
  79. self.done = 1
  80. self.assertEqual(self.got, 1)
  81. elif V in val:
  82. self.got = 1
  83. for x in range(3):
  84. self.got = 0
  85. self.done = 0
  86. self.l[randrange(0, self.num)].valueForKey(K, _rcb)
  87. while not self.done:
  88. self.r.listen_once(1)
  89. class AASimpleTests(TestCase):
  90. def setUp(self):
  91. d = dict([(x[0],x[1]) for x in common_options + rare_options])
  92. self.r = RawServer(d)
  93. self.a = Khashmir('127.0.0.1', 4044, '/tmp/a.test', self.r)
  94. self.b = Khashmir('127.0.0.1', 4045, '/tmp/b.test', self.r)
  95. def tearDown(self):
  96. self.r.stop_listening_udp(self.a.socket)
  97. self.r.stop_listening_udp(self.b.socket)
  98. self.a.socket.close()
  99. self.b.socket.close()
  100. def addContacts(self):
  101. self.a.addContact('127.0.0.1', 4045)
  102. self.r.listen_once(1)
  103. self.r.listen_once(1)
  104. def testStoreRetrieve(self):
  105. self.addContacts()
  106. self.got = 0
  107. self.a.storeValueForKey(sha('foo').digest(), 'foobar')
  108. self.r.listen_once(1)
  109. self.r.listen_once(1)
  110. self.r.listen_once(1)
  111. self.r.listen_once(1)
  112. self.r.listen_once(1)
  113. self.r.listen_once(1)
  114. self.a.valueForKey(sha('foo').digest(), self._cb)
  115. self.r.listen_once(1)
  116. self.r.listen_once(1)
  117. self.r.listen_once(1)
  118. self.r.listen_once(1)
  119. def _cb(self, val):
  120. if not val:
  121. self.assertEqual(self.got, 1)
  122. elif 'foobar' in val:
  123. self.got = 1
  124. def testAddContact(self):
  125. self.assertEqual(len(self.a.table.buckets), 1)
  126. self.assertEqual(len(self.a.table.buckets[0].l), 0)
  127. self.assertEqual(len(self.b.table.buckets), 1)
  128. self.assertEqual(len(self.b.table.buckets[0].l), 0)
  129. self.addContacts()
  130. self.assertEqual(len(self.a.table.buckets), 1)
  131. self.assertEqual(len(self.a.table.buckets[0].l), 1)
  132. self.assertEqual(len(self.b.table.buckets), 1)
  133. self.assertEqual(len(self.b.table.buckets[0].l), 1)