diff options
author | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-11-26 16:49:29 +0200 |
---|---|---|
committer | Thiago Marcos P. Santos <thiago@mapbox.com> | 2015-12-01 00:00:09 +0200 |
commit | 6762cd0ca90aaafa81eab3a638fee1d1eb391b74 (patch) | |
tree | 8f9ac697baf3309d07106587e05dbed473af3b9b /test | |
parent | 123c5df92219bde3dda39d3ba942b996e765e8e9 (diff) | |
download | qtlocation-mapboxgl-6762cd0ca90aaafa81eab3a638fee1d1eb391b74.tar.gz |
[tests] Add utests for AsyncTask
Diffstat (limited to 'test')
-rw-r--r-- | test/miscellaneous/async_task.cpp | 129 | ||||
-rw-r--r-- | test/test.gypi | 1 |
2 files changed, 130 insertions, 0 deletions
diff --git a/test/miscellaneous/async_task.cpp b/test/miscellaneous/async_task.cpp new file mode 100644 index 0000000000..4873ec5afc --- /dev/null +++ b/test/miscellaneous/async_task.cpp @@ -0,0 +1,129 @@ +#include <mbgl/util/async_task.hpp> +#include <mbgl/util/run_loop.hpp> +#include <mbgl/util/thread.hpp> + +#include "../fixtures/util.hpp" + +#include <vector> + +using namespace mbgl::util; + +namespace { + +class TestWorker { +public: + TestWorker(AsyncTask *async_) + : async(async_) {} + + void run() { + for (unsigned i = 0; i < 100000; ++i) { + async->send(); + } + } + + void runWithCallback(std::function<void()> cb) { + for (unsigned i = 0; i < 100000; ++i) { + async->send(); + } + + cb(); + } + +private: + AsyncTask *async; +}; + +} + +TEST(AsyncTask, RequestCoalescing) { + RunLoop loop; + + unsigned count = 0; + AsyncTask async([&count] { ++count; }); + async.unref(); + + async.send(); + async.send(); + async.send(); + async.send(); + async.send(); + + loop.runOnce(); + + EXPECT_EQ(count, 1); +} + +TEST(AsyncTask, DestroyShouldNotRunQueue) { + RunLoop loop; + + unsigned count = 0; + auto async = std::make_unique<AsyncTask>([&count] { ++count; }); + + async->send(); + async.reset(); + + EXPECT_EQ(count, 0); +} + +TEST(AsyncTask, RequestCoalescingMultithreaded) { + RunLoop loop; + + unsigned count = 0; + AsyncTask async([&count] { ++count; }); + async.unref(); + + std::vector<std::unique_ptr<Thread<TestWorker>>> threads; + ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular}; + + unsigned numThreads = 50; + for (unsigned i = 0; i < numThreads; ++i) { + std::unique_ptr<Thread<TestWorker>> thread = + std::make_unique<Thread<TestWorker>>(context, &async); + + thread->invoke(&TestWorker::run); + threads.push_back(std::move(thread)); + } + + // Join all the threads + threads.clear(); + + loop.runOnce(); + + EXPECT_EQ(count, 1); +} + +TEST(AsyncTask, ThreadSafety) { + RunLoop loop; + + unsigned count = 0; + AsyncTask async([&count] { ++count; }); + async.unref(); + + unsigned numThreads = 50; + + auto callback = [&] { + if (!--numThreads) { + loop.stop(); + } + }; + + std::vector<std::unique_ptr<Thread<TestWorker>>> threads; + std::vector<std::unique_ptr<mbgl::WorkRequest>> requests; + ThreadContext context = {"Test", ThreadType::Map, ThreadPriority::Regular}; + + for (unsigned i = 0; i < numThreads; ++i) { + std::unique_ptr<Thread<TestWorker>> thread = + std::make_unique<Thread<TestWorker>>(context, &async); + + requests.push_back( + thread->invokeWithCallback(&TestWorker::runWithCallback, callback)); + + threads.push_back(std::move(thread)); + } + + loop.run(); + + // We expect here more than 1 but 1 would also be + // a valid result, although very unlikely (I hope). + EXPECT_GT(count, 1); +} diff --git a/test/test.gypi b/test/test.gypi index c96b4f686c..74996a2e13 100644 --- a/test/test.gypi +++ b/test/test.gypi @@ -45,6 +45,7 @@ 'api/set_style.cpp', + 'miscellaneous/async_task.cpp', 'miscellaneous/clip_ids.cpp', 'miscellaneous/binpack.cpp', 'miscellaneous/bilinear.cpp', |