CodeQL / Analyze (actions) (push) Canceled after 0s
CodeQL / Analyze (python) (push) Canceled after 0s
pre-commit / pre-commit (push) Canceled after 0s
Multi-Arch CI / setup (push) Canceled after 0s
Publish build_manylinux_x86_64 images / publish_build_manylinux_x86_64 (push) Canceled after 0s
Setup (Multi-Arch) / setup (push) Canceled after 0s
Multi-Arch CI / Linux::${{ fromJSON(needs.setup.outputs.linux_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubi10 image / publish_no_rocm_image_ubi10 (push) Canceled after 0s
Multi-Arch CI / Windows::${{ fromJSON(needs.setup.outputs.windows_build_config || '{}').build_variant_label || 'skip' }} (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Multi-Arch CI / Manifest Diff (push) Canceled after 0s
TheRock Manifest Diff Report / Generate Manifest Diff Report (push) Canceled after 0s
Multi-Arch CI / CI Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_ocl_rt images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04_rocgdb images / publish_no_rocm_image_ubuntu24_04_rocgdb (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_ocl_rt (push) Canceled after 0s
Publish TheRock Docker image / build-and-push-image (push) Canceled after 0s
Unit Tests / Unit Tests :: ubuntu-24.04 (push) Canceled after 0s
Unit Tests / Unit Tests :: windows-2022 (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_media (push) Canceled after 0s
Unit Tests / Unit Tests Summary (push) Canceled after 0s
Publish no_rocm_image_ubuntu24_04 images / publish_no_rocm_image_ubuntu24_04_openmpi (push) Canceled after 0s
116 lines
3.6 KiB
Python
116 lines
3.6 KiB
Python
# Copyright (c) Advanced Micro Devices, Inc. All rights reserved.
|
|
#
|
|
# SPDX-License-Identifier: MIT
|
|
|
|
"""Tests for hipfile.buffer.Buffer."""
|
|
|
|
# pylint: disable=missing-function-docstring,redefined-outer-name,protected-access
|
|
# pylint: disable=import-error # hipfile not installed in lint env; extension faked
|
|
|
|
from unittest import mock
|
|
|
|
import pytest
|
|
|
|
import hipfile.buffer
|
|
from hipfile.buffer import Buffer
|
|
from hipfile.enums import OpError
|
|
from hipfile.error import HipFileException
|
|
|
|
_SUCCESS = (OpError.SUCCESS, 0)
|
|
_FAILURE = (OpError.MEMORY_ALREADY_REGISTERED, 0)
|
|
|
|
|
|
def test_from_ctypes_void_p_rejects_null(fake_void_p):
|
|
with pytest.raises(ValueError):
|
|
Buffer.from_ctypes_void_p(fake_void_p(None), 1024, 0)
|
|
|
|
|
|
def test_from_ctypes_void_p_forwards_value(fake_void_p):
|
|
buf = Buffer.from_ctypes_void_p(fake_void_p(0xDEAD), 1024, 0)
|
|
assert buf.ptr == 0xDEAD
|
|
|
|
|
|
def test_register_success():
|
|
buf = Buffer(0x1000, 2048, 0)
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufRegister", autospec=True, return_value=_SUCCESS
|
|
) as reg:
|
|
buf.register()
|
|
reg.assert_called_once_with(0x1000, 2048, 0)
|
|
assert buf._registered is True
|
|
|
|
|
|
def test_register_raises_on_error():
|
|
buf = Buffer(0x1000, 2048, 0)
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufRegister", autospec=True, return_value=_FAILURE
|
|
):
|
|
with pytest.raises(HipFileException):
|
|
buf.register()
|
|
assert buf._registered is False
|
|
|
|
|
|
def test_deregister_success():
|
|
buf = Buffer(0x1000, 2048, 0)
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufRegister", autospec=True, return_value=_SUCCESS
|
|
):
|
|
buf.register()
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufDeregister", autospec=True, return_value=_SUCCESS
|
|
) as dereg:
|
|
buf.deregister()
|
|
dereg.assert_called_once_with(0x1000)
|
|
assert buf._registered is False
|
|
|
|
|
|
def test_deregister_is_noop_when_not_registered():
|
|
buf = Buffer(0x1000, 2048, 0)
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufDeregister", autospec=True, return_value=_SUCCESS
|
|
) as dereg:
|
|
buf.deregister()
|
|
dereg.assert_not_called()
|
|
|
|
|
|
def test_deregister_is_idempotent_after_register():
|
|
buf = Buffer(0x1000, 2048, 0)
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufRegister", autospec=True, return_value=_SUCCESS
|
|
):
|
|
buf.register()
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufDeregister", autospec=True, return_value=_SUCCESS
|
|
) as dereg:
|
|
buf.deregister()
|
|
buf.deregister()
|
|
# Second call is a no-op: the extension is only invoked once.
|
|
dereg.assert_called_once_with(0x1000)
|
|
|
|
|
|
def test_deregister_raises_on_error():
|
|
buf = Buffer(0x1000, 2048, 0)
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufRegister", autospec=True, return_value=_SUCCESS
|
|
):
|
|
buf.register()
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufDeregister", autospec=True, return_value=_FAILURE
|
|
):
|
|
with pytest.raises(HipFileException):
|
|
buf.deregister()
|
|
|
|
|
|
def test_context_manager_registers_then_deregisters():
|
|
buf = Buffer(0x1000, 2048, 0)
|
|
with mock.patch.object(
|
|
hipfile.buffer, "hipFileBufRegister", autospec=True, return_value=_SUCCESS
|
|
) as reg, mock.patch.object(
|
|
hipfile.buffer, "hipFileBufDeregister", autospec=True, return_value=_SUCCESS
|
|
) as dereg:
|
|
with buf as entered:
|
|
assert entered is buf
|
|
reg.assert_called_once_with(0x1000, 2048, 0)
|
|
dereg.assert_not_called()
|
|
dereg.assert_called_once_with(0x1000)
|