Mock API: special case the SetErrorCallback procs.

Also add a suppression for these procs in wiretests, unless we are
specifically testing them.
This commit is contained in:
Corentin Wallez 2017-05-16 22:05:51 +02:00 committed by Corentin Wallez
parent 0a58812f34
commit 1b7c5e3f70
3 changed files with 91 additions and 14 deletions

View File

@ -23,8 +23,8 @@ namespace {
, {{as_annotated_cType(arg)}} , {{as_annotated_cType(arg)}}
{%- endfor -%} {%- endfor -%}
) { ) {
auto tablePtr = reinterpret_cast<ProcTableAsClass**>(self); auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
return (*tablePtr)->{{as_MethodSuffix(type.name, method.name)}}(self return object->procs->{{as_MethodSuffix(type.name, method.name)}}(self
{%- for arg in method.arguments -%} {%- for arg in method.arguments -%}
, {{as_varName(arg.name)}} , {{as_varName(arg.name)}}
{%- endfor -%} {%- endfor -%}
@ -36,9 +36,6 @@ namespace {
} }
ProcTableAsClass::~ProcTableAsClass() { ProcTableAsClass::~ProcTableAsClass() {
for (auto ptr : selfPtrs) {
delete ptr;
}
} }
void ProcTableAsClass::GetProcTableAndDevice(nxtProcTable* table, nxtDevice* device) { void ProcTableAsClass::GetProcTableAndDevice(nxtProcTable* table, nxtDevice* device) {
@ -51,10 +48,29 @@ void ProcTableAsClass::GetProcTableAndDevice(nxtProcTable* table, nxtDevice* dev
{% endfor %} {% endfor %}
} }
{% for type in by_category["object"] %} void ProcTableAsClass::DeviceSetErrorCallback(nxtDevice self, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata) {
{{as_cType(type.name)}} ProcTableAsClass::GetNew{{type.name.CamelCase()}}() { auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
auto self = new ProcTableAsClass*(this); object->deviceErrorCallback = callback;
selfPtrs.push_back(self); object->userdata1 = userdata;
return reinterpret_cast<{{as_cType(type.name)}}>(self);
this->OnDeviceSetErrorCallback(self, callback, userdata);
}
{% for type in by_category["object"] if type.is_builder %}
void ProcTableAsClass::{{as_MethodSuffix(type.name, Name("set error callback"))}}({{as_cType(type.name)}} self, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2) {
auto object = reinterpret_cast<ProcTableAsClass::Object*>(self);
object->builderErrorCallback = callback;
object->userdata1 = userdata1;
object->userdata2 = userdata2;
this->OnBuilderSetErrorCallback(reinterpret_cast<nxtBufferBuilder>(self), callback, userdata1, userdata2);
}
{% endfor %}
{% for type in by_category["object"] %}
{{as_cType(type.name)}} ProcTableAsClass::GetNew{{type.name.CamelCase()}}() {
objects.emplace_back(new Object);
objects.back()->procs = this;
return reinterpret_cast<{{as_cType(type.name)}}>(objects.back().get());
} }
{% endfor %} {% endfor %}

View File

@ -18,18 +18,25 @@
#include <gmock/gmock.h> #include <gmock/gmock.h>
#include <nxt/nxt.h> #include <nxt/nxt.h>
// An abstract base class representing a proc table so that API calls can be mocked. Most API calls
// are directly represented by a delete virtual method but others need minimal state tracking to be
// useful as mocks.
class ProcTableAsClass { class ProcTableAsClass {
public: public:
virtual ~ProcTableAsClass(); virtual ~ProcTableAsClass();
void GetProcTableAndDevice(nxtProcTable* table, nxtDevice* device); void GetProcTableAndDevice(nxtProcTable* table, nxtDevice* device);
// Creates an object that can be returned by a mocked call as in WillOnce(Return(foo)).
// It returns an object of the write type that isn't equal to any previously returned object.
// Otherwise some mock expectation could be triggered by two different objects having the same
// value.
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{{as_cType(type.name)}} GetNew{{type.name.CamelCase()}}(); {{as_cType(type.name)}} GetNew{{type.name.CamelCase()}}();
{% endfor %} {% endfor %}
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{% for method in native_methods(type) if len(method.arguments) < 10 %} {% for method in type.methods if len(method.arguments) < 10 %}
virtual {{as_cType(method.return_type.name)}} {{as_MethodSuffix(type.name, method.name)}}( virtual {{as_cType(method.return_type.name)}} {{as_MethodSuffix(type.name, method.name)}}(
{{-as_cType(type.name)}} {{as_varName(type.name)}} {{-as_cType(type.name)}} {{as_varName(type.name)}}
{%- for arg in method.arguments -%} {%- for arg in method.arguments -%}
@ -37,17 +44,39 @@ class ProcTableAsClass {
{%- endfor -%} {%- endfor -%}
) = 0; ) = 0;
{% endfor %} {% endfor %}
virtual void {{as_MethodSuffix(type.name, Name("reference"))}}({{as_cType(type.name)}} self) = 0;
virtual void {{as_MethodSuffix(type.name, Name("release"))}}({{as_cType(type.name)}} self) = 0;
// Stores callback and userdata and calls OnBuilderSetErrorCallback
{% if type.is_builder %}
void {{as_MethodSuffix(type.name, Name("set error callback"))}}({{as_cType(type.name)}} self, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2);
{% endif %}
{% endfor %} {% endfor %}
// Stores callback and userdata and calls OnDeviceSetErrorCallback
void DeviceSetErrorCallback(nxtDevice self, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata);
// Special cased mockable methods
virtual void OnDeviceSetErrorCallback(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata) = 0;
virtual void OnBuilderSetErrorCallback(nxtBufferBuilder builder, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2) = 0;
struct Object {
ProcTableAsClass* procs = nullptr;
nxtDeviceErrorCallback deviceErrorCallback = nullptr;
nxtBuilderErrorCallback builderErrorCallback = nullptr;
nxtCallbackUserdata userdata1 = 0;
nxtCallbackUserdata userdata2 = 0;
};
private: private:
std::vector<ProcTableAsClass**> selfPtrs; // Remembers the values returned by GetNew* so they can be freed.
std::vector<std::unique_ptr<Object>> objects;
}; };
class MockProcTable : public ProcTableAsClass { class MockProcTable : public ProcTableAsClass {
public: public:
{% for type in by_category["object"] %} {% for type in by_category["object"] %}
{% for method in native_methods(type) if len(method.arguments) < 10 %} {% for method in type.methods if len(method.arguments) < 10 %}
MOCK_METHOD{{len(method.arguments) + 1}}( MOCK_METHOD{{len(method.arguments) + 1}}(
{{-as_MethodSuffix(type.name, method.name)}}, {{-as_MethodSuffix(type.name, method.name)}},
{{as_cType(method.return_type.name)}}( {{as_cType(method.return_type.name)}}(
@ -58,7 +87,12 @@ class MockProcTable : public ProcTableAsClass {
)); ));
{% endfor %} {% endfor %}
MOCK_METHOD1({{as_MethodSuffix(type.name, Name("reference"))}}, void({{as_cType(type.name)}} self));
MOCK_METHOD1({{as_MethodSuffix(type.name, Name("release"))}}, void({{as_cType(type.name)}} self));
{% endfor %} {% endfor %}
MOCK_METHOD3(OnDeviceSetErrorCallback, void(nxtDevice device, nxtDeviceErrorCallback callback, nxtCallbackUserdata userdata));
MOCK_METHOD4(OnBuilderSetErrorCallback, void(nxtBufferBuilder builder, nxtBuilderErrorCallback callback, nxtCallbackUserdata userdata1, nxtCallbackUserdata userdata2));
}; };
#endif // MOCK_NXT_H #endif // MOCK_NXT_H

View File

@ -18,16 +18,27 @@
#include "wire/TerribleCommandBuffer.h" #include "wire/TerribleCommandBuffer.h"
#include "wire/Wire.h" #include "wire/Wire.h"
#include <iostream>
using namespace testing; using namespace testing;
using namespace nxt::wire; using namespace nxt::wire;
class WireTests : public Test { class WireTestsBase : public Test {
protected: protected:
WireTestsBase(bool ignoreSetCallbackCalls)
: ignoreSetCallbackCalls(ignoreSetCallbackCalls) {
}
void SetUp() override { void SetUp() override {
nxtProcTable mockProcs; nxtProcTable mockProcs;
nxtDevice mockDevice; nxtDevice mockDevice;
api.GetProcTableAndDevice(&mockProcs, &mockDevice); api.GetProcTableAndDevice(&mockProcs, &mockDevice);
if (ignoreSetCallbackCalls) {
EXPECT_CALL(api, OnDeviceSetErrorCallback(_, _, _)).Times(Exactly(1));
EXPECT_CALL(api, OnBuilderSetErrorCallback(_, _, _, _)).Times(AnyNumber());
}
s2cBuf = new TerribleCommandBuffer(); s2cBuf = new TerribleCommandBuffer();
c2sBuf = new TerribleCommandBuffer(wireServer); c2sBuf = new TerribleCommandBuffer(wireServer);
@ -63,12 +74,20 @@ class WireTests : public Test {
nxtDevice device; nxtDevice device;
private: private:
bool ignoreSetCallbackCalls = false;
CommandHandler* wireServer = nullptr; CommandHandler* wireServer = nullptr;
CommandHandler* wireClient = nullptr; CommandHandler* wireClient = nullptr;
TerribleCommandBuffer* s2cBuf = nullptr; TerribleCommandBuffer* s2cBuf = nullptr;
TerribleCommandBuffer* c2sBuf = nullptr; TerribleCommandBuffer* c2sBuf = nullptr;
}; };
class WireTests : public WireTestsBase {
public:
WireTests() : WireTestsBase(true) {
}
};
// One call gets forwarded correctly. // One call gets forwarded correctly.
TEST_F(WireTests, CallForwarded) { TEST_F(WireTests, CallForwarded) {
nxtCommandBufferBuilder builder = nxtDeviceCreateCommandBufferBuilder(device); nxtCommandBufferBuilder builder = nxtDeviceCreateCommandBufferBuilder(device);
@ -189,3 +208,11 @@ TEST_F(WireTests, OneObjectAsPointerArgument) {
// - Test multiple objects as value work // - Test multiple objects as value work
// - Object creation, then calls do nothing after error on builder // - Object creation, then calls do nothing after error on builder
// - Object creation then error then create object, then should do nothing. // - Object creation then error then create object, then should do nothing.
// - Device error gets forwarded properly
// - Builder error
// - An error gets forwarded properly
// - No other call to builder after error
// - No call to object after error
// - No error -> success
// - Builder destroyed on client side -> gets unknown
// - Same for getresult then destroyed object