Coverage for tests/test_thread_utils.py: 24%

37 statements  

« prev     ^ index     » next       coverage.py v7.5.1, created at 2024-05-07 02:46 -0700

1# This file is part of daf_butler. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (http://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 <http://www.gnu.org/licenses/>. 

27 

28import unittest 

29 

30from lsst.daf.butler._utilities.locked_object import LockedObject 

31from lsst.daf.butler._utilities.named_locks import NamedLocks 

32from lsst.daf.butler._utilities.thread_safe_cache import ThreadSafeCache 

33 

34 

35class ThreadSafeCacheTestCase(unittest.TestCase): 

36 """Test ThreadSafeCache.""" 

37 

38 def test_cache(self): 

39 cache = ThreadSafeCache() 

40 self.assertIsNone(cache.get("unknown")) 

41 self.assertEqual(cache.set_or_get("key", "a"), "a") 

42 self.assertEqual(cache.get("key"), "a") 

43 self.assertEqual(cache.set_or_get("key", "b"), "a") 

44 self.assertEqual(cache.get("key"), "a") 

45 self.assertIsNone(cache.get("other")) 

46 

47 

48class NamedLocksTestCase(unittest.TestCase): 

49 """Test NamedLocks.""" 

50 

51 def test_named_locks(self): 

52 locks = NamedLocks() 

53 lock1 = locks._get_lock("a") 

54 lock2 = locks._get_lock("b") 

55 lock3 = locks._get_lock("a") 

56 

57 self.assertIs(lock1, lock3) 

58 self.assertIsNot(lock1, lock2) 

59 

60 self.assertFalse(lock1.locked()) 

61 self.assertFalse(lock2.locked()) 

62 with locks.lock("a"): 

63 self.assertTrue(lock1.locked()) 

64 self.assertFalse(lock2.locked()) 

65 self.assertFalse(lock1.locked()) 

66 self.assertFalse(lock2.locked()) 

67 

68 

69class LockedObjectTestCase(unittest.TestCase): 

70 """Test LockedObject.""" 

71 

72 def test_named_locks(self): 

73 data = object() 

74 locked_obj = LockedObject(data) 

75 self.assertFalse(locked_obj._lock.locked()) 

76 with locked_obj.access() as accessed: 

77 self.assertTrue(locked_obj._lock.locked()) 

78 self.assertIs(data, accessed) 

79 self.assertFalse(locked_obj._lock.locked()) 

80 

81 

82if __name__ == "__main__": 

83 unittest.main()