summaryrefslogtreecommitdiff
path: root/test/actor
diff options
context:
space:
mode:
authorIvo van Dongen <info@ivovandongen.nl>2017-05-25 09:42:48 +0300
committerIvo van Dongen <ivovandongen@users.noreply.github.com>2017-05-27 09:25:03 +0300
commitb608d85db78cb672da76cb4531438ba32843c6fb (patch)
tree79e6a058fc6f3f1420fe254c1b409dedcb8d5daa /test/actor
parentf0d0e7423b610782fdbed576bc2a442b82c8e1f8 (diff)
downloadqtlocation-mapboxgl-b608d85db78cb672da76cb4531438ba32843c6fb.tar.gz
[core] allow self closing mailbox/actor
Diffstat (limited to 'test/actor')
-rw-r--r--test/actor/actor.test.cpp32
1 files changed, 32 insertions, 0 deletions
diff --git a/test/actor/actor.test.cpp b/test/actor/actor.test.cpp
index 9db6882889..638c24ed7d 100644
--- a/test/actor/actor.test.cpp
+++ b/test/actor/actor.test.cpp
@@ -6,6 +6,7 @@
#include <chrono>
#include <functional>
#include <future>
+#include <memory>
using namespace mbgl;
using namespace std::chrono_literals;
@@ -123,6 +124,37 @@ TEST(Actor, DestructionBlocksOnSend) {
thread.join();
}
+TEST(Actor, DestructionAllowedInReceiveOnSameThread) {
+ // Destruction doesn't block if occurring on the same
+ // thread as receive(). This prevents deadlocks and
+ // allows for self-closing actors
+
+ struct Test {
+
+ Test(ActorRef<Test>){};
+
+ void callMeBack(std::function<void ()> callback) {
+ callback();
+ }
+ };
+
+ ThreadPool pool { 1 };
+
+ std::promise<void> callbackFiredPromise;
+
+ auto test = std::make_unique<Actor<Test>>(pool);
+
+ // Callback (triggered while mutex is locked in Mailbox::receive())
+ test->invoke(&Test::callMeBack, [&]() {
+ // Destroy the Actor/Mailbox in the same thread
+ test.reset();
+ callbackFiredPromise.set_value();
+ });
+
+ auto status = callbackFiredPromise.get_future().wait_for(std::chrono::seconds(1));
+ ASSERT_EQ(std::future_status::ready, status);
+}
+
TEST(Actor, OrderedMailbox) {
// Messages are processed in order.