Debian Codegraph @

futurist/tests/test_waiters.py

raw
1 # Licensed under the Apache License, Version 2.0 (the "License"); you may
2 # not use this file except in compliance with the License. You may obtain
3 # a copy of the License at
4 #
5 # http://www.apache.org/licenses/LICENSE-2.0
6 #
7 # Unless required by applicable law or agreed to in writing, software
8 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
9 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
10 # License for the specific language governing permissions and limitations
11 # under the License.
12
13 import time
14
15 import eventlet
16 import testscenarios
17
18 import futurist
19 from futurist.tests import base
20 from futurist import waiters
21
22
23 # Module level functions need to be used since the process pool
24 # executor can not access instance or lambda level functions (since those
25 # are not pickleable).
26
27 def mini_delay(use_eventlet_sleep=False):
28 if use_eventlet_sleep:
29 eventlet.sleep(0.1)
30 else:
31 time.sleep(0.1)
32 return 1
33
34
35 class TestWaiters(testscenarios.TestWithScenarios, base.TestCase):
36 scenarios = [
37 ('sync', {'executor_cls': futurist.SynchronousExecutor,
38 'executor_kwargs': {}, 'use_eventlet_sleep': False}),
39 ('green_sync', {'executor_cls': futurist.SynchronousExecutor,
40 'executor_kwargs': {'green': True},
41 'use_eventlet_sleep': True}),
42 ('green', {'executor_cls': futurist.GreenThreadPoolExecutor,
43 'executor_kwargs': {}, 'use_eventlet_sleep': True}),
44 ('thread', {'executor_cls': futurist.ThreadPoolExecutor,
45 'executor_kwargs': {}, 'use_eventlet_sleep': False}),
46 ('process', {'executor_cls': futurist.ProcessPoolExecutor,
47 'executor_kwargs': {}, 'use_eventlet_sleep': False}),
48 ]
49
50 def setUp(self):
51 super().setUp()
52 self.executor = self.executor_cls(**self.executor_kwargs)
53
54 def tearDown(self):
55 super().tearDown()
56 self.executor.shutdown()
57 self.executor = None
58
59 def test_wait_for_any(self):
60 fs = []
61 for _i in range(0, 10):
62 fs.append(self.executor.submit(
63 mini_delay, use_eventlet_sleep=self.use_eventlet_sleep))
64 all_done_fs = []
65 total_fs = len(fs)
66 while len(all_done_fs) != total_fs:
67 done, not_done = waiters.wait_for_any(fs)
68 all_done_fs.extend(done)
69 fs = not_done
70 self.assertEqual(total_fs, sum(f.result() for f in all_done_fs))
71
72 def test_wait_for_all(self):
73 fs = []
74 for _i in range(0, 10):
75 fs.append(self.executor.submit(
76 mini_delay, use_eventlet_sleep=self.use_eventlet_sleep))
77 done_fs, not_done_fs = waiters.wait_for_all(fs)
78 self.assertEqual(len(fs), sum(f.result() for f in done_fs))
79 self.assertEqual(0, len(not_done_fs))
80
81 def test_no_mixed_wait_for_any(self):
82 fs = [futurist.GreenFuture(), futurist.Future()]
83 self.assertRaises(RuntimeError, waiters.wait_for_any, fs)
84
85 def test_no_mixed_wait_for_all(self):
86 fs = [futurist.GreenFuture(), futurist.Future()]
87 self.assertRaises(RuntimeError, waiters.wait_for_all, fs)