|
| 1 | +# -*- coding: utf-8 -*- |
| 2 | +# Copyright 2022 Karellen, Inc. and contributors |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +from multiprocessing.process import current_process |
| 17 | + |
| 18 | +from gevent import monkey |
| 19 | + |
| 20 | +if not getattr(current_process(), "_inheriting", False): |
| 21 | + monkey.patch_all() |
| 22 | + |
| 23 | + # from multiprocessing.util import log_to_stderr |
| 24 | + # log_to_stderr(1) |
| 25 | + |
| 26 | +from unittest import TestCase, main |
| 27 | + |
| 28 | +from geventmp.monkey import GEVENT_SAVED_MODULE_SETTINGS |
| 29 | + |
| 30 | +import multiprocessing as mp |
| 31 | +from multiprocessing.util import get_logger |
| 32 | +import sys |
| 33 | +from time import monotonic |
| 34 | + |
| 35 | +logger = get_logger() |
| 36 | + |
| 37 | + |
| 38 | +class TestSynchronizers(TestCase): |
| 39 | + def setUp(self): |
| 40 | + self.assertTrue(monkey.saved[GEVENT_SAVED_MODULE_SETTINGS].get("geventmp"), |
| 41 | + "GeventMP patch has not run!") |
| 42 | + self.tearDown() |
| 43 | + |
| 44 | + def tearDown(self): |
| 45 | + sys.stdout.flush() |
| 46 | + sys.stderr.flush() |
| 47 | + logger.info("=====================") |
| 48 | + sys.stdout.flush() |
| 49 | + |
| 50 | + def test_semaphore_fork(self): |
| 51 | + self._test_semaphore("fork") |
| 52 | + |
| 53 | + def test_semaphore_spawn(self): |
| 54 | + self._test_semaphore("spawn") |
| 55 | + |
| 56 | + def test_semaphore_forkserver(self): |
| 57 | + self._test_semaphore("forkserver") |
| 58 | + |
| 59 | + def test_bounded_semaphore_fork(self): |
| 60 | + self._test_bounded_semaphore("fork") |
| 61 | + |
| 62 | + def test_bounded_semaphore_spawn(self): |
| 63 | + self._test_bounded_semaphore("spawn") |
| 64 | + |
| 65 | + def test_bounded_semaphore_forkserver(self): |
| 66 | + self._test_bounded_semaphore("forkserver") |
| 67 | + |
| 68 | + def _test_semaphore(self, ctx_name): |
| 69 | + ctx = mp.get_context(ctx_name) |
| 70 | + s = ctx.Semaphore() |
| 71 | + self.assertEqual(s.get_value(), 1) |
| 72 | + self.assertEqual(s._semlock._count(), 0) |
| 73 | + repr(s) |
| 74 | + |
| 75 | + s.release() |
| 76 | + self.assertEqual(s.get_value(), 2) |
| 77 | + self.assertEqual(s._semlock._count(), -1) |
| 78 | + repr(s) |
| 79 | + |
| 80 | + self.assertTrue(s.acquire()) |
| 81 | + self.assertEqual(s.get_value(), 1) |
| 82 | + self.assertEqual(s._semlock._count(), 0) |
| 83 | + repr(s) |
| 84 | + |
| 85 | + self.assertTrue(s.acquire()) |
| 86 | + self.assertEqual(s.get_value(), 0) |
| 87 | + self.assertEqual(s._semlock._count(), 1) |
| 88 | + repr(s) |
| 89 | + |
| 90 | + self.assertFalse(s.acquire(block=False)) |
| 91 | + self.assertEqual(s.get_value(), 0) |
| 92 | + self.assertEqual(s._semlock._count(), 1) |
| 93 | + repr(s) |
| 94 | + |
| 95 | + start = monotonic() |
| 96 | + self.assertFalse(s.acquire(timeout=1)) |
| 97 | + duration = monotonic() - start |
| 98 | + self.assertGreater(duration, 1) |
| 99 | + self.assertLess(duration, 3) |
| 100 | + s.release() |
| 101 | + self.assertEqual(s.get_value(), 1) |
| 102 | + self.assertEqual(s._semlock._count(), 0) |
| 103 | + |
| 104 | + def _test_bounded_semaphore(self, ctx_name): |
| 105 | + ctx = mp.get_context(ctx_name) |
| 106 | + s = ctx.BoundedSemaphore(2) |
| 107 | + repr(s) |
| 108 | + |
| 109 | + self.assertEqual(s.get_value(), 2) |
| 110 | + self.assertEqual(s._semlock._count(), 0) |
| 111 | + |
| 112 | + self.assertTrue(s.acquire()) |
| 113 | + self.assertEqual(s.get_value(), 1) |
| 114 | + self.assertEqual(s._semlock._count(), 1) |
| 115 | + repr(s) |
| 116 | + |
| 117 | + self.assertTrue(s.acquire()) |
| 118 | + self.assertEqual(s.get_value(), 0) |
| 119 | + self.assertEqual(s._semlock._count(), 2) |
| 120 | + repr(s) |
| 121 | + |
| 122 | + self.assertFalse(s.acquire(block=False)) |
| 123 | + self.assertEqual(s.get_value(), 0) |
| 124 | + self.assertEqual(s._semlock._count(), 2) |
| 125 | + repr(s) |
| 126 | + |
| 127 | + start = monotonic() |
| 128 | + self.assertFalse(s.acquire(timeout=1)) |
| 129 | + duration = monotonic() - start |
| 130 | + self.assertGreater(duration, 1) |
| 131 | + self.assertLess(duration, 3) |
| 132 | + s.release() |
| 133 | + self.assertEqual(s.get_value(), 1) |
| 134 | + self.assertEqual(s._semlock._count(), 1) |
| 135 | + s.release() |
| 136 | + self.assertEqual(s.get_value(), 2) |
| 137 | + self.assertEqual(s._semlock._count(), 0) |
| 138 | + with self.assertRaises(ValueError): |
| 139 | + s.release() |
| 140 | + |
| 141 | + |
| 142 | +if __name__ == '__main__': |
| 143 | + main() |
0 commit comments