File size: 4,143 Bytes
b585c7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import unittest
import time

from iterators import TimeoutIterator


def iter_simple():
    yield 1
    yield 2


def iter_with_sleep():
    yield 1
    time.sleep(0.6)
    yield 2
    time.sleep(0.4)
    yield 3


def iter_with_exception():
    yield 1
    yield 2
    raise Exception
    yield 3


class TestTimeoutIterator(unittest.TestCase):

    def test_normal_iteration(self):
        i = iter_simple()
        it = TimeoutIterator(i)

        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), 2)

        self.assertRaises(StopIteration, next, it)
        self.assertRaises(StopIteration, next, it)

    def test_normal_iteration_for_loop(self):
        i = iter_simple()
        it = TimeoutIterator(i)
        iterResults = []
        for x in it:
            iterResults.append(x)
        self.assertEqual(iterResults, [1, 2])

    def test_timeout_block(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i)
        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), 2)
        self.assertEqual(next(it), 3)
        self.assertRaises(StopIteration, next, it)
        self.assertRaises(StopIteration, next, it)

    def test_timeout_block_for_loop(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i)
        iterResults = []
        for x in it:
            iterResults.append(x)
        self.assertEqual(iterResults, [1, 2, 3])

    def test_fixed_timeout(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i, timeout=0.5)
        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), it.get_sentinel())

        self.assertEqual(next(it), 2)
        self.assertEqual(next(it), 3)
        self.assertRaises(StopIteration, next, it)

    def test_fixed_timeout_for_loop(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i, timeout=0.5)
        iterResults = []
        for x in it:
            iterResults.append(x)
        self.assertEqual(iterResults, [1, it.get_sentinel(), 2, 3])

    def test_timeout_update(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i, timeout=0.5)
        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), it.get_sentinel())

        it.set_timeout(0.3)
        self.assertEqual(next(it), 2)
        self.assertEqual(next(it), it.get_sentinel())

        self.assertEqual(next(it), 3)
        self.assertRaises(StopIteration, next, it)

    def test_custom_sentinel(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i, timeout=0.5, sentinel="END")
        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), "END")

        self.assertEqual(next(it), 2)
        self.assertEqual(next(it), 3)
        self.assertRaises(StopIteration, next, it)

    def test_feature_timeout_reset(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i, timeout=0.5, reset_on_next=True)
        self.assertEqual(next(it), 1)  # timeout gets reset after first iteration
        self.assertEqual(next(it), 2)
        self.assertEqual(next(it), 3)
        self.assertRaises(StopIteration, next, it)

    def test_function_set_reset_on_next(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i, timeout=0.35, reset_on_next=False)
        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), it.get_sentinel())
        it.set_reset_on_next(True)
        self.assertEqual(next(it), 2)
        self.assertEqual(next(it), 3)
        self.assertRaises(StopIteration, next, it)

    def test_iterator_raises_exception(self):
        i = iter_with_exception()
        it = TimeoutIterator(i, timeout=0.5, sentinel="END")
        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), 2)
        self.assertRaises(Exception, next, it)
        self.assertRaises(StopIteration, next, it)

    def test_interrupt_thread(self):
        i = iter_with_sleep()
        it = TimeoutIterator(i, timeout=0.5, sentinel="END")
        self.assertEqual(next(it), 1)
        self.assertEqual(next(it), it.get_sentinel())
        it.interrupt()
        self.assertEqual(next(it), 2)
        self.assertRaises(StopIteration, next, it)