summaryrefslogtreecommitdiff
path: root/tests/network_packetTest.py
blob: c5bb7095fb820aab8e498999eae6d435aca4888c (plain)
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
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()