[tests] Fix -Werror=shadow issues in the unit tests

This commit is contained in:
Thiago Marcos P. Santos
2019-12-12 14:43:07 +02:00
parent 91ba9b655e
commit 7dad386f46
4 changed files with 85 additions and 124 deletions

View File

@@ -14,32 +14,26 @@ using namespace mbgl;
using namespace std::chrono_literals;
TEST(Actor, Construction) {
struct Test {
Test(ActorRef<Test>, bool& constructed) {
constructed = true;
};
struct TestActor {
TestActor(ActorRef<TestActor>, bool& constructed) { constructed = true; };
};
bool constructed = false;
Actor<Test> test(Scheduler::GetBackground(), std::ref(constructed));
Actor<TestActor> test(Scheduler::GetBackground(), std::ref(constructed));
EXPECT_TRUE(constructed);
}
TEST(Actor, Destruction) {
struct Test {
Test(ActorRef<Test>, bool& destructed_) : destructed(destructed_) {};
~Test() {
destructed = true;
}
struct TestActor {
TestActor(ActorRef<TestActor>, bool& destructed_) : destructed(destructed_){};
~TestActor() { destructed = true; }
bool& destructed;
};
bool destructed = false;
{
Actor<Test> test(Scheduler::GetBackground(), std::ref(destructed));
}
{ Actor<TestActor> test(Scheduler::GetBackground(), std::ref(destructed)); }
EXPECT_TRUE(destructed);
}
@@ -47,20 +41,15 @@ TEST(Actor, Destruction) {
TEST(Actor, DestructionBlocksOnReceive) {
// Destruction blocks until the actor is not receiving.
struct Test {
struct TestActor {
std::promise<void> promise;
std::future<void> future;
std::atomic<bool> waited;
Test(ActorRef<Test>, std::promise<void> promise_, std::future<void> future_)
: promise(std::move(promise_)),
future(std::move(future_)),
waited(false) {
}
TestActor(ActorRef<TestActor>, std::promise<void> promise_, std::future<void> future_)
: promise(std::move(promise_)), future(std::move(future_)), waited(false) {}
~Test() {
EXPECT_TRUE(waited.load());
}
~TestActor() { EXPECT_TRUE(waited.load()); }
void wait() {
promise.set_value();
@@ -76,9 +65,9 @@ TEST(Actor, DestructionBlocksOnReceive) {
std::promise<void> exitingPromise;
std::future<void> exitingFuture = exitingPromise.get_future();
Actor<Test> test(Scheduler::GetBackground(), std::move(enteredPromise), std::move(exitingFuture));
Actor<TestActor> test(Scheduler::GetBackground(), std::move(enteredPromise), std::move(exitingFuture));
test.self().invoke(&Test::wait);
test.self().invoke(&TestActor::wait);
enteredFuture.wait();
exitingPromise.set_value();
}
@@ -111,8 +100,8 @@ TEST(Actor, DestructionBlocksOnSend) {
mapbox::base::WeakPtr<Scheduler> makeWeakPtr() override { return weakFactory.makeWeakPtr(); }
};
struct Test {
Test(ActorRef<Test>) {}
struct TestActor {
TestActor(ActorRef<TestActor>) {}
void message() {}
};
@@ -123,14 +112,9 @@ TEST(Actor, DestructionBlocksOnSend) {
std::future<void> exitingFuture = exitingPromise.get_future();
auto scheduler = std::make_unique<TestScheduler>(std::move(enteredPromise), std::move(exitingFuture));
auto actor = std::make_unique<Actor<Test>>(*scheduler);
auto actor = std::make_unique<Actor<TestActor>>(*scheduler);
std::thread thread {
[] (ActorRef<Test> ref) {
ref.invoke(&Test::message);
},
actor->self()
};
std::thread thread{[](ActorRef<TestActor> ref) { ref.invoke(&TestActor::message); }, actor->self()};
enteredFuture.wait();
exitingPromise.set_value();
@@ -146,9 +130,8 @@ TEST(Actor, DestructionAllowedInReceiveOnSameThread) {
// thread as receive(). This prevents deadlocks and
// allows for self-closing actors
struct Test {
Test(ActorRef<Test>){};
struct TestActor {
TestActor(ActorRef<TestActor>){};
void callMeBack(std::function<void ()> callback) {
callback();
@@ -157,10 +140,10 @@ TEST(Actor, DestructionAllowedInReceiveOnSameThread) {
std::promise<void> callbackFiredPromise;
std::shared_ptr<Scheduler> retainer = Scheduler::GetBackground();
auto test = std::make_unique<Actor<Test>>(retainer);
auto test = std::make_unique<Actor<TestActor>>(retainer);
// Callback (triggered while mutex is locked in Mailbox::receive())
test->self().invoke(&Test::callMeBack, [&]() {
test->self().invoke(&TestActor::callMeBack, [&]() {
// Destroy the Actor/Mailbox in the same thread
test.reset();
callbackFiredPromise.set_value();
@@ -175,36 +158,31 @@ TEST(Actor, SelfDestructionDoesntCrashWaitingReceivingThreads) {
// crash when a actor closes it's own mailbox from a
// callback
struct Test {
Test(ActorRef<Test>){};
struct TestActor {
TestActor(ActorRef<TestActor>){};
void callMeBack(std::function<void ()> callback) {
callback();
}
};
std::promise<void> actorClosedPromise;
auto closingActor = std::make_unique<Actor<Test>>(Scheduler::GetBackground());
auto waitingActor = std::make_unique<Actor<Test>>(Scheduler::GetBackground());
auto closingActor = std::make_unique<Actor<TestActor>>(Scheduler::GetBackground());
auto waitingActor = std::make_unique<Actor<TestActor>>(Scheduler::GetBackground());
std::atomic<bool> waitingMessageProcessed {false};
// Callback (triggered while mutex is locked in Mailbox::receive())
closingActor->self().invoke(&Test::callMeBack, [&]() {
closingActor->self().invoke(&TestActor::callMeBack, [&]() {
// Queue up another message from another thread
std::promise<void> messageQueuedPromise;
waitingActor->self().invoke(&Test::callMeBack, [&]() {
waitingActor->self().invoke(&TestActor::callMeBack, [&]() {
// This will be waiting on the mutex in
// Mailbox::receive(), holding a lock
// on the weak_ptr so the mailbox is not
// destroyed
closingActor->self().invoke(&Test::callMeBack, [&]() {
waitingMessageProcessed.store(true);
});
closingActor->self().invoke(&TestActor::callMeBack, [&]() { waitingMessageProcessed.store(true); });
messageQueuedPromise.set_value();
});
@@ -227,13 +205,11 @@ TEST(Actor, SelfDestructionDoesntCrashWaitingReceivingThreads) {
TEST(Actor, OrderedMailbox) {
// Messages are processed in order.
struct Test {
struct TestActor {
int last = 0;
std::promise<void> promise;
Test(ActorRef<Test>, std::promise<void> promise_)
: promise(std::move(promise_)) {
}
TestActor(ActorRef<TestActor>, std::promise<void> promise_) : promise(std::move(promise_)) {}
void receive(int i) {
EXPECT_EQ(i, last + 1);
@@ -247,26 +223,24 @@ TEST(Actor, OrderedMailbox) {
std::promise<void> endedPromise;
std::future<void> endedFuture = endedPromise.get_future();
Actor<Test> test(Scheduler::GetBackground(), std::move(endedPromise));
Actor<TestActor> test(Scheduler::GetBackground(), std::move(endedPromise));
for (auto i = 1; i <= 10; ++i) {
test.self().invoke(&Test::receive, i);
test.self().invoke(&TestActor::receive, i);
}
test.self().invoke(&Test::end);
test.self().invoke(&TestActor::end);
endedFuture.wait();
}
TEST(Actor, NonConcurrentMailbox) {
// An individual actor is never itself concurrent.
struct Test {
struct TestActor {
int last = 0;
std::promise<void> promise;
Test(ActorRef<Test>, std::promise<void> promise_)
: promise(std::move(promise_)) {
}
TestActor(ActorRef<TestActor>, std::promise<void> promise_) : promise(std::move(promise_)) {}
void receive(int i) {
EXPECT_EQ(i, last + 1);
@@ -281,31 +255,30 @@ TEST(Actor, NonConcurrentMailbox) {
std::promise<void> endedPromise;
std::future<void> endedFuture = endedPromise.get_future();
Actor<Test> test(Scheduler::GetBackground(), std::move(endedPromise));
Actor<TestActor> test(Scheduler::GetBackground(), std::move(endedPromise));
for (auto i = 1; i <= 10; ++i) {
test.self().invoke(&Test::receive, i);
test.self().invoke(&TestActor::receive, i);
}
test.self().invoke(&Test::end);
test.self().invoke(&TestActor::end);
endedFuture.wait();
}
TEST(Actor, Ask) {
// Asking for a result
struct Test {
Test(ActorRef<Test>) {}
struct TestActor {
TestActor(ActorRef<TestActor>) {}
int doubleIt(int i) {
return i * 2;
}
};
Actor<Test> test(Scheduler::GetBackground());
Actor<TestActor> test(Scheduler::GetBackground());
auto result = test.self().ask(&Test::doubleIt, 1);
auto result = test.self().ask(&TestActor::doubleIt, 1);
ASSERT_TRUE(result.valid());
@@ -317,11 +290,10 @@ TEST(Actor, Ask) {
TEST(Actor, AskVoid) {
// Ask waits for void methods
struct Test {
struct TestActor {
bool& executed;
Test(bool& executed_) : executed(executed_) {
}
TestActor(bool& executed_) : executed(executed_) {}
void doIt() {
executed = true;
@@ -329,9 +301,9 @@ TEST(Actor, AskVoid) {
};
bool executed = false;
Actor<Test> actor(Scheduler::GetBackground(), executed);
Actor<TestActor> actor(Scheduler::GetBackground(), executed);
actor.self().ask(&Test::doIt).get();
actor.self().ask(&TestActor::doIt).get();
EXPECT_TRUE(executed);
}
@@ -370,13 +342,10 @@ TEST(Actor, TwoPhaseConstruction) {
// its actor in two parts so that the Thread<Object> instance can be created without waiting
// for the target thread to be up and running.
struct Test {
Test(ActorRef<Test>, std::shared_ptr<bool> destroyed_)
: destroyed(std::move(destroyed_)) {};
struct TestActor {
TestActor(ActorRef<TestActor>, std::shared_ptr<bool> destroyed_) : destroyed(std::move(destroyed_)){};
~Test() {
*destroyed = true;
}
~TestActor() { *destroyed = true; }
void callMe(std::promise<void> p) {
p.set_value();
@@ -389,22 +358,22 @@ TEST(Actor, TwoPhaseConstruction) {
std::shared_ptr<bool> destroyed;
};
AspiringActor<Test> parent;
AspiringActor<TestActor> parent;
auto destroyed = std::make_shared<bool>(false);
std::promise<void> queueExecuted;
auto queueExecutedFuture = queueExecuted.get_future();
parent.self().invoke(&Test::callMe, std::move(queueExecuted));
parent.self().invoke(&Test::stop);
parent.self().invoke(&TestActor::callMe, std::move(queueExecuted));
parent.self().invoke(&TestActor::stop);
auto thread = std::thread([
capturedArgs = std::make_tuple(destroyed),
&parent
] () mutable {
util::RunLoop loop(util::RunLoop::Type::New);
EstablishedActor<Test> test(loop, parent, capturedArgs);
EstablishedActor<TestActor> test(loop, parent, capturedArgs);
loop.run();
});