File: aout.cpp

package info (click to toggle)
actor-framework 0.18.7-1~exp1
  • links: PTS
  • area: main
  • in suites: experimental
  • size: 8,740 kB
  • sloc: cpp: 85,162; sh: 491; python: 187; makefile: 11
file content (83 lines) | stat: -rw-r--r-- 2,427 bytes parent folder | download
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
// This file is part of CAF, the C++ Actor Framework. See the file LICENSE in
// the main distribution directory for license terms and copyright or visit
// https://github.com/actor-framework/actor-framework/blob/master/LICENSE.

#define CAF_SUITE aout

#include "core-test.hpp"

#include "caf/all.hpp"

using namespace caf;

using std::endl;

namespace {

constexpr const char* global_redirect = ":test";
constexpr const char* local_redirect = ":test2";

constexpr const char* chatty_line = "hi there!:)";
constexpr const char* chattier_line = "hello there, fellow friend!:)";

void chatty_actor(event_based_actor* self) {
  aout(self) << chatty_line << endl;
}

void chattier_actor(event_based_actor* self, const std::string& fn) {
  aout(self) << chatty_line << endl;
  actor_ostream::redirect(self, fn);
  aout(self) << chattier_line << endl;
}

struct fixture {
  fixture() : system(cfg) {
    // nop
  }

  actor_system_config cfg;
  actor_system system;
  scoped_actor self{system, true};
};

} // namespace

BEGIN_FIXTURE_SCOPE(fixture)

CAF_TEST(redirect_aout_globally) {
  self->join(system.groups().get_local(global_redirect));
  actor_ostream::redirect_all(system, global_redirect);
  system.spawn(chatty_actor);
  self->receive([](const std::string& virtual_file, std::string& line) {
    // drop trailing '\n'
    if (!line.empty())
      line.pop_back();
    CHECK_EQ(virtual_file, ":test");
    CHECK_EQ(line, chatty_line);
  });
  self->await_all_other_actors_done();
  CHECK_EQ(self->mailbox().size(), 0u);
}

CAF_TEST(global_and_local_redirect) {
  self->join(system.groups().get_local(global_redirect));
  self->join(system.groups().get_local(local_redirect));
  actor_ostream::redirect_all(system, global_redirect);
  system.spawn(chatty_actor);
  system.spawn(chattier_actor, local_redirect);
  std::vector<std::pair<std::string, std::string>> expected{
    {":test", chatty_line}, {":test", chatty_line}, {":test2", chattier_line}};
  std::vector<std::pair<std::string, std::string>> lines;
  int i = 0;
  self->receive_for(i, 3)([&](std::string& virtual_file, std::string& line) {
    // drop trailing '\n'
    if (!line.empty())
      line.pop_back();
    lines.emplace_back(std::move(virtual_file), std::move(line));
  });
  CHECK(std::is_permutation(lines.begin(), lines.end(), expected.begin()));
  self->await_all_other_actors_done();
  CHECK_EQ(self->mailbox().size(), 0u);
}

END_FIXTURE_SCOPE()