Coverage for tests/test_utils.py: 100%
47 statements
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 02:14 -0700
« prev ^ index » next coverage.py v7.16.0, created at 2026-09-22 02:14 -0700
1# This file is part of lsst-resources.
2#
3# Developed for the LSST Data Management System.
4# This product includes software developed by the LSST Project
5# (https://www.lsst.org).
6# See the COPYRIGHT file at the top-level directory of this distribution
7# for details of code ownership.
8#
9# Use of this source code is governed by a 3-clause BSD-style
10# license that can be found in the LICENSE file.
12import os
13import unittest
14import unittest.mock
16from lsst.resources import ResourcePath
17from lsst.resources.file import FileResourcePath
18from lsst.resources.s3 import S3ResourcePath
19from lsst.resources.utils import (
20 MAX_WORKERS,
21 _get_configured_num_workers,
22 _get_default_num_workers,
23 _get_num_workers,
24)
27def _clear_worker_caches() -> None:
28 """Discard memoized worker-count lookups."""
29 _get_configured_num_workers.cache_clear()
30 _get_default_num_workers.cache_clear()
33class NumWorkersTestCase(unittest.TestCase):
34 """Tests for the worker-count calculation."""
36 def setUp(self) -> None:
37 _clear_worker_caches()
39 def tearDown(self) -> None:
40 _clear_worker_caches()
42 @unittest.mock.patch.dict(os.environ, {}, clear=False)
43 def test_default_is_capped(self) -> None:
44 os.environ.pop("LSST_RESOURCES_NUM_WORKERS", None)
45 _clear_worker_caches()
46 self.assertLessEqual(_get_num_workers(), MAX_WORKERS)
47 self.assertEqual(_get_num_workers(2), 2)
49 @unittest.mock.patch.dict(os.environ, {"LSST_RESOURCES_NUM_WORKERS": "99"})
50 def test_explicit_request_bypasses_cap(self) -> None:
51 _clear_worker_caches()
52 self.assertEqual(_get_num_workers(), 99)
53 self.assertEqual(_get_num_workers(2), 99)
55 def test_docstring_is_present(self) -> None:
56 # An f-string in the leading position is not a docstring.
57 self.assertIsNotNone(_get_num_workers.__doc__)
60class WorkerCapTestCase(unittest.TestCase):
61 """Tests for per-scheme worker caps."""
63 def setUp(self) -> None:
64 _clear_worker_caches()
66 def tearDown(self) -> None:
67 _clear_worker_caches()
69 def test_schemes_share_the_default_cap(self) -> None:
70 self.assertEqual(ResourcePath._max_workers, MAX_WORKERS)
71 self.assertEqual(FileResourcePath._max_workers, MAX_WORKERS)
72 self.assertEqual(S3ResourcePath._max_workers, MAX_WORKERS)
74 @unittest.mock.patch.dict(os.environ, {}, clear=False)
75 @unittest.mock.patch.object(FileResourcePath, "_max_workers", 2)
76 def test_an_overridden_cap_is_honored(self) -> None:
77 os.environ.pop("LSST_RESOURCES_NUM_WORKERS", None)
78 _clear_worker_caches()
79 self.assertEqual(_get_num_workers(FileResourcePath._max_workers), 2)
81 @unittest.mock.patch.dict(os.environ, {"LSST_RESOURCES_NUM_WORKERS": "99"})
82 def test_explicit_request_overrides_the_scheme_cap(self) -> None:
83 _clear_worker_caches()
84 self.assertEqual(_get_num_workers(S3ResourcePath._max_workers), 99)
87if __name__ == "__main__":
88 unittest.main()