Coverage for tests/test_threads.py: 28%

26 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-07-08 09:53 +0000

1# This file is part of utils. 

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. 

11# 

12 

13import os 

14import unittest 

15 

16from lsst.utils.threads import disable_implicit_threading, set_thread_envvars 

17 

18try: 

19 import numexpr 

20except ImportError: 

21 numexpr = None 

22try: 

23 import threadpoolctl 

24except ImportError: 

25 threadpoolctl = None 

26 

27 

28class ThreadsTestCase(unittest.TestCase): 

29 """Tests for threads.""" 

30 

31 def testDisable(self): 

32 set_thread_envvars(2, override=True) 

33 self.assertEqual(os.environ["OMP_NUM_THREADS"], "2") 

34 set_thread_envvars(3, override=False) 

35 self.assertEqual(os.environ["OMP_NUM_THREADS"], "2") 

36 

37 disable_implicit_threading() 

38 self.assertEqual(os.environ["OMP_NUM_THREADS"], "1") 

39 self.assertEqual(os.environ["OMP_PROC_BIND"], "false") 

40 

41 # Check that we have only one thread. 

42 if numexpr: 

43 self.assertEqual(numexpr.utils.get_num_threads(), 1) 

44 if threadpoolctl: 

45 info = threadpoolctl.threadpool_info() 

46 for api in info: 

47 self.assertEqual(api["num_threads"], 1, f"API: {api}") 

48 

49 

50if __name__ == "__main__": 

51 unittest.main()