-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_twispread.py
53 lines (37 loc) · 1.31 KB
/
test_twispread.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
import sys
import random
import unittest
from twisted.internet import reactor, protocol
import twispread
import spread
class Storer(protocol.AbstractDatagramProtocol):
def __init__(self):
self.store = []
def datagramReceived(self, msg, addr=None):
self.store.append(msg)
class Simple(unittest.TestCase):
def setUp(self):
self.l = []
self.group = 'test-' + ''.join([chr(random.randint(0,255)) for x in range(4)]).encode('hex')
for i in range(3):
e = Storer()
p = reactor.listenWith(twispread.Port, protocol=e)
p.join_group(self.group)
self.l.append(e)
def testIt(self):
p = self.l[0]
self.words = ['foo', 'bar', 'bang']
for i in xrange(6):
reactor.iterate(0.01)
for w in self.words:
p.transport.write(w, self.group)
for i in xrange(10):
reactor.doIteration(0.01)
for p in self.l:
p.store = [msg for msg in p.store if type(msg)== spread.RegularMsgType]
self.failUnlessEqual(len(p.store), len(self.words))
for i in xrange(len(self.words)):
for p in self.l:
self.failUnlessEqual(p.store[i].message, self.words[i])
if __name__ =="__main__":
unittest.main()