summaryrefslogtreecommitdiff
path: root/test_futures.py
blob: c0f0539616ec72fe4e69b53159af4f5bafc2af7d (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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
import test.support
from test.support import verbose

import unittest
import threading
import time

import futures.thread as threaded_futures

class Call(object):
    def __init__(self, manual_finish=False):
        self._called_event = threading.Event()

        self._can_finished = threading.Event()
        if not manual_finish:
            self._can_finished.set()

    def wait_on_called(self):
        self._called_event.wait()

    def set_can(self):
        self._can_finished.set()

    def called(self):
        return self._called_event.is_set()

    def __call__(self):
        if self._called_event.is_set(): print('called twice')

        print('Doing call...')
        self._called_event.set()
        self._can_finished.wait()
        print('About to return...')
        return 42

class ExceptionCall(Call):
    def __call__(self):
        assert not self._called_event.is_set(), 'already called'

        print('Doing exception call...')
        self._called_event.set()
        self._can_finished.wait()
        print('About to raise...')
        raise ZeroDivisionError()

class FutureStub(object):
    def __init__(self, cancelled, done, exception=None):
        self._cancelled = cancelled
        self._done = done
        self._exception = exception

    def cancelled(self):
        return self._cancelled

    def done(self):
        return self._done

    def exception(self):
        return self._exception

class ShutdownTest(unittest.TestCase):
    def test_run_after_shutdown(self):
        self.executor = threaded_futures.ThreadPoolExecutor(max_threads=1)

        call1 = Call()
        self.executor.shutdown()
        self.assertRaises(RuntimeError,
                          self.executor.run,
                          [call1])

    def test_threads_terminate(self):
        self.executor = threaded_futures.ThreadPoolExecutor(max_threads=5)

        call1 = Call(manual_finish=True)
        call2 = Call(manual_finish=True)
        call3 = Call(manual_finish=True)

        self.executor.run([call1, call2, call3],
                          run_until=threaded_futures.RETURN_IMMEDIATELY)

        call1.wait_on_called()
        call2.wait_on_called()
        call3.wait_on_called()

        call1.set_can()
        call2.set_can()
        call3.set_can()

        self.assertEqual(len(self.executor._threads), 3)
        self.executor.shutdown()
        for t in self.executor._threads:
            t.join()
            

class ConcurrentWaitsTest(unittest.TestCase):
    def test(self):
        def aaa():
            fs.wait(run_until=threaded_futures.ALL_COMPLETED)
            self.assertTrue(f1.done())
            self.assertTrue(f2.done())
            self.assertTrue(f3.done())
            self.assertTrue(f4.done())

        def bbb():
            fs.wait(run_until=threaded_futures.FIRST_COMPLETED)
            self.assertTrue(f1.done())
            self.assertFalse(f2.done())
            self.assertFalse(f3.done())
            self.assertFalse(f4.done())

        def ccc():
            fs.wait(run_until=threaded_futures.FIRST_EXCEPTION)
            self.assertTrue(f1.done())
            self.assertTrue(f2.done())
            print('fs:', fs)
            print(f1, f2, f3, f4)
            self.assertFalse(f3.done())
            self.assertFalse(f4.done())

        executor = threaded_futures.ThreadPoolExecutor(max_threads=1)

        call1 = Call(manual_finish=True)
        call2 = ExceptionCall(manual_finish=True)
        call3 = Call(manual_finish=True)
        call4 = Call()

        fs = executor.run([call1, call2, call3, call4],
                          run_until=threaded_futures.RETURN_IMMEDIATELY)
        f1, f2, f3, f4 = fs

        threads = []
        for call in [aaa, bbb, ccc] * 3:
            t = threading.Thread(target=call)
            t.start()
            threads.append(t)

        time.sleep(1)
        call1.set_can()
        time.sleep(1)
        call2.set_can()        
        time.sleep(1)
        call3.set_can()
        time.sleep(1)
        call4.set_can()

        for t in threads:
            print('join')
            t.join()
        print('shutdown')
        executor.shutdown()
        print('done shutdown')

class CancelTests(unittest.TestCase):
    def test_cancel_states(self):
        executor = threaded_futures.ThreadPoolExecutor(max_threads=1)

        call1 = Call(manual_finish=True)
        call2 = Call()
        call3 = Call()
        call4 = Call()

        fs = executor.run([call1, call2, call3, call4],
                          run_until=threaded_futures.RETURN_IMMEDIATELY)
        f1, f2, f3, f4 = fs

        call1.wait_on_called()
        self.assertEqual(f1.cancel(), False)
        self.assertEqual(f2.cancel(), True)
        self.assertEqual(f4.cancel(), True)
        self.assertEqual(f1.cancelled(), False)
        self.assertEqual(f2.cancelled(), True)
        self.assertEqual(f3.cancelled(), False)
        self.assertEqual(f4.cancelled(), True)
        self.assertEqual(f1.done(), False)
        self.assertEqual(f2.done(), True)
        self.assertEqual(f3.done(), False)
        self.assertEqual(f4.done(), True)

        call1.set_can()
        fs.wait(run_until=threaded_futures.ALL_COMPLETED)
        self.assertEqual(f1.result(), 42)
        self.assertRaises(threaded_futures.CancelledException, f2.result)
        self.assertRaises(threaded_futures.CancelledException, f2.exception)
        self.assertEqual(f3.result(), 42)
        self.assertRaises(threaded_futures.CancelledException, f4.result)
        self.assertRaises(threaded_futures.CancelledException, f4.exception)

        self.assertEqual(call2.called(), False)
        self.assertEqual(call4.called(), False)
        executor.shutdown()

    def test_wait_for_individual_cancel(self):
        def end_call():
            print ('Here1')
            time.sleep(1)
            print ('Here2')
            f2.cancel()
            print ('Here3')
            call1.set_can()
            print ('Here4')

        executor = threaded_futures.ThreadPoolExecutor(max_threads=1)

        call1 = Call(manual_finish=True)
        call2 = Call()

        fs = executor.run([call1, call2], run_until=threaded_futures.RETURN_IMMEDIATELY)
        f1, f2 = fs

        call1.wait_on_called()
        t = threading.Thread(target=end_call)
        t.start()
        self.assertRaises(threaded_futures.CancelledException, f2.result)
        self.assertRaises(threaded_futures.CancelledException, f2.exception)
        t.join()
        executor.shutdown()

    def test_cancel_all(self):
        executor = threaded_futures.ThreadPoolExecutor(max_threads=1)

        call1 = Call(manual_finish=True)
        call2 = Call()
        call3 = Call()
        call4 = Call()

        fs = executor.run([call1, call2, call3, call4],
                          run_until=threaded_futures.RETURN_IMMEDIATELY)
        f1, f2, f3, f4 = fs

        call1.wait_on_called()
        print('HERE!!!')
        self.assertRaises(threaded_futures.TimeoutException, fs.cancel, timeout=0)
        print('HERE 2!!!')
        call1.set_can()
        fs.cancel()

        self.assertFalse(f1.cancelled())
        self.assertTrue(f2.cancelled())
        self.assertTrue(f3.cancelled())
        self.assertTrue(f4.cancelled())
        executor.shutdown()

    def test_cancel_repr(self):
        executor = threaded_futures.ThreadPoolExecutor(max_threads=1)

        call1 = Call(manual_finish=True)
        call2 = Call()

        fs = executor.run([call1, call2], run_until=threaded_futures.RETURN_IMMEDIATELY)
        f1, f2 = fs

        call1.wait_on_called()
        call1.set_can()
        f2.cancel()
        self.assertEqual(repr(f2), '<Future state=cancelled>')
        executor.shutdown()

class FutureListTests(unittest.TestCase):
    def test_cancel_states(self):
        f1 = FutureStub(cancelled=False, done=False)
        f2 = FutureStub(cancelled=False, done=True)
        f3 = FutureStub(cancelled=False, done=True, exception=IOError())
        f4 = FutureStub(cancelled=True, done=True)

        fs = [f1, f2, f3, f4]
        f = threaded_futures.FutureList(fs, None)

        self.assertEqual(f.running_futures(), [f1])
        self.assertEqual(f.cancelled_futures(), [f4])
        self.assertEqual(f.done_futures(), [f2, f3, f4])
        self.assertEqual(f.successful_futures(), [f2])
        self.assertEqual(f.exception_futures(), [f3])

    def test_has_running_futures(self):
        f1 = FutureStub(cancelled=False, done=False)
        f2 = FutureStub(cancelled=False, done=True)

        self.assertTrue(
                threaded_futures.FutureList([f1], None).has_running_futures())
        self.assertFalse(
                threaded_futures.FutureList([f2], None).has_running_futures())

    def test_has_cancelled_futures(self):
        f1 = FutureStub(cancelled=True, done=True)
        f2 = FutureStub(cancelled=False, done=True)

        self.assertTrue(
                threaded_futures.FutureList([f1], None).has_cancelled_futures())
        self.assertFalse(
                threaded_futures.FutureList([f2], None).has_cancelled_futures())

    def test_has_done_futures(self):
        f1 = FutureStub(cancelled=True, done=True)
        f2 = FutureStub(cancelled=False, done=True)
        f3 = FutureStub(cancelled=False, done=False)

        self.assertTrue(
                threaded_futures.FutureList([f1], None).has_done_futures())
        self.assertTrue(
                threaded_futures.FutureList([f2], None).has_done_futures())
        self.assertFalse(
                threaded_futures.FutureList([f3], None).has_done_futures())

    def test_has_successful_futures(self):
        f1 = FutureStub(cancelled=False, done=True)
        f2 = FutureStub(cancelled=False, done=True, exception=IOError())
        f3 = FutureStub(cancelled=False, done=False)
        f4 = FutureStub(cancelled=True, done=True)

        self.assertTrue(
                threaded_futures.FutureList([f1], None).has_successful_futures())
        self.assertFalse(
                threaded_futures.FutureList([f2], None).has_successful_futures())
        self.assertFalse(
                threaded_futures.FutureList([f3], None).has_successful_futures())
        self.assertFalse(
                threaded_futures.FutureList([f4], None).has_successful_futures())

    def test_has_exception_futures(self):
        f1 = FutureStub(cancelled=False, done=True)
        f2 = FutureStub(cancelled=False, done=True, exception=IOError())
        f3 = FutureStub(cancelled=False, done=False)
        f4 = FutureStub(cancelled=True, done=True)

        self.assertFalse(
                threaded_futures.FutureList([f1], None).has_exception_futures())
        self.assertTrue(
                threaded_futures.FutureList([f2], None).has_exception_futures())
        self.assertFalse(
                threaded_futures.FutureList([f3], None).has_exception_futures())
        self.assertFalse(
                threaded_futures.FutureList([f4], None).has_exception_futures())

    def test_get_item(self):
        f1 = FutureStub(cancelled=False, done=False)
        f2 = FutureStub(cancelled=False, done=True)
        f3 = FutureStub(cancelled=False, done=True)

        fs = [f1, f2, f3]
        f = threaded_futures.FutureList(fs, None)
        self.assertEqual(f[0], f1)
        self.assertEqual(f[1], f2)
        self.assertEqual(f[2], f3)
        self.assertRaises(IndexError, f.__getitem__, 3)

    def test_len(self):
        f1 = FutureStub(cancelled=False, done=False)
        f2 = FutureStub(cancelled=False, done=True)
        f3 = FutureStub(cancelled=False, done=True)

        f = threaded_futures.FutureList([f1, f2, f3], None)
        self.assertEqual(len(f), 3)

    def test_iter(self):
        f1 = FutureStub(cancelled=False, done=False)
        f2 = FutureStub(cancelled=False, done=True)
        f3 = FutureStub(cancelled=False, done=True)

        fs = [f1, f2, f3]
        f = threaded_futures.FutureList(fs, None)
        self.assertEqual(list(iter(f)), fs)

    def test_contains(self):
        f1 = FutureStub(cancelled=False, done=False)
        f2 = FutureStub(cancelled=False, done=True)
        f3 = FutureStub(cancelled=False, done=True)

        f = threaded_futures.FutureList([f1, f2], None)
        self.assertTrue(f1 in f)
        self.assertTrue(f2 in f)
        self.assertFalse(f3 in f)

    def test_repr(self):
        running = FutureStub(cancelled=False, done=False)
        result = FutureStub(cancelled=False, done=True)
        exception = FutureStub(cancelled=False, done=True, exception=IOError())
        cancelled = FutureStub(cancelled=True, done=True)

        f = threaded_futures.FutureList(
                [running] * 4 + [result] * 3 + [exception] * 2 + [cancelled],
                None)

        self.assertEqual(repr(f),
                         '<FutureList #futures=10 '
                         '[#success=3 #exception=2 #cancelled=1]>')
def test_main():
    test.support.run_unittest(CancelTests,
                              ConcurrentWaitsTest,
                              FutureListTests,
                              ShutdownTest)

if __name__ == "__main__":
    test_main()