Skip to content

Commit 8f89093

Browse files
committed
Add some tests for BlockingLimitedButler.
A glaring omission here is a test that blocks and then succeeds, which would require some combination of deeper mocking or threads.
1 parent d52d788 commit 8f89093

1 file changed

Lines changed: 102 additions & 0 deletions

File tree

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
# This file is part of pipe_base.
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+
# This software is dual licensed under the GNU General Public License and also
10+
# under a 3-clause BSD license. Recipients may choose which of these licenses
11+
# to use; please see the files gpl-3.0.txt and/or bsd_license.txt,
12+
# respectively. If you choose the GPL option then the following text applies
13+
# (but note that there is still no warranty even if you opt for BSD instead):
14+
#
15+
# This program is free software: you can redistribute it and/or modify
16+
# it under the terms of the GNU General Public License as published by
17+
# the Free Software Foundation, either version 3 of the License, or
18+
# (at your option) any later version.
19+
#
20+
# This program is distributed in the hope that it will be useful,
21+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
22+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23+
# GNU General Public License for more details.
24+
#
25+
# You should have received a copy of the GNU General Public License
26+
# along with this program. If not, see <https://www.gnu.org/licenses/>.
27+
28+
"""Tests for execution butler."""
29+
30+
import logging
31+
import os
32+
import unittest
33+
34+
import lsst.utils.tests
35+
from lsst.daf.butler import DataCoordinate, DatasetRef
36+
from lsst.pipe.base.blocking_limited_butler import _LOG as module_log
37+
from lsst.pipe.base.blocking_limited_butler import BlockingLimitedButler
38+
from lsst.pipe.base.tests.mocks import InMemoryRepo
39+
40+
TESTDIR = os.path.abspath(os.path.dirname(__file__))
41+
42+
43+
class BlockingLimitedButlerTestCase(unittest.TestCase):
44+
"""Unit tests for BlockingLimitedButler"""
45+
46+
def test_no_block_nonexistent(self):
47+
"""Test checking/getting with no dataset and blocking disabled."""
48+
helper = InMemoryRepo("base.yaml")
49+
helper.add_task()
50+
helper.pipeline_graph.resolve(helper.butler.registry)
51+
ref = DatasetRef(
52+
helper.pipeline_graph.dataset_types["dataset_auto0"].dataset_type,
53+
DataCoordinate.make_empty(helper.butler.dimensions),
54+
run="input_run",
55+
)
56+
helper.pipeline_graph.register_dataset_types(helper.butler)
57+
in_memory_butler = helper.make_limited_butler()
58+
blocking_butler = BlockingLimitedButler(in_memory_butler, timeouts={})
59+
with self.assertNoLogs(module_log, level=logging.INFO):
60+
self.assertFalse(blocking_butler.stored_many([ref])[ref])
61+
with self.assertRaises(FileNotFoundError):
62+
blocking_butler.get(ref)
63+
64+
def test_timeout_nonexistent(self):
65+
"""Test checking/getting with no dataset, leading to a timeout."""
66+
helper = InMemoryRepo("base.yaml")
67+
helper.add_task()
68+
helper.pipeline_graph.resolve(helper.butler.registry)
69+
ref = DatasetRef(
70+
helper.pipeline_graph.dataset_types["dataset_auto0"].dataset_type,
71+
DataCoordinate.make_empty(helper.butler.dimensions),
72+
run="input_run",
73+
)
74+
helper.pipeline_graph.register_dataset_types(helper.butler)
75+
in_memory_butler = helper.make_limited_butler()
76+
blocking_butler = BlockingLimitedButler(in_memory_butler, timeouts={"dataset_auto0": 0.1})
77+
with self.assertLogs(module_log, level=logging.INFO) as cm:
78+
self.assertFalse(blocking_butler.stored_many([ref])[ref])
79+
self.assertIn("not immediately available", cm.output[0])
80+
with self.assertLogs(module_log, level=logging.INFO) as cm:
81+
with self.assertRaises(FileNotFoundError):
82+
blocking_butler.get(ref)
83+
self.assertIn("not immediately available", cm.output[0])
84+
85+
def test_no_waiting_if_exists(self):
86+
"""Test checking/getting with dataset present immediately, so no
87+
waiting should be necessary.
88+
"""
89+
helper = InMemoryRepo("base.yaml")
90+
helper.add_task()
91+
(ref,) = helper.insert_datasets("dataset_auto0")
92+
helper.pipeline_graph.register_dataset_types(helper.butler)
93+
in_memory_butler = helper.make_limited_butler()
94+
blocking_butler = BlockingLimitedButler(in_memory_butler, timeouts={})
95+
with self.assertNoLogs(module_log, level=logging.INFO):
96+
self.assertTrue(blocking_butler.stored_many([ref])[ref])
97+
self.assertIsNotNone(blocking_butler.get(ref))
98+
99+
100+
if __name__ == "__main__":
101+
lsst.utils.tests.init()
102+
unittest.main()

0 commit comments

Comments
 (0)