File: detached_actors.cpp

package info (click to toggle)
actor-framework 0.17.6-3.2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 9,008 kB
  • sloc: cpp: 77,684; sh: 674; python: 309; makefile: 13
file content (104 lines) | stat: -rw-r--r-- 3,524 bytes parent folder | download | duplicates (4)
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright 2011-2018 Dominik Charousset                                     *
 *                                                                            *
 * Distributed under the terms and conditions of the BSD 3-Clause License or  *
 * (at your option) under the terms and conditions of the Boost Software      *
 * License 1.0. See accompanying files LICENSE and LICENSE_ALTERNATIVE.       *
 *                                                                            *
 * If you did not receive a copy of the license files, see                    *
 * http://opensource.org/licenses/BSD-3-Clause and                            *
 * http://www.boost.org/LICENSE_1_0.txt.                                      *
 ******************************************************************************/

#include "caf/config.hpp"

#define CAF_SUITE detached_actors
#include "caf/test/unit_test.hpp"

#include "caf/all.hpp"

using namespace caf;

using std::endl;

namespace {

struct fixture {
  actor_system_config cfg;
  actor_system sys;
  scoped_actor self;

  fixture() : sys(cfg), self(sys, true) {
    // nop
  }
};

} // namespace

CAF_TEST_FIXTURE_SCOPE(detached_actors, fixture)

CAF_TEST(shutdown) {
  CAF_MESSAGE("does sys shut down after spawning a detached actor?");
  sys.spawn<detached>([] {});
}

CAF_TEST(shutdown_with_delayed_send) {
  CAF_MESSAGE("does sys shut down after spawning a detached actor that used "
              "delayed_send?");
  auto f = [](event_based_actor* self) -> behavior {
    self->delayed_send(self, std::chrono::nanoseconds(1), ok_atom_v);
    return {
      [=](ok_atom) {
        self->quit();
      }
    };
  };
  sys.spawn<detached>(f);
}

CAF_TEST(shutdown_with_unhandled_delayed_send) {
  CAF_MESSAGE("does sys shut down after spawning a detached actor that used "
              "delayed_send but didn't bother waiting for it?");
  auto f = [](event_based_actor* self) {
    self->delayed_send(self, std::chrono::nanoseconds(1), ok_atom_v);
  };
  sys.spawn<detached>(f);
}

CAF_TEST(shutdown_with_after) {
  CAF_MESSAGE("does sys shut down after spawning a detached actor that used "
              "after()?");
  auto f = [](event_based_actor* self) -> behavior {
    return {
      after(std::chrono::nanoseconds(1)) >> [=] {
        self->quit();
      }
    };
  };
  sys.spawn<detached>(f);
}

CAF_TEST(shutdown_delayed_send_loop) {
  CAF_MESSAGE("does sys shut down after spawning a detached actor that used "
              "a delayed send loop and was interrupted via exit message?");
  auto f = [](event_based_actor* self) -> behavior {
    self->delayed_send(self, std::chrono::milliseconds(1), ok_atom_v);
    return {
      [=](ok_atom) {
        self->delayed_send(self, std::chrono::milliseconds(1), ok_atom_v);
      },
    };
  };
  auto a = sys.spawn<detached>(f);
  auto g = detail::make_scope_guard([&] {
    self->send_exit(a, exit_reason::user_shutdown);
  });
}

CAF_TEST_FIXTURE_SCOPE_END()