added more tests

This commit is contained in:
Brian Muller 2014-01-03 17:58:09 -05:00
parent f7e8bd9442
commit 2a4802f56f
5 changed files with 26 additions and 3 deletions

View File

@ -3,5 +3,5 @@ Kademlia is a Python implementation of the Kademlia protocol for U{Twisted <http
@author: Brian Muller U{bamuller@gmail.com}
"""
version_info = (0, 0)
version_info = (0, 1)
version = '.'.join(map(str, version_info))

View File

@ -33,7 +33,7 @@ class SpiderCrawl(object):
self.nearest = NodeHeap(self.ksize)
self.node = node
self.lastIDsCrawled = []
self.log = Logger(system=self)
self.log = Logger(system=self)
self.log.info("creating spider with peers: %s" % peers)
for peer in peers:
self.nearest.push(self.node.distanceTo(peer), peer)
@ -179,11 +179,13 @@ class Server(object):
TODO - if no one responds, freak out
"""
self.log.debug("setting '%s' = '%s' on network" % (key, value))
dkey = digest(key)
dkey = digest(key)
def store(nodes):
self.log.info("setting '%s' on %s" % (key, map(str, nodes)))
ds = [self.protocol.callStore(node, dkey, value) for node in nodes]
return defer.gatherResults(ds)
node = Node(None, None, dkey)
nearest = self.protocol.router.findNeighbors(node)
spider = SpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha)

View File

@ -0,0 +1,3 @@
"""
Tests live here.
"""

View File

@ -1,11 +1,28 @@
import random
import hashlib
from twisted.trial import unittest
from kademlia.node import Node, NodeHeap
class NodeTest(unittest.TestCase):
def test_longID(self):
rid = hashlib.sha1(str(random.getrandbits(255))).digest()
n = Node(None, None, rid)
self.assertEqual(n.long_id, long(rid.encode('hex'), 16))
def test_distanceCalculation(self):
ridone = hashlib.sha1(str(random.getrandbits(255)))
ridtwo = hashlib.sha1(str(random.getrandbits(255)))
shouldbe = long(ridone.hexdigest(), 16) ^ long(ridtwo.hexdigest(), 16)
none = Node(None, None, ridone.digest())
ntwo = Node(None, None, ridtwo.digest())
self.assertEqual(none.distanceTo(ntwo), shouldbe)
class NodeHeapTest(unittest.TestCase):
def test_maxSize(self):
n = NodeHeap(3)
self.assertEqual(0, len(n))

View File

@ -10,6 +10,7 @@ def digest(s):
s = str(s)
return hashlib.sha1(s).digest()
def deferredDict(d):
"""
Just like a C{defer.DeferredList} but instead accepts and returns a C{dict}.