forked from aosabook/500lines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmember_single.py
58 lines (41 loc) · 1.38 KB
/
member_single.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
import logging
from network import Node
from statemachine import sequence_generator
# Remove from final copy:
# - logging
class Member(Node):
def __init__(self):
Node.__init__(self)
def start(self, execute_fn, initial_value=None):
self.execute_fn = execute_fn
self.state = initial_value
self.run()
def invoke(self, input):
self.state, output = self.execute_fn(self.state, input)
return output
def do_INVOKE(self, input, cid, caller):
self.send([caller], 'INVOKED', output=self.invoke(input))
if __name__ == "__main__":
logging.basicConfig(
format="%(asctime)s %(name)s %(message)s", level=logging.DEBUG)
member = Member()
print member.address
member.start(sequence_generator, initial_value=0)
# tests
import unittest
import threading
class FakeClient(Node):
def do_INVOKED(self, output):
self.output = output
self.stop()
class MemberTests(unittest.TestCase):
def test_invoke(self):
member = Member()
client = FakeClient()
client.member = member
memberthd = threading.Thread(target=member.start, args=(sequence_generator, 0,))
memberthd.daemon = 1
memberthd.start()
client.send([member.address], 'INVOKE', input=5, caller=client.address)
client.run()
self.assertEqual(client.output, [0, 1, 2, 3, 4])