File: mailbox_element.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 (157 lines) | stat: -rw-r--r-- 6,183 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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    C++                           *
 *                     | |     / _ \ | |_       Actor                         *
 *                     | |___ / ___ \|  _|      Framework                     *
 *                      \____/_/   \_|_|                                      *
 *                                                                            *
 * Copyright (C) 2011 - 2017                                                  *
 * Dominik Charousset <dominik.charousset (at) haw-hamburg.de>                *
 *                                                                            *
 * 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 mailbox_element

#include <string>
#include <tuple>
#include <vector>

#include "caf/all.hpp"

#include "caf/test/unit_test.hpp"

using std::make_tuple;
using std::string;
using std::tuple;
using std::vector;

using namespace caf;

namespace {

template <class... Ts>
optional<tuple<Ts...>> fetch(const type_erased_tuple& x) {
  if (!x.match_elements<Ts...>())
    return none;
  return x.get_as_tuple<Ts...>();
}

template <class... Ts>
optional<tuple<Ts...>> fetch(const message& x) {
  return fetch<Ts...>(x.content());
}

template <class... Ts>
optional<tuple<Ts...>> fetch(const mailbox_element& x) {
  return fetch<Ts...>(x.content());
}

} // namespace

CAF_TEST(empty_message) {
  auto m1 = make_mailbox_element(nullptr, make_message_id(),
                                 no_stages, make_message());
  CAF_CHECK(m1->mid.is_async());
  CAF_CHECK(m1->mid.category() == message_id::normal_message_category);
  CAF_CHECK(m1->content().empty());
}

CAF_TEST(non_empty_message) {
  auto m1 = make_mailbox_element(nullptr, make_message_id(),
                                 no_stages, make_message(1, 2, 3));
  CAF_CHECK(m1->mid.is_async());
  CAF_CHECK(m1->mid.category() == message_id::normal_message_category);
  CAF_CHECK(!m1->content().empty());
  CAF_CHECK_EQUAL((fetch<int, int>(*m1)), none);
  CAF_CHECK_EQUAL((fetch<int, int, int>(*m1)), make_tuple(1, 2, 3));
}

CAF_TEST(message_roundtrip) {
  auto msg = make_message(1, 2, 3);
  auto msg_ptr = msg.cvals().get();
  auto m1 = make_mailbox_element(nullptr, make_message_id(),
                                 no_stages, std::move(msg));
  auto msg2 = m1->move_content_to_message();
  CAF_CHECK_EQUAL(msg2.cvals().get(), msg_ptr);
}

CAF_TEST(message_roundtrip_with_copy) {
  auto msg = make_message(1, 2, 3);
  auto msg_ptr = msg.cvals().get();
  auto m1 = make_mailbox_element(nullptr, make_message_id(),
                                 no_stages, std::move(msg));
  auto msg2 = m1->copy_content_to_message();
  auto msg3 = m1->move_content_to_message();
  CAF_CHECK_EQUAL(msg2.cvals().get(), msg_ptr);
  CAF_CHECK_EQUAL(msg3.cvals().get(), msg_ptr);
}

CAF_TEST(tuple) {
  auto m1 = make_mailbox_element(nullptr, make_message_id(),
                                 no_stages, 1, 2, 3);
  CAF_CHECK(m1->mid.is_async());
  CAF_CHECK(m1->mid.category() == message_id::normal_message_category);
  CAF_CHECK(!m1->content().empty());
  CAF_CHECK_EQUAL((fetch<int, int>(*m1)), none);
  CAF_CHECK_EQUAL((fetch<int, int, int>(*m1)), make_tuple(1, 2, 3));
}

CAF_TEST(move_tuple) {
  auto m1 = make_mailbox_element(nullptr, make_message_id(),
                                 no_stages, "hello", "world");
  using strings = tuple<string, string>;
  CAF_CHECK_EQUAL((fetch<string, string>(*m1)), strings("hello", "world"));
  auto msg = m1->move_content_to_message();
  CAF_CHECK_EQUAL((fetch<string, string>(msg)), strings("hello", "world"));
  CAF_CHECK_EQUAL((fetch<string, string>(*m1)), strings("", ""));
}

CAF_TEST(copy_tuple) {
  auto m1 = make_mailbox_element(nullptr, make_message_id(),
                                 no_stages, "hello", "world");
  using strings = tuple<string, string>;
  CAF_CHECK_EQUAL((fetch<string, string>(*m1)), strings("hello", "world"));
  auto msg = m1->copy_content_to_message();
  CAF_CHECK_EQUAL((fetch<string, string>(msg)), strings("hello", "world"));
  CAF_CHECK_EQUAL((fetch<string, string>(*m1)), strings("hello", "world"));
}

CAF_TEST(high_priority) {
  auto m1 = make_mailbox_element(nullptr,
                                 make_message_id(message_priority::high),
                                 no_stages, 42);
  CAF_CHECK(m1->mid.category() == message_id::urgent_message_category);
}

CAF_TEST(upstream_msg_static) {
  auto m1 = make_mailbox_element(nullptr, make_message_id(), no_stages,
                                 make<upstream_msg::drop>({0, 0}, nullptr));
  CAF_CHECK(m1->mid.category() == message_id::upstream_message_category);
}

CAF_TEST(upstream_msg_dynamic) {
  auto m1 = make_mailbox_element(
    nullptr, make_message_id(), no_stages,
    make_message(make<upstream_msg::drop>({0, 0}, nullptr)));
  CAF_CHECK(m1->mid.category() == message_id::upstream_message_category);
}

CAF_TEST(downstream_msg_static) {
  auto m1 = make_mailbox_element(nullptr, make_message_id(), no_stages,
                                 make<downstream_msg::close>({0, 0}, nullptr));
  CAF_CHECK(m1->mid.category() == message_id::downstream_message_category);
}

CAF_TEST(downstream_msg_dynamic) {
  auto m1 = make_mailbox_element(
    nullptr, make_message_id(), no_stages,
    make_message(make<downstream_msg::close>({0, 0}, nullptr)));
  CAF_CHECK(m1->mid.category() == message_id::downstream_message_category);
}