aboutsummaryrefslogtreecommitdiffstats
path: root/tests
diff options
context:
space:
mode:
authorGravatar ionite34 <[email protected]>2022-11-22 23:49:06 -0500
committerGravatar ionite34 <[email protected]>2022-11-22 23:49:06 -0500
commitb74b813ae6ac2ebf374cc12bb0e9689f49776f2e (patch)
tree6a83bd9e498bcc42397b591e743d39660d596309 /tests
parentRefactor MemFS set and semaphore to rely on fid lock (diff)
Add memfs unit tests
Diffstat (limited to 'tests')
-rw-r--r--tests/test_memfs.py39
1 files changed, 39 insertions, 0 deletions
diff --git a/tests/test_memfs.py b/tests/test_memfs.py
new file mode 100644
index 0000000..128530a
--- /dev/null
+++ b/tests/test_memfs.py
@@ -0,0 +1,39 @@
+import logging
+from concurrent.futures import ThreadPoolExecutor
+from unittest import TestCase, mock
+from uuid import uuid4
+
+from snekbox.memfs import MemFS
+
+UUID_TEST = uuid4()
+
+
+def get_memfs_with_context():
+ return MemFS(10).__enter__()
+
+
+class NsJailTests(TestCase):
+ def setUp(self):
+ super().setUp()
+ self.logger = logging.getLogger("snekbox.memfs")
+ self.logger.setLevel(logging.WARNING)
+
+ @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 pool:
+ memfs: MemFS | None = None
+ futures = [pool.submit(get_memfs_with_context) 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.exists())