summaryrefslogtreecommitdiff
path: root/test/util
diff options
context:
space:
mode:
authorThiago Marcos P. Santos <tmpsantos@gmail.com>2017-06-15 17:20:19 +0300
committerThiago Marcos P. Santos <tmpsantos@gmail.com>2017-06-21 14:30:09 +0300
commitbfdc4ef64655bf1bbfd216072500e701a9af6d27 (patch)
treeff447e08f978b7c1dc454fdef79f4eecf49ea212 /test/util
parent1178256ae9e2eba04f0bfca136d2355276a8961f (diff)
downloadqtlocation-mapboxgl-bfdc4ef64655bf1bbfd216072500e701a9af6d27.tar.gz
[tests] Port the ThreadLocalStorage test to the actor model
Diffstat (limited to 'test/util')
-rw-r--r--test/util/thread_local.test.cpp45
1 files changed, 29 insertions, 16 deletions
diff --git a/test/util/thread_local.test.cpp b/test/util/thread_local.test.cpp
index 4ee7042580..b2eae74c5f 100644
--- a/test/util/thread_local.test.cpp
+++ b/test/util/thread_local.test.cpp
@@ -1,16 +1,18 @@
#include <mbgl/util/run_loop.hpp>
-#include <mbgl/util/thread.hpp>
+#include <mbgl/util/threaded_object.hpp>
#include <mbgl/util/thread_local.hpp>
#include <mbgl/test/util.hpp>
+#include <future>
+
using namespace mbgl::util;
namespace {
class TestThread {
public:
- TestThread(int *number_) {
+ TestThread(mbgl::ActorRef<TestThread>, int *number_) {
number.set(number_);
}
@@ -18,8 +20,8 @@ public:
number.set(nullptr);
}
- int getNumber() {
- return *number.get();
+ void getNumber(std::promise<int> result){
+ result.set_value(*number.get());
}
private:
@@ -37,15 +39,28 @@ TEST(ThreadLocalStorage, Basic) {
int number2 = 2;
int number3 = 3;
- ThreadContext context = {"Test"};
+ ThreadedObject<TestThread> thread1("Test", &number1);
+ ThreadedObject<TestThread> thread2("Test", &number2);
+ ThreadedObject<TestThread> thread3("Test", &number3);
+
+ auto thread1Ref = thread1.actor();
+ auto thread2Ref = thread2.actor();
+ auto thread3Ref = thread3.actor();
- Thread<TestThread> thread1(context, &number1);
- Thread<TestThread> thread2(context, &number2);
- Thread<TestThread> thread3(context, &number3);
+ std::promise<int> result1;
+ auto result1Future = result1.get_future();
+ thread1Ref.invoke(&TestThread::getNumber, std::move(result1));
+ EXPECT_EQ(number1, result1Future.get());
- EXPECT_EQ(number1, thread1.invokeSync(&TestThread::getNumber));
- EXPECT_EQ(number2, thread2.invokeSync(&TestThread::getNumber));
- EXPECT_EQ(number3, thread3.invokeSync(&TestThread::getNumber));
+ std::promise<int> result2;
+ auto result2Future = result2.get_future();
+ thread2Ref.invoke(&TestThread::getNumber, std::move(result2));
+ EXPECT_EQ(number2, result2Future.get());
+
+ std::promise<int> result3;
+ auto result3Future = result3.get_future();
+ thread3Ref.invoke(&TestThread::getNumber, std::move(result3));
+ EXPECT_EQ(number3, result3Future.get());
}
TEST(ThreadLocalStorage, NotSetReturnsNull) {
@@ -63,7 +78,7 @@ struct DtorCounter {
class TestThreadReclaim {
public:
- TestThreadReclaim(DtorCounter* counter_) {
+ TestThreadReclaim(mbgl::ActorRef<TestThreadReclaim>, DtorCounter* counter_) {
counter.set(counter_);
}
@@ -83,10 +98,8 @@ TEST(ThreadLocalStorage, AutoReclaim) {
auto dtorCounter1 = new DtorCounter{ &counter };
auto dtorCounter2 = new DtorCounter{ &counter };
- ThreadContext context = {"Test"};
-
- auto thread1 = std::make_unique<Thread<TestThreadReclaim>>(context, dtorCounter1);
- auto thread2 = std::make_unique<Thread<TestThreadReclaim>>(context, dtorCounter2);
+ auto thread1 = std::make_unique<ThreadedObject<TestThreadReclaim>>("Test", dtorCounter1);
+ auto thread2 = std::make_unique<ThreadedObject<TestThreadReclaim>>("Test", dtorCounter2);
thread1.reset();
thread2.reset();