Skip to content

Commit

Permalink
tests: Ensure that Com<*> mocks are kept alive until reset
Browse files Browse the repository at this point in the history
  • Loading branch information
jp7677 committed Aug 21, 2024
1 parent bf30536 commit 36affbe
Show file tree
Hide file tree
Showing 10 changed files with 84 additions and 49 deletions.
6 changes: 2 additions & 4 deletions tests/mock_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ class MockFactory : public dxvk::ResourceFactory {
return std::move(m_lfxMock);
}

[[nodiscard]] std::array<std::unique_ptr<expectation>, 1> ConfigureAllowRelease() {
return {
NAMED_ALLOW_CALL(*m_dxgiFactoryMock, Release())
.RETURN(0)};
[[nodiscard]] DXGIDxvkFactoryMock* GetDXGIDxvkFactoryMock() {
return m_dxgiFactoryMock.get();
}

private:
Expand Down
6 changes: 3 additions & 3 deletions tests/nvapi_d3d.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,11 @@ TEST_CASE("D3D Reflex/LatencyFleX depending methods succeed", "[.d3d]") {
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
D3DLowLatencyDeviceMock lowLatencyDevice;
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();
auto lowLatencyDeviceRefCount = 0;

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);

ALLOW_CALL(unknown, QueryInterface(__uuidof(ID3DLowLatencyDevice), _))
.RETURN(E_NOINTERFACE);
Expand Down
6 changes: 3 additions & 3 deletions tests/nvapi_d3d11.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -441,10 +441,10 @@ TEST_CASE("D3D11 MultiGPU methods succeed", "[.d3d11]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);

SetupResourceFactory(std::move(dxgiFactory), std::move(vulkan), std::move(nvml), std::move(lfx));
REQUIRE(NvAPI_Initialize() == NVAPI_OK);
Expand Down
12 changes: 6 additions & 6 deletions tests/nvapi_d3d12.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,11 @@ TEST_CASE("D3D12 methods succeed", "[.d3d12]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();
LUID luid{};

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);

ALLOW_CALL(device, QueryInterface(__uuidof(ID3D12Device), _))
.LR_SIDE_EFFECT(*_2 = static_cast<ID3D12Device*>(&device))
Expand Down Expand Up @@ -802,10 +802,10 @@ TEST_CASE("D3D12 methods succeed", "[.d3d12]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);

ALLOW_CALL(commandQueue, GetDevice(__uuidof(ID3DLowLatencyDevice), _))
.LR_SIDE_EFFECT(*_2 = static_cast<ID3DLowLatencyDevice*>(&lowLatencyDevice))
Expand Down
30 changes: 15 additions & 15 deletions tests/nvapi_sysinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ TEST_CASE("Initialize succeeds", "[.sysinfo]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);

SECTION("Initialize returns OK") {
SetupResourceFactory(std::move(dxgiFactory), std::move(vulkan), std::move(nvml), std::move(lfx));
Expand All @@ -45,7 +45,7 @@ TEST_CASE("Initialize succeeds", "[.sysinfo]") {
SECTION("Initialize returns device-not-found when DXVK NVAPI hack is enabled") {
::SetEnvironmentVariableA("DXVK_ENABLE_NVAPI", "0");

ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT(_1->VendorId = 0x1002)
.RETURN(S_OK);

Expand All @@ -64,7 +64,7 @@ TEST_CASE("Initialize succeeds", "[.sysinfo]") {

::SetEnvironmentVariableA("DXVK_ENABLE_NVAPI", "1");

ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT(_1->VendorId = 0x1002)
.RETURN(S_OK);
ALLOW_CALL(*vulkan, GetPhysicalDeviceProperties2(_, _, _))
Expand Down Expand Up @@ -112,10 +112,10 @@ TEST_CASE("Sysinfo methods succeed", "[.sysinfo]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);
auto primaryDisplayId = 0x00010001;

SECTION("Initialize and unloads return OK") {
Expand Down Expand Up @@ -382,7 +382,7 @@ TEST_CASE("Sysinfo methods succeed", "[.sysinfo]") {
}

SECTION("GetPCIIdentifiers returns OK") {
ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT({
_1->VendorId = 0x10de;
_1->DeviceId = 0x1234;
Expand Down Expand Up @@ -502,7 +502,7 @@ TEST_CASE("Sysinfo methods succeed", "[.sysinfo]") {
}

SECTION("GetPhysicalFrameBufferSize returns OK") {
ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT({
_1->VendorId = 0x10de;
_1->DedicatedVideoMemory = 8191 * 1024;
Expand All @@ -521,7 +521,7 @@ TEST_CASE("Sysinfo methods succeed", "[.sysinfo]") {
}

SECTION("GetVirtualFrameBufferSize returns OK") {
ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT({
_1->VendorId = 0x10de;
_1->DedicatedVideoMemory = 8191 * 1024;
Expand All @@ -541,15 +541,15 @@ TEST_CASE("Sysinfo methods succeed", "[.sysinfo]") {
}

SECTION("GetMemoryInfo succeeds") {
ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT({
_1->VendorId = 0x10de;
_1->DedicatedVideoMemory = 8191 * 1024;
_1->DedicatedSystemMemory = 1014 * 1024;
_1->SharedSystemMemory = 16382 * 1024;
})
.RETURN(S_OK);
ALLOW_CALL(adapter, QueryVideoMemoryInfo(_, _, _))
ALLOW_CALL(*adapter, QueryVideoMemoryInfo(_, _, _))
.SIDE_EFFECT({
_3->Budget = 4096 * 1024;
_3->CurrentUsage = 1024 * 1024;
Expand Down Expand Up @@ -610,15 +610,15 @@ TEST_CASE("Sysinfo methods succeed", "[.sysinfo]") {
}

SECTION("GetMemoryInfoEx succeeds") {
ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT({
_1->VendorId = 0x10de;
_1->DedicatedVideoMemory = 8191 * 1024;
_1->DedicatedSystemMemory = 1014 * 1024;
_1->SharedSystemMemory = 16382 * 1024;
})
.RETURN(S_OK);
ALLOW_CALL(adapter, QueryVideoMemoryInfo(_, _, _))
ALLOW_CALL(*adapter, QueryVideoMemoryInfo(_, _, _))
.SIDE_EFFECT({
_3->Budget = 4096 * 1024;
_3->CurrentUsage = 1024 * 1024;
Expand Down
12 changes: 6 additions & 6 deletions tests/nvapi_sysinfo_hdr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ TEST_CASE("HDR related sysinfo methods succeed", "[.sysinfo-hdr]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);
auto primaryDisplayId = 0x00010001;

auto desc1 = DXGI_OUTPUT_DESC1{
Expand All @@ -32,10 +32,10 @@ TEST_CASE("HDR related sysinfo methods succeed", "[.sysinfo-hdr]") {
1499.0f,
799.0f};

ALLOW_CALL(output, QueryInterface(__uuidof(IDXGIOutput6), _))
.LR_SIDE_EFFECT(*_2 = static_cast<IDXGIOutput6*>(&output))
ALLOW_CALL(*output, QueryInterface(__uuidof(IDXGIOutput6), _))
.LR_SIDE_EFFECT(*_2 = static_cast<IDXGIOutput6*>(output))
.RETURN(S_OK);
ALLOW_CALL(output, GetDesc1(_))
ALLOW_CALL(*output, GetDesc1(_))
.SIDE_EFFECT(*_1 = desc1)
.RETURN(S_OK);

Expand Down
10 changes: 5 additions & 5 deletions tests/nvapi_sysinfo_nvml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ TEST_CASE("NVML related sysinfo methods succeed", "[.sysinfo-nvml]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter;
DXGIOutput6Mock output;
DXGIDxvkAdapterMock* adapter = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output = CreateDXGIOutput6Mock();

auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter, output);
auto e = ConfigureDefaultTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter, *output);
auto primaryDisplayId = 0x00010001;

SECTION("NVML depending methods succeed when NVML is available") {
Expand Down Expand Up @@ -112,13 +112,13 @@ TEST_CASE("NVML related sysinfo methods succeed", "[.sysinfo-nvml]") {
}

SECTION("GetMemoryInfo/GetMemoryInfoEx returns OK") {
ALLOW_CALL(adapter, GetDesc1(_))
ALLOW_CALL(*adapter, GetDesc1(_))
.SIDE_EFFECT({
_1->VendorId = 0x10de;
_1->DedicatedVideoMemory = 8191 * 1024;
})
.RETURN(S_OK);
ALLOW_CALL(adapter, QueryVideoMemoryInfo(_, _, _))
ALLOW_CALL(*adapter, QueryVideoMemoryInfo(_, _, _))
.RETURN(S_OK);

SetupResourceFactory(std::move(dxgiFactory), std::move(vulkan), std::move(nvml), std::move(lfx));
Expand Down
12 changes: 6 additions & 6 deletions tests/nvapi_sysinfo_topo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ TEST_CASE("Topology methods succeed", "[.sysinfo-topo]") {
auto vulkan = std::make_unique<VulkanMock>();
auto nvml = std::make_unique<NvmlMock>();
auto lfx = std::make_unique<LfxMock>();
DXGIDxvkAdapterMock adapter1;
DXGIDxvkAdapterMock adapter2;
DXGIOutput6Mock output1;
DXGIOutput6Mock output2;
DXGIOutput6Mock output3;
DXGIDxvkAdapterMock* adapter1 = CreateDXGIDxvkAdapterMock();
DXGIDxvkAdapterMock* adapter2 = CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* output1 = CreateDXGIOutput6Mock();
DXGIOutput6Mock* output2 = CreateDXGIOutput6Mock();
DXGIOutput6Mock* output3 = CreateDXGIOutput6Mock();

auto e = ConfigureExtendedTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, adapter1, adapter2, output1, output2, output3);
auto e = ConfigureExtendedTestEnvironment(*dxgiFactory, *vulkan, *nvml, *lfx, *adapter1, *adapter2, *output1, *output2, *output3);

SetupResourceFactory(std::move(dxgiFactory), std::move(vulkan), std::move(nvml), std::move(lfx));
REQUIRE(NvAPI_Initialize() == NVAPI_OK);
Expand Down
36 changes: 35 additions & 1 deletion tests/resource_factory_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,19 @@
using namespace trompeloeil;
using namespace dxvk;

std::vector<std::unique_ptr<DXGIDxvkAdapterMock>> m_dxgiAdapterMocks;
std::vector<std::unique_ptr<DXGIOutput6Mock>> m_dxgiOutputMocks;

DXGIDxvkAdapterMock* CreateDXGIDxvkAdapterMock() {
auto mock = std::make_unique<DXGIDxvkAdapterMock>();
return m_dxgiAdapterMocks.emplace_back(std::move(mock)).get();
}

DXGIOutput6Mock* CreateDXGIOutput6Mock() {
auto mock = std::make_unique<DXGIOutput6Mock>();
return m_dxgiOutputMocks.emplace_back(std::move(mock)).get();
}

void SetupResourceFactory(
std::unique_ptr<DXGIDxvkFactoryMock> dxgiFactory,
std::unique_ptr<VulkanMock> vulkan,
Expand All @@ -20,11 +33,32 @@ void ResetGlobals() {
return;

auto mockFactory = reinterpret_cast<MockFactory*>(resourceFactory.get());
auto e = mockFactory->ConfigureAllowRelease(); // Ensure that Com<*> mocks can be deleted by destructors

// Ensure that Com<*> mocks can be deleted by destructors
std::vector<std::unique_ptr<expectation>> e;

e.emplace_back(
NAMED_ALLOW_CALL(*mockFactory->GetDXGIDxvkFactoryMock(), Release()).RETURN(0));

std::for_each(m_dxgiAdapterMocks.begin(), m_dxgiAdapterMocks.end(),
[&e](auto& mock) {
e.emplace_back(
NAMED_ALLOW_CALL(*mock, Release()).RETURN(0));
});

std::for_each(m_dxgiOutputMocks.begin(), m_dxgiOutputMocks.end(),
[&e](auto& mock) {
e.emplace_back(
NAMED_ALLOW_CALL(*mock, Release()).RETURN(0));
});

nvapiD3dInstance.reset();
nvapiAdapterRegistry.reset();
initializationCount = 0ULL;
resourceFactory.reset();

m_dxgiAdapterMocks.clear();
m_dxgiOutputMocks.clear();
}

[[nodiscard]] std::array<std::unique_ptr<expectation>, 24> ConfigureDefaultTestEnvironment(
Expand Down
3 changes: 3 additions & 0 deletions tests/resource_factory_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
#include "nvapi_sysinfo_mocks.h"
#include "mock_factory.h"

DXGIDxvkAdapterMock* CreateDXGIDxvkAdapterMock();
DXGIOutput6Mock* CreateDXGIOutput6Mock();

void SetupResourceFactory(
std::unique_ptr<DXGIDxvkFactoryMock> dxgiFactory,
std::unique_ptr<VulkanMock> vulkan,
Expand Down

0 comments on commit 36affbe

Please sign in to comment.