DawnTest: Make error/device loss mocks StrictMocks

Any unhandled error or device loss in the tests that are not expected
should cause a test failure. Mocks other than StrictMock will not cause
a failure and instead just write to stdout (or not write at all).

Fixed: dawn:1412
Change-Id: I2dd108da8fa6ba2cd7967790de4d7fa4b9f821ae
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/89600
Commit-Queue: Corentin Wallez <cwallez@chromium.org>
Reviewed-by: Austin Eng <enga@chromium.org>
Reviewed-by: Loko Kung <lokokung@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Corentin Wallez 2022-05-10 17:06:34 +00:00 committed by Dawn LUCI CQ
parent 636e3d0111
commit a439e7b89c
2 changed files with 21 additions and 19 deletions

View File

@ -955,10 +955,17 @@ std::pair<wgpu::Device, WGPUDevice> DawnTestBase::CreateDeviceImpl(std::string i
auto devices = mWireHelper->RegisterDevice(mBackendAdapter.CreateDevice(&deviceDescriptor)); auto devices = mWireHelper->RegisterDevice(mBackendAdapter.CreateDevice(&deviceDescriptor));
wgpu::Device device = devices.first; wgpu::Device device = devices.first;
// Set up the mocks for uncaptured errors and device loss. The loss of the device is expected
// to happen at the end of the test so at it directly.
device.SetUncapturedErrorCallback(mDeviceErrorCallback.Callback(), device.SetUncapturedErrorCallback(mDeviceErrorCallback.Callback(),
mDeviceErrorCallback.MakeUserdata(device.Get())); mDeviceErrorCallback.MakeUserdata(device.Get()));
device.SetDeviceLostCallback(mDeviceLostCallback.Callback(), device.SetDeviceLostCallback(mDeviceLostCallback.Callback(),
mDeviceLostCallback.MakeUserdata(device.Get())); mDeviceLostCallback.MakeUserdata(device.Get()));
EXPECT_CALL(mDeviceLostCallback,
Call(WGPUDeviceLostReason_Destroyed, testing::_, device.Get()))
.Times(testing::AtMost(1));
device.SetLoggingCallback( device.SetLoggingCallback(
[](WGPULoggingType type, char const* message, void*) { [](WGPULoggingType type, char const* message, void*) {
switch (type) { switch (type) {
@ -977,6 +984,7 @@ std::pair<wgpu::Device, WGPUDevice> DawnTestBase::CreateDeviceImpl(std::string i
} }
}, },
nullptr); nullptr);
return devices; return devices;
} }
@ -1050,33 +1058,26 @@ void DawnTestBase::TearDown() {
} }
void DawnTestBase::DestroyDevice(wgpu::Device device) { void DawnTestBase::DestroyDevice(wgpu::Device device) {
wgpu::Device resolvedDevice; wgpu::Device resolvedDevice = device;
if (device != nullptr) { if (resolvedDevice == nullptr) {
resolvedDevice = device;
} else {
resolvedDevice = this->device; resolvedDevice = this->device;
} }
EXPECT_CALL(mDeviceLostCallback,
Call(WGPUDeviceLostReason_Destroyed, testing::_, resolvedDevice.Get())) // No expectation is added because the expectations for this kind of destruction is set up as
.Times(1); // soon as the device is created.
resolvedDevice.Destroy(); resolvedDevice.Destroy();
FlushWire();
testing::Mock::VerifyAndClearExpectations(&mDeviceLostCallback);
} }
void DawnTestBase::LoseDeviceForTesting(wgpu::Device device) { void DawnTestBase::LoseDeviceForTesting(wgpu::Device device) {
wgpu::Device resolvedDevice; wgpu::Device resolvedDevice = device;
if (device != nullptr) { if (resolvedDevice == nullptr) {
resolvedDevice = device;
} else {
resolvedDevice = this->device; resolvedDevice = this->device;
} }
EXPECT_CALL(mDeviceLostCallback, EXPECT_CALL(mDeviceLostCallback,
Call(WGPUDeviceLostReason_Undefined, testing::_, resolvedDevice.Get())) Call(WGPUDeviceLostReason_Undefined, testing::_, resolvedDevice.Get()))
.Times(1); .Times(1);
resolvedDevice.LoseForTesting(); resolvedDevice.LoseForTesting();
FlushWire();
testing::Mock::VerifyAndClearExpectations(&mDeviceLostCallback);
} }
std::ostringstream& DawnTestBase::AddBufferExpectation(const char* file, std::ostringstream& DawnTestBase::AddBufferExpectation(const char* file,

View File

@ -351,10 +351,11 @@ class DawnTestBase {
size_t mLastWarningCount = 0; size_t mLastWarningCount = 0;
// Mock callbacks tracking errors and destruction. Device lost is a nice mock since tests that // Mock callbacks tracking errors and destruction. These are strict mocks because any errors or
// do not care about device destruction can ignore the callback entirely. // device loss that aren't expected should result in test failures and not just some warnings
testing::MockCallback<WGPUErrorCallback> mDeviceErrorCallback; // printed to stdout.
testing::MockCallback<WGPUDeviceLostCallback> mDeviceLostCallback; testing::StrictMock<testing::MockCallback<WGPUErrorCallback>> mDeviceErrorCallback;
testing::StrictMock<testing::MockCallback<WGPUDeviceLostCallback>> mDeviceLostCallback;
// Helper methods to implement the EXPECT_ macros // Helper methods to implement the EXPECT_ macros
std::ostringstream& AddBufferExpectation(const char* file, std::ostringstream& AddBufferExpectation(const char* file,