diff options
| author | 2022-12-04 09:52:42 +0800 | |
|---|---|---|
| committer | 2022-12-04 09:52:42 +0800 | |
| commit | d33a55d775c9f15b6cd9089c7e2de3e827f0b837 (patch) | |
| tree | 267a1b699c4ccd5ea00b9311e3ee29511c28174e /tests | |
| parent | Add refactors for Size enum rename (diff) | |
Add usage of ExitStack, remove warning suppress
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/test_memfs.py | 38 | 
1 files changed, 18 insertions, 20 deletions
| diff --git a/tests/test_memfs.py b/tests/test_memfs.py index 53dee22..0555726 100644 --- a/tests/test_memfs.py +++ b/tests/test_memfs.py @@ -1,6 +1,6 @@  import logging -import warnings  from concurrent.futures import ThreadPoolExecutor +from contextlib import ExitStack  from unittest import TestCase, mock  from uuid import uuid4 @@ -14,32 +14,30 @@ class MemFSTests(TestCase):          super().setUp()          self.logger = logging.getLogger("snekbox.memfs")          self.logger.setLevel(logging.WARNING) -        warnings.filterwarnings( -            "ignore", -            ".*Implicitly cleaning up.*", -            ResourceWarning, -            "snekbox.memfs", -        )      @mock.patch("snekbox.memfs.uuid4", lambda: UUID_TEST)      def test_assignment_thread_safe(self):          """Test concurrent mounting works in multi-thread environments."""          # Concurrently create MemFS in threads, check only 1 can be created          # Others should result in RuntimeError -        with ThreadPoolExecutor() as executor: -            memfs: MemFS | None = None -            futures = [executor.submit(MemFS, 10) for _ in range(8)] -            for future in futures: -                # We should have exactly one result and all others RuntimeErrors -                if err := future.exception(): -                    self.assertIsInstance(err, RuntimeError) -                else: -                    self.assertIsNone(memfs) -                    memfs = future.result() +        with ExitStack() as stack: +            with ThreadPoolExecutor() as executor: +                memfs: MemFS | None = None +                # Each future uses enter_context to ensure __exit__ on test exception +                futures = [ +                    executor.submit(lambda: stack.enter_context(MemFS(10))) for _ in range(8) +                ] +                for future in futures: +                    # We should have exactly one result and all others RuntimeErrors +                    if err := future.exception(): +                        self.assertIsInstance(err, RuntimeError) +                    else: +                        self.assertIsNone(memfs) +                        memfs = future.result() -            # Original memfs should still exist afterwards -            self.assertIsInstance(memfs, MemFS) -            self.assertTrue(memfs.path.is_mount()) +                # Original memfs should still exist afterwards +                self.assertIsInstance(memfs, MemFS) +                self.assertTrue(memfs.path.is_mount())      def test_cleanup(self):          """Test explicit cleanup.""" | 
