-
Notifications
You must be signed in to change notification settings - Fork 2
/
TristeroTest.py
executable file
·114 lines (91 loc) · 3.99 KB
/
TristeroTest.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#!/usr/bin/env python
# Copyright (c) 2002 Bryce "Zooko" Wilcox-O'Hearn
# portions Copyright (c) 2001 Autonomous Zone Industries
# This file is licensed under the
# GNU Lesser General Public License v2.1.
# See the file COPYING or visit http://www.gnu.org/ for details.
#
__cvsid = '$Id: TristeroTest.py,v 1.3 2002/04/21 16:25:36 zooko Exp $'
# standard Python modules
import threading, types
# pyutil modules
import DoQ
import config
from debugprint import debugprint
from humanreadable import hr
from timeutil import timer
# libbase32 modules
# from humread import hr # XXX for when we switch to base32 encoding...
# (old) MN modules
from confutils import confman
import idlib
from interfaces import *
# EGTP modules
import CommStrat
import Node
from TristeroLookup import TristeroLookup
import NodeMappingVerifier
true = 1
false = 0
confman['MAX_VERBOSITY'] = 0
config.MAX_VERBOSITY = 0
# a discovery man which uses only local data; In a real app you need distributed discovery in the form of MetaTrackers, Tristero, Plex, Alpine, or something.
class LocalDiscoveryMan(IDiscoveryManager):
def __init__(self):
self.data = {}
def discover(self, key, discoveryhand):
discoveryhand.result(self.data.get(key))
return # `discover()' never returns any return value!
def _help_test(finishedflag, numsuccessesh, lm, dm, name="a test"):
"""
@param lm an object that satisfies the ILookupMan interface
@param dm an object that satisfies the IDiscoveryMan interface
"""
print '_help_test'
start = timer.time()
print 'Making node'
# Make a listener. He will announce his EGTP address to the lookupman `lm'.
n1 = Node.Node(allownonrouteableip=true, lookupman=lm, discoveryman=dm)
print 'Node 1:', n1
# Set a handler func: if any messages come in with message type "ping", the EGTP Node will call this function.
def l_ping_handler(sender, msg, finishedflag=finishedflag, numsuccessesh=numsuccessesh, start=start, name=name):
debugprint("%s(): passed in %s seconds: Got a message from %s. The message says: %s\n", args=(name, "%0.1f" % (timer.time() - start), sender, msg,), v=0)
numsuccessesh[0] += 1
finishedflag.set()
n1.set_handler_func(mtype="ping", handler_func=l_ping_handler)
print 'Set Handler'
# Make a sender. He'll keep a reference to `lm' for later use.
n2 = Node.Node(allownonrouteableip=true, lookupman=lm, discoveryman=dm)
print 'Node 2:', n2
# Have the second node ping the first, using only the first's id.
n2.send(CommStrat.addr_to_id(n1.get_address()), mtype="ping", msg="hello there, you crazy listener!")
def test_1(finishedflag, numsuccessesh):
verifier = NodeMappingVerifier.NodeMappingVerifier()
localLM = TristeroLookup(verifier, "http://numbers:4035")
print 'TristeroLookup Service:', localLM
localDM = LocalDiscoveryMan()
_help_test(finishedflag, numsuccessesh, localLM, localDM, name="test_1")
def runalltests(tests, expectedfailures=0):
if expectedfailures > 0:
print "WARNING: this module is currently failing some of the unit tests. Number of expected failures: %s" % expectedfailures
# Create the event queue for this process:
DoQ.doq = DoQ.DoQ()
# Call `init()'.
Node.init()
numsuccessesh = [0]
ts = []
for test in tests:
finishedflag = threading.Event()
ts.append((test, finishedflag,))
DoQ.doq.add_task(test, args=(finishedflag, numsuccessesh,))
timeout = 60
for (test, finishedflag,) in ts:
tstart = timer.time()
while not finishedflag.isSet():
if (timer.time() - tstart) < timeout:
finishedflag.wait(1)
else:
print "test %s didn't finish within %s seconds" % (test, timeout,)
break
assert numsuccessesh[0] == len(tests), "not all tests passed: num successes: %s, num failures: %s" % (numsuccessesh[0], map(lambda x: x[0], filter(lambda x: not x[1].isSet(), ts)),)
runalltests((test_1,), expectedfailures=0)