Skip to content

Commit fa40693

Browse files
Adjust test suite to not crash when no devices are present
1 parent 7d70a99 commit fa40693

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

dpctl/tests/test_service.py

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,19 +119,24 @@ def test_dev_utils():
119119

120120
ctx_mngr = dd.syclinterface_diagnostics
121121

122+
try:
123+
device = dpctl.SyclDevice()
124+
except dpctl.SyclDeviceCreationError:
125+
pytest.skip("Default-constructed device could not be created")
126+
122127
with ctx_mngr():
123-
dpctl.SyclDevice().parent_device
128+
device.parent_device
124129
with ctx_mngr(verbosity="error"):
125-
dpctl.SyclDevice().parent_device
130+
device.parent_device
126131
with pytest.raises(ValueError):
127132
with ctx_mngr(verbosity="blah"):
128-
dpctl.SyclDevice().parent_device
133+
device.parent_device
129134
with tempfile.TemporaryDirectory() as temp_dir:
130135
with ctx_mngr(log_dir=temp_dir):
131-
dpctl.SyclDevice().parent_device
136+
device.parent_device
132137
with pytest.raises(ValueError):
133138
with ctx_mngr(log_dir="/not_a_dir"):
134-
dpctl.SyclDevice().parent_device
139+
device.parent_device
135140

136141

137142
def test_syclinterface():
@@ -189,34 +194,37 @@ def test_main_full_list():
189194
[sys.executable, "-m", "dpctl", "-f"], capture_output=True
190195
)
191196
assert res.returncode == 0
192-
assert res.stdout
193-
assert res.stdout.decode("utf-8")
197+
if dpctl.get_num_devices() > 0:
198+
assert res.stdout
199+
assert res.stdout.decode("utf-8")
194200

195201

196202
def test_main_long_list():
197203
res = subprocess.run(
198204
[sys.executable, "-m", "dpctl", "-l"], capture_output=True
199205
)
200206
assert res.returncode == 0
201-
assert res.stdout
202-
assert res.stdout.decode("utf-8")
207+
if dpctl.get_num_devices() > 0:
208+
assert res.stdout
209+
assert res.stdout.decode("utf-8")
203210

204211

205212
def test_main_summary():
206213
res = subprocess.run(
207214
[sys.executable, "-m", "dpctl", "-s"], capture_output=True
208215
)
209216
assert res.returncode == 0
210-
assert res.stdout
211-
assert res.stdout.decode("utf-8")
217+
if dpctl.get_num_devices() > 0:
218+
assert res.stdout
219+
assert res.stdout.decode("utf-8")
212220

213221

214222
def test_main_warnings():
215223
res = subprocess.run(
216224
[sys.executable, "-m", "dpctl", "-s", "--includes"], capture_output=True
217225
)
218226
assert res.returncode == 0
219-
assert res.stdout
227+
assert res.stdout or dpctl.get_num_devices() == 0
220228
assert "UserWarning" in res.stderr.decode("utf-8")
221229
assert "is being ignored." in res.stderr.decode("utf-8")
222230

@@ -225,6 +233,6 @@ def test_main_warnings():
225233
capture_output=True,
226234
)
227235
assert res.returncode == 0
228-
assert res.stdout
236+
assert res.stdout or dpctl.get_num_devices() == 0
229237
assert "UserWarning" in res.stderr.decode("utf-8")
230238
assert "are being ignored." in res.stderr.decode("utf-8")

dpctl/tests/test_sycl_context.py

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,10 @@ def test_address_of():
5858
"""
5959
Test if the address_of method returns an int value.
6060
"""
61-
ctx = dpctl.SyclContext()
61+
try:
62+
ctx = dpctl.SyclContext()
63+
except dpctl.SyclContextCreationError:
64+
pytest.skip("Failed to create context using default constructor")
6265
assert ctx.addressof_ref() is not None
6366
assert isinstance(ctx.addressof_ref(), int)
6467

@@ -85,7 +88,10 @@ def test_context_not_equals2():
8588
Test if comparing a SyclContext object to some random Python object is
8689
correctly handled and returns False.
8790
"""
88-
ctx = dpctl.SyclContext()
91+
try:
92+
ctx = dpctl.SyclContext()
93+
except dpctl.SyclContextCreationError:
94+
pytest.skip("Failed to create context using default constructor")
8995
assert ctx != "some context"
9096

9197

@@ -103,15 +109,21 @@ def test_name():
103109
"""
104110
Test if a __name__ method is defined for SyclContext.
105111
"""
106-
ctx = dpctl.SyclContext()
112+
try:
113+
ctx = dpctl.SyclContext()
114+
except dpctl.SyclContextCreationError:
115+
pytest.skip("Failed to create context using default constructor")
107116
assert ctx.__name__ == "SyclContext"
108117

109118

110119
def test_repr():
111120
"""
112121
Test if a __repr__ method is defined for SyclContext.
113122
"""
114-
ctx = dpctl.SyclContext()
123+
try:
124+
ctx = dpctl.SyclContext()
125+
except dpctl.SyclContextCreationError:
126+
pytest.skip("Failed to create context using default constructor")
115127
assert ctx.__repr__ is not None
116128

117129

@@ -181,20 +193,30 @@ def test_hashing_of_context():
181193
as a dictionary key.
182194
183195
"""
184-
ctx_dict = {dpctl.SyclContext(): "default_context"}
196+
try:
197+
ctx = dpctl.SyclContext()
198+
except dpctl.SyclContextCreationError:
199+
pytest.skip("Failed to create context using default constructor")
200+
ctx_dict = {ctx: "default_context"}
185201
assert ctx_dict
186202

187203

188204
def test_context_repr():
189-
ctx = dpctl.SyclContext()
205+
try:
206+
ctx = dpctl.SyclContext()
207+
except dpctl.SyclContextCreationError:
208+
pytest.skip("Failed to create context using default constructor")
190209
assert type(ctx.__repr__()) is str
191210

192211

193212
def test_cpython_api_SyclContext_GetContextRef():
194213
import ctypes
195214
import sys
196215

197-
ctx = dpctl.SyclContext()
216+
try:
217+
ctx = dpctl.SyclContext()
218+
except dpctl.SyclContextCreationError:
219+
pytest.skip("Failed to create context using default constructor")
198220
mod = sys.modules[ctx.__class__.__module__]
199221
# get capsule storign SyclContext_GetContextRef function ptr
200222
ctx_ref_fn_cap = mod.__pyx_capi__["SyclContext_GetContextRef"]
@@ -217,7 +239,10 @@ def test_cpython_api_SyclContext_Make():
217239
import ctypes
218240
import sys
219241

220-
ctx = dpctl.SyclContext()
242+
try:
243+
ctx = dpctl.SyclContext()
244+
except dpctl.SyclContextCreationError:
245+
pytest.skip("Failed to create context using default constructor")
221246
mod = sys.modules[ctx.__class__.__module__]
222247
# get capsule storign SyclContext_Make function ptr
223248
make_ctx_fn_cap = mod.__pyx_capi__["SyclContext_Make"]
@@ -243,6 +268,8 @@ def test_invalid_capsule():
243268

244269
def test_multi_device_different_platforms():
245270
devs = dpctl.get_devices() # all devices
246-
if len(devs) > 1:
271+
if len(devs) > 1 and len(set(d.sycl_platform for d in devs)) > 1:
247272
with pytest.raises(dpctl.SyclContextCreationError):
248273
dpctl.SyclContext(devs)
274+
else:
275+
pytest.skip("Insufficient amount of available devices for this test")

dpctl/tests/test_sycl_event.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,8 @@ def test_execution_status_nondefault_event():
122122

123123

124124
def test_backend():
125+
if dpctl.get_num_devices() == 0:
126+
pytest.skip("No backends are available")
125127
try:
126128
dpctl.SyclEvent().backend
127129
except ValueError:

0 commit comments

Comments
 (0)