diff options
| author | 2022-11-24 14:43:32 +0800 | |
|---|---|---|
| committer | 2022-11-24 14:43:32 +0800 | |
| commit | ea9d5e8b7be85400301721efdf19066013bb4a7a (patch) | |
| tree | ef09b23aa795d76470776b1d21be80b326ebf0a1 | |
| parent | Add unit tests for libmount (diff) | |
Add concurrency tests to libmount
Diffstat (limited to '')
| -rw-r--r-- | tests/test_libmount.py | 38 | 
1 files changed, 37 insertions, 1 deletions
| diff --git a/tests/test_libmount.py b/tests/test_libmount.py index fe9b7ca..b9a5c15 100644 --- a/tests/test_libmount.py +++ b/tests/test_libmount.py @@ -1,4 +1,5 @@ -from contextlib import contextmanager +from concurrent.futures import ThreadPoolExecutor +from contextlib import contextmanager, suppress  from pathlib import Path  from unittest import TestCase  from uuid import uuid4 @@ -79,3 +80,38 @@ class LibMountTests(TestCase):              with self.assertRaises(OSError) as cm:                  libmount.unmount(path, 251)              self.assertIn("Invalid argument", str(cm.exception)) + +    def test_threading(self): +        """Test concurrent mounting works in multi-thread environments.""" +        paths = [self.temp_dir / str(uuid4()) for _ in range(16)] + +        for path in paths: +            path.mkdir() +            self.assertFalse(path.is_mount()) + +        try: +            with ThreadPoolExecutor() as pool: +                res = list( +                    pool.map( +                        libmount.mount, +                        [""] * len(paths), +                        paths, +                        ["tmpfs"] * len(paths), +                    ) +                ) +                self.assertEqual(len(res), len(paths)) + +                for path in paths: +                    with self.subTest(path=path): +                        self.assertTrue(path.is_mount()) + +                unmounts = list(pool.map(libmount.unmount, paths)) +                self.assertEqual(len(unmounts), len(paths)) + +                for path in paths: +                    with self.subTest(path=path): +                        self.assertFalse(path.is_mount()) +        finally: +            with suppress(OSError): +                for path in paths: +                    libmount.unmount(path) | 
