File: unpublish.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 (110 lines) | stat: -rw-r--r-- 3,631 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
105
106
107
108
109
110
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    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.                                      *
 ******************************************************************************/

#define CAF_SUITE io.unpublish

#include "caf/config.hpp"

#include "caf/test/io_dsl.hpp"

#include <atomic>
#include <memory>
#include <new>
#include <thread>

#include "caf/all.hpp"
#include "caf/io/all.hpp"

using namespace caf;

namespace {

struct suite_state {
  long dtors_called = 0;
  suite_state() = default;
};

using suite_state_ptr = std::shared_ptr<suite_state>;

class dummy : public event_based_actor {
public:
  dummy(actor_config& cfg, suite_state_ptr ssp)
    : event_based_actor(cfg), ssp_(std::move(ssp)) {
    // nop
  }

  ~dummy() override {
    ssp_->dtors_called += 1;
  }

  behavior make_behavior() override {
    return {[] {
      // nop
    }};
  }

private:
  suite_state_ptr ssp_;
};

struct fixture : point_to_point_fixture<> {
  fixture() {
    prepare_connection(mars, earth, "mars", 8080);
    ssp = std::make_shared<suite_state>();
  }

  ~fixture() {
    run();
    CAF_CHECK_EQUAL(ssp->dtors_called, 2);
  }

  suite_state_ptr ssp;
};

} // namespace

CAF_TEST_FIXTURE_SCOPE(unpublish_tests, fixture)

CAF_TEST(actors can become unpublished) {
  auto testee = mars.sys.spawn<dummy>(ssp);
  auto guard = detail::make_scope_guard([&] {
    // The MM holds a reference to this actor publishing it, so we need to kill
    // it manually.
    anon_send_exit(testee, exit_reason::user_shutdown);
  });
  loop_after_next_enqueue(mars);
  auto port = unbox(mars.mm.publish(testee, 8080));
  CAF_REQUIRE_EQUAL(port, 8080);
  CAF_MESSAGE("the middleman ignores invalid unpublish() calls");
  auto testee2 = mars.sys.spawn<dummy>(ssp);
  loop_after_next_enqueue(mars);
  auto res = mars.mm.unpublish(testee2, 8080);
  CAF_CHECK(!res && res.error() == sec::no_actor_published_at_port);
  anon_send_exit(testee2, exit_reason::user_shutdown);
  CAF_MESSAGE("after unpublishing an actor, remotes can no longer connect");
  loop_after_next_enqueue(mars);
  CAF_CHECK(mars.mm.unpublish(testee, 8080));
  // TODO: ideally, we'd check that remote actors in fact can no longer connect.
  //       However, the test multiplexer does not support "closing" connections
  //       and the remote_actor blocks forever.
  // run();
  // loop_after_next_enqueue(earth);
  // CAF_CHECK(!earth.mm.remote_actor("mars", 8080));
}

CAF_TEST_FIXTURE_SCOPE_END()