summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
authorHaidong Ji2019-01-29 16:18:10 -0600
committerHaidong Ji2019-01-29 16:18:10 -0600
commite314d433e66a6177c8a17e3fca5acc59005e1b06 (patch)
tree6e2994a471767a5d756e1184d5286fc8887f4e6a /tests
parentcefc6a2cd763e09adb8b4df33697df4a2cb5ddab (diff)
Network Packet done!
It's not too bad, since I worked things out in Java first. 2 things are of note: 1. Java Deque's pop is Python Deque's popleft. 2. I didn't use an iterator to remove items. It seemed to work fine.
Diffstat (limited to 'tests')
-rw-r--r--tests/network_packetTest.py52
1 files changed, 52 insertions, 0 deletions
diff --git a/tests/network_packetTest.py b/tests/network_packetTest.py
new file mode 100644
index 0000000..c5bb709
--- /dev/null
+++ b/tests/network_packetTest.py
@@ -0,0 +1,52 @@
+import unittest
+
+from collections import namedtuple
+
+Request = namedtuple("Request", ["arrived_at", "time_to_process"])
+Response = namedtuple("Response", ["was_dropped", "started_at"])
+
+import sources.network_packet
+from sources.network_packet import process_requests
+
+
+class MyTestCase(unittest.TestCase):
+ def test(self):
+ buffer_max_size = 1
+ buf = sources.network_packet.Buffer(buffer_max_size)
+ requests = []
+ responses = process_requests(requests, buf)
+ self.assertEqual(0, len(responses))
+
+ def test1(self):
+ buffer_max_size = 1
+ buf = sources.network_packet.Buffer(buffer_max_size)
+ requests = [Request(0, 1)]
+ responses = process_requests(requests, buf)
+ self.assertEqual(1, len(responses))
+ self.assertFalse(responses[0].was_dropped)
+ self.assertEqual(0, responses[0].started_at)
+
+ def test2(self):
+ buffer_max_size = 1
+ buf = sources.network_packet.Buffer(buffer_max_size)
+ requests = [Request(0, 1), Request(0, 1)]
+ responses = process_requests(requests, buf)
+ self.assertEqual(2, len(responses))
+ self.assertFalse(responses[0].was_dropped)
+ self.assertEqual(0, responses[0].started_at)
+ self.assertTrue(responses[1].was_dropped)
+
+ def test3(self):
+ buffer_max_size = 1
+ buf = sources.network_packet.Buffer(buffer_max_size)
+ requests = [Request(0, 1), Request(1, 1)]
+ responses = process_requests(requests, buf)
+ self.assertEqual(2, len(responses))
+ self.assertFalse(responses[0].was_dropped)
+ self.assertEqual(0, responses[0].started_at)
+ self.assertFalse(responses[1].was_dropped)
+ self.assertEqual(1, responses[1].started_at)
+
+
+if __name__ == '__main__':
+ unittest.main()