Skip to content

Commit

Permalink
Add comments to explain the Node tests.
Browse files Browse the repository at this point in the history
Signed-off-by: Daira Emma Hopwood <[email protected]>
  • Loading branch information
daira committed Oct 20, 2023
1 parent 92c8082 commit 3d4a647
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions simtfl/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,32 @@ def run(self):
class PassiveReceiverTestNode(PassiveNode):
def __init__(self):
super().__init__()
self.received = deque()
self.handled = deque()

def handle(self, sender, message):
self.received.append((sender, message, self.env.now))
# Record when each message is handled.
self.handled.append((sender, message, self.env.now))
# The handler takes 3 time units.
yield self.env.timeout(3)


class SequentialReceiverTestNode(SequentialNode):
def __init__(self):
super().__init__()
self.received = deque()
self.handled = deque()

def handle(self, sender, message):
self.received.append((sender, message, self.env.now))
# Record when each message is handled.
self.handled.append((sender, message, self.env.now))
# The handler takes 3 time units.
yield self.env.timeout(3)


class SenderTestNode(PassiveNode):
def run(self):
# We send messages at times 0, 1, 2. Since the network
# propagation delay is 1 (the default), they will be
# received at times 1, 2, 3.
for i in range(3):
yield from self.send(0, PayloadMessage(i))
yield self.env.timeout(1)
Expand All @@ -159,16 +166,23 @@ def _test_node(self, receiver_node, expected):
network.add_node(SenderTestNode())
network.run_all()

self.assertEqual(list(network.node(0).received), expected)
self.assertEqual(list(network.node(0).handled), expected)

def test_passive_node(self):
# A PassiveNode subclass does not block on handling of
# previous messages, so it handles each message immediately
# when it is received.
self._test_node(PassiveReceiverTestNode(), [
(1, PayloadMessage(0), 1),
(1, PayloadMessage(1), 2),
(1, PayloadMessage(2), 3),
])

def test_sequential_node(self):
# A SequentialNode subclass *does* block on handling of
# previous messages. It handles the messages as soon as
# possible after they are received subject to that blocking,
# so they will be handled at intervals of 3 time units.
self._test_node(SequentialReceiverTestNode(), [
(1, PayloadMessage(0), 1),
(1, PayloadMessage(1), 4),
Expand Down

0 comments on commit 3d4a647

Please sign in to comment.