11# SPDX-FileCopyrightText: Copyright (c) 2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
22# SPDX-License-Identifier: BSD-2-Clause
33
4+ import concurrent .futures
45import multiprocessing as mp
56import os
67
@@ -20,88 +21,83 @@ def cuInit_raising(arg):
2021# not assigned until we attempt to initialize - mock.patch.object cannot locate
2122# the non-existent original method, and so fails. Instead we patch
2223# driver.cuInit with our raising version prior to any attempt to initialize.
23- def cuInit_raising_test (result_queue ):
24+ def cuInit_raising_test ():
2425 driver .cuInit = cuInit_raising
2526
26- success = False
27- msg = None
28-
2927 try :
3028 # A CUDA operation that forces initialization of the device
3129 cuda .device_array (1 )
3230 except CudaSupportError as e :
3331 success = True
3432 msg = e .msg
33+ else :
34+ success = False
35+ msg = None
3536
36- result_queue . put (( success , msg ))
37+ return success , msg
3738
3839
3940# Similar to cuInit_raising_test above, but for testing that the string
4041# returned by cuda_error() is as expected.
41- def initialization_error_test (result_queue ):
42+ def initialization_error_test ():
4243 driver .cuInit = cuInit_raising
4344
44- success = False
45- msg = None
46-
4745 try :
4846 # A CUDA operation that forces initialization of the device
4947 cuda .device_array (1 )
5048 except CudaSupportError :
5149 success = True
50+ else :
51+ success = False
5252
53- msg = cuda .cuda_error ()
54- result_queue .put ((success , msg ))
53+ return success , cuda .cuda_error ()
5554
5655
5756# For testing the path where Driver.__init__() catches a CudaSupportError
58- def cuda_disabled_test (result_queue ):
59- success = False
60- msg = None
61-
57+ def cuda_disabled_test ():
6258 try :
6359 # A CUDA operation that forces initialization of the device
6460 cuda .device_array (1 )
6561 except CudaSupportError as e :
6662 success = True
6763 msg = e .msg
64+ else :
65+ success = False
66+ msg = None
6867
69- result_queue . put (( success , msg ))
68+ return success , msg
7069
7170
7271# Similar to cuda_disabled_test, but checks cuda.cuda_error() instead of the
7372# exception raised on initialization
74- def cuda_disabled_error_test (result_queue ):
75- success = False
76- msg = None
77-
73+ def cuda_disabled_error_test ():
7874 try :
7975 # A CUDA operation that forces initialization of the device
8076 cuda .device_array (1 )
8177 except CudaSupportError :
8278 success = True
79+ else :
80+ success = False
8381
84- msg = cuda .cuda_error ()
85- result_queue .put ((success , msg ))
82+ return success , cuda .cuda_error ()
8683
8784
8885@skip_on_cudasim ("CUDA Simulator does not initialize driver" )
8986class TestInit (CUDATestCase ):
9087 def _test_init_failure (self , target , expected ):
9188 # Run the initialization failure test in a separate subprocess
92- ctx = mp .get_context ("spawn" )
93- result_queue = ctx .Queue ()
94- proc = ctx .Process (target = target , args = (result_queue ,))
95- proc .start ()
96- proc .join (30 ) # should complete within 30s
97- success , msg = result_queue .get ()
89+ with concurrent .futures .ProcessPoolExecutor (
90+ mp_context = mp .get_context ("spawn" )
91+ ) as exe :
92+ # should complete within 30s
93+ success , msg = exe .submit (target ).result (timeout = 30 )
9894
9995 # Ensure the child process raised an exception during initialization
10096 # before checking the message
10197 if not success :
102- self . fail ( "CudaSupportError not raised" )
98+ assert "CudaSupportError not raised" in msg
10399
104- self . assertIn ( expected , msg )
100+ assert expected in msg
105101
106102 def test_init_failure_raising (self ):
107103 expected = "Error at driver init: CUDA_ERROR_UNKNOWN (999)"
0 commit comments