From 1178256ae9e2eba04f0bfca136d2355276a8961f Mon Sep 17 00:00:00 2001 From: "Thiago Marcos P. Santos" Date: Thu, 15 Jun 2017 16:36:01 +0300 Subject: [PATCH] [tests] Port the AsyncTask test to the actor model --- test/util/async_task.test.cpp | 59 +++++++++++++++++------------------ 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/test/util/async_task.test.cpp b/test/util/async_task.test.cpp index 78dc79dd1..f3025e895 100644 --- a/test/util/async_task.test.cpp +++ b/test/util/async_task.test.cpp @@ -1,9 +1,12 @@ #include #include -#include +#include +#include #include +#include +#include #include using namespace mbgl::util; @@ -29,6 +32,10 @@ public: cb(); } + void sync(std::promise barrier) { + barrier.set_value(); + } + private: AsyncTask *async; }; @@ -94,23 +101,24 @@ TEST(AsyncTask, DestroyAfterSignaling) { TEST(AsyncTask, RequestCoalescingMultithreaded) { RunLoop loop; - unsigned count = 0; + unsigned count = 0, numThreads = 25; AsyncTask async([&count] { ++count; }); - std::vector>> threads; - ThreadContext context = {"Test"}; + mbgl::ThreadPool threads(numThreads); + auto mailbox = std::make_shared(threads); + + TestWorker worker(&async); + mbgl::ActorRef workerRef(worker, mailbox); - unsigned numThreads = 25; for (unsigned i = 0; i < numThreads; ++i) { - std::unique_ptr> thread = - std::make_unique>(context, &async); - - thread->invoke(&TestWorker::run); - threads.push_back(std::move(thread)); + workerRef.invoke(&TestWorker::run); } - // Join all the threads - threads.clear(); + std::promise barrier; + std::future barrierFuture = barrier.get_future(); + + workerRef.invoke(&TestWorker::sync, std::move(barrier)); + barrierFuture.wait(); loop.runOnce(); @@ -120,29 +128,20 @@ TEST(AsyncTask, RequestCoalescingMultithreaded) { TEST(AsyncTask, ThreadSafety) { RunLoop loop; - unsigned count = 0; + unsigned count = 0, numThreads = 25; + std::atomic_uint completed(numThreads); + AsyncTask async([&count] { ++count; }); - unsigned numThreads = 25; + mbgl::ThreadPool threads(numThreads); + auto mailbox = std::make_shared(threads); - auto callback = [&] { - if (!--numThreads) { - loop.stop(); - } - }; - - std::vector>> threads; - std::vector> requests; - ThreadContext context = {"Test"}; + TestWorker worker(&async); + mbgl::ActorRef workerRef(worker, mailbox); for (unsigned i = 0; i < numThreads; ++i) { - std::unique_ptr> thread = - std::make_unique>(context, &async); - - requests.push_back( - thread->invokeWithCallback(&TestWorker::runWithCallback, callback)); - - threads.push_back(std::move(thread)); + // The callback runs on the worker, thus the atomic type. + workerRef.invoke(&TestWorker::runWithCallback, [&] { if (!--completed) loop.stop(); }); } loop.run();