summaryrefslogtreecommitdiff
path: root/test/actor/actor_ref.test.cpp
blob: 78721c965e95a0975508008c933932ca75dc7ade (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <mbgl/actor/actor.hpp>
#include <mbgl/util/default_thread_pool.hpp>

#include <mbgl/test/util.hpp>

#include <chrono>
#include <functional>
#include <future>

using namespace mbgl;
using namespace std::chrono_literals;

TEST(ActorRef, CanOutliveActor) {
    // An ActorRef can outlive its actor. Doing does not extend the actor's lifetime.
    // Sending a message to an ActorRef whose actor has died is a no-op.

    struct Test {
        bool& died;

        Test(ActorRef<Test>, bool& died_)
            : died(died_) {
        }

        ~Test() {
            died = true;
        }

        void receive() {
            FAIL();
        }
    };

    ThreadPool pool { 1 };
    bool died = false;

    ActorRef<Test> test = [&] () {
        return Actor<Test>(pool, std::ref(died)).self();
    }();

    EXPECT_TRUE(died);
    test.invoke(&Test::receive);
}