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} @author: Brian Muller U{bamuller@gmail.com}
""" """
version_info = (0, 0) version_info = (0, 1)
version = '.'.join(map(str, version_info)) version = '.'.join(map(str, version_info))

View File

@ -180,10 +180,12 @@ class Server(object):
""" """
self.log.debug("setting '%s' = '%s' on network" % (key, value)) self.log.debug("setting '%s' = '%s' on network" % (key, value))
dkey = digest(key) dkey = digest(key)
def store(nodes): def store(nodes):
self.log.info("setting '%s' on %s" % (key, map(str, nodes))) self.log.info("setting '%s' on %s" % (key, map(str, nodes)))
ds = [self.protocol.callStore(node, dkey, value) for node in nodes] ds = [self.protocol.callStore(node, dkey, value) for node in nodes]
return defer.gatherResults(ds) return defer.gatherResults(ds)
node = Node(None, None, dkey) node = Node(None, None, dkey)
nearest = self.protocol.router.findNeighbors(node) nearest = self.protocol.router.findNeighbors(node)
spider = SpiderCrawl(self.protocol, node, nearest, self.ksize, self.alpha) 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 random
import hashlib import hashlib
from twisted.trial import unittest from twisted.trial import unittest
from kademlia.node import Node, NodeHeap from kademlia.node import Node, NodeHeap
class NodeTest(unittest.TestCase): class NodeTest(unittest.TestCase):
def test_longID(self): def test_longID(self):
rid = hashlib.sha1(str(random.getrandbits(255))).digest() rid = hashlib.sha1(str(random.getrandbits(255))).digest()
n = Node(None, None, rid) n = Node(None, None, rid)
self.assertEqual(n.long_id, long(rid.encode('hex'), 16)) 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) s = str(s)
return hashlib.sha1(s).digest() return hashlib.sha1(s).digest()
def deferredDict(d): def deferredDict(d):
""" """
Just like a C{defer.DeferredList} but instead accepts and returns a C{dict}. Just like a C{defer.DeferredList} but instead accepts and returns a C{dict}.