File: request_timeout.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 (309 lines) | stat: -rw-r--r-- 11,361 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
/******************************************************************************
 *                       ____    _    _____                                   *
 *                      / ___|  / \  |  ___|    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/after.hpp"

#define CAF_SUITE request_timeout

#include "caf/test/dsl.hpp"

#include <chrono>

#include "caf/all.hpp"

using namespace caf;

using std::string;
using std::chrono::milliseconds;
using std::chrono::seconds;

namespace {

using ping_atom = atom_constant<atom("ping")>;
using pong_atom = atom_constant<atom("pong")>;
using timeout_atom = atom_constant<atom("timeout")>;

struct pong_state {
  static const char* name;
};

const char* pong_state::name = "pong";

behavior pong(stateful_actor<pong_state>*) {
  return {
    [=](ping_atom) { return pong_atom_v; },
  };
}

struct ping_state {
  static const char* name;
  bool had_first_timeout = false; // unused in ping_singleN functions
};

const char* ping_state::name = "ping";

using ping_actor = stateful_actor<ping_state>;

using fptr = behavior (*)(ping_actor*, bool*, const actor&);
using test_vec = std::vector<std::pair<fptr, string>>;

// assumes to receive a timeout (sent via delayed_send) before pong replies
behavior ping_single1(ping_actor* self, bool* had_timeout, const actor& buddy) {
  self->send(buddy, ping_atom_v);
  self->delayed_send(self, std::chrono::seconds(1), timeout_atom_v);
  return {
    [=](pong_atom) { CAF_FAIL("received pong atom"); },
    [=](timeout_atom) {
      *had_timeout = true;
      self->quit();
    },
  };
}

// assumes to receive a timeout (via after()) before pong replies
behavior ping_single2(ping_actor* self, bool* had_timeout, const actor& buddy) {
  self->send(buddy, ping_atom_v);
  return {
    [=](pong_atom) { CAF_FAIL("received pong atom"); },
    after(std::chrono::seconds(1)) >>
      [=] {
        *had_timeout = true;
        self->quit();
      },
  };
}

// assumes to receive a timeout (via request error handler) before pong replies
behavior ping_single3(ping_actor* self, bool* had_timeout, const actor& buddy) {
  self->request(buddy, milliseconds(100), ping_atom_v)
    .then([=](pong_atom) { CAF_FAIL("received pong atom"); },
          [=](const error& err) {
            CAF_REQUIRE(err == sec::request_timeout);
            *had_timeout = true;
          });
  return {}; // dummy value in order to give all 3 variants the same fun sig
}

// assumes to receive an inner timeout (sent via delayed_send) before pong
// replies, then second timeout fires
behavior ping_nested1(ping_actor* self, bool* had_timeout, const actor& buddy) {
  self->send(buddy, ping_atom_v);
  self->delayed_send(self, std::chrono::seconds(1), timeout_atom_v);
  return {
    [=](pong_atom) { CAF_FAIL("received pong atom"); },
    [=](timeout_atom) {
      self->state.had_first_timeout = true;
      self->become(after(milliseconds(100)) >> [=] {
        CAF_CHECK(self->state.had_first_timeout);
        *had_timeout = true;
        self->quit();
      });
    },
  };
}

// assumes to receive an inner timeout (via after()) before pong replies, then a
// second timeout fires
behavior ping_nested2(ping_actor* self, bool* had_timeout, const actor& buddy) {
  self->send(buddy, ping_atom_v);
  return {
    [=](pong_atom) { CAF_FAIL("received pong atom"); },
    after(std::chrono::seconds(1)) >>
      [=] {
        self->state.had_first_timeout = true;
        self->become(after(milliseconds(100)) >> [=] {
          CAF_CHECK(self->state.had_first_timeout);
          *had_timeout = true;
          self->quit();
        });
      },
  };
}

// assumes to receive an inner timeout (via request error handler) before pong
// replies, then a second timeout fires
behavior ping_nested3(ping_actor* self, bool* had_timeout, const actor& buddy) {
  self->request(buddy, milliseconds(100), ping_atom_v)
    .then(
      [=](pong_atom) {
        CAF_FAIL("received pong atom");
        self->quit(sec::unexpected_message);
      },
      [=](const error& err) {
        CAF_REQUIRE_EQUAL(err, sec::request_timeout);
        self->state.had_first_timeout = true;
      });
  return {
    after(milliseconds(100)) >>
      [=] {
        CAF_CHECK(self->state.had_first_timeout);
        *had_timeout = true;
        self->quit();
      },
  };
}

// uses .then on both requests
behavior ping_multiplexed1(ping_actor* self, bool* had_timeout,
                           const actor& pong_actor) {
  self->request(pong_actor, milliseconds(100), ping_atom_v)
    .then([=](pong_atom) { CAF_FAIL("received pong atom"); },
          [=](const error& err) {
            CAF_REQUIRE_EQUAL(err, sec::request_timeout);
            if (!self->state.had_first_timeout)
              self->state.had_first_timeout = true;
            else
              *had_timeout = true;
          });
  self->request(pong_actor, milliseconds(100), ping_atom_v)
    .then([=](pong_atom) { CAF_FAIL("received pong atom"); },
          [=](const error& err) {
            CAF_REQUIRE_EQUAL(err, sec::request_timeout);
            if (!self->state.had_first_timeout)
              self->state.had_first_timeout = true;
            else
              *had_timeout = true;
          });
  return {};
}

// uses .await on both requests
behavior ping_multiplexed2(ping_actor* self, bool* had_timeout,
                           const actor& pong_actor) {
  self->request(pong_actor, milliseconds(100), ping_atom_v)
    .await([=](pong_atom) { CAF_FAIL("received pong atom"); },
           [=](const error& err) {
             CAF_REQUIRE_EQUAL(err, sec::request_timeout);
             if (!self->state.had_first_timeout)
               self->state.had_first_timeout = true;
             else
               *had_timeout = true;
           });
  self->request(pong_actor, milliseconds(100), ping_atom_v)
    .await([=](pong_atom) { CAF_FAIL("received pong atom"); },
           [=](const error& err) {
             CAF_REQUIRE_EQUAL(err, sec::request_timeout);
             if (!self->state.had_first_timeout)
               self->state.had_first_timeout = true;
             else
               *had_timeout = true;
           });
  return {};
}

// uses .await and .then
behavior ping_multiplexed3(ping_actor* self, bool* had_timeout,
                           const actor& pong_actor) {
  self->request(pong_actor, milliseconds(100), ping_atom_v)
    .then([=](pong_atom) { CAF_FAIL("received pong atom"); },
          [=](const error& err) {
            CAF_REQUIRE_EQUAL(err, sec::request_timeout);
            if (!self->state.had_first_timeout)
              self->state.had_first_timeout = true;
            else
              *had_timeout = true;
          });
  self->request(pong_actor, milliseconds(100), ping_atom_v)
    .await([=](pong_atom) { CAF_FAIL("received pong atom"); },
           [=](const error& err) {
             CAF_REQUIRE_EQUAL(err, sec::request_timeout);
             if (!self->state.had_first_timeout)
               self->state.had_first_timeout = true;
             else
               *had_timeout = true;
           });
  return {};
}

} // namespace

CAF_TEST_FIXTURE_SCOPE(request_timeout_tests, test_coordinator_fixture<>)

CAF_TEST(single_timeout) {
  test_vec fs{{ping_single1, "ping_single1"},
              {ping_single2, "ping_single2"},
              {ping_single3, "ping_single3"}};
  for (auto f : fs) {
    bool had_timeout = false;
    CAF_MESSAGE("test implemenation " << f.second);
    auto testee = sys.spawn(f.first, &had_timeout, sys.spawn<lazy_init>(pong));
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 1u);
    CAF_REQUIRE_EQUAL(sched.next_job<local_actor>().name(), string{"ping"});
    sched.run_once();
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 1u);
    CAF_REQUIRE_EQUAL(sched.next_job<local_actor>().name(), string{"pong"});
    sched.trigger_timeout();
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 2u);
    // now, the timeout message is already dispatched, while pong did
    // not respond to the message yet, i.e., timeout arrives before response
    CAF_CHECK_EQUAL(sched.run(), 2u);
    CAF_CHECK(had_timeout);
  }
}

CAF_TEST(nested_timeout) {
  test_vec fs{{ping_nested1, "ping_nested1"},
              {ping_nested2, "ping_nested2"},
              {ping_nested3, "ping_nested3"}};
  for (auto f : fs) {
    bool had_timeout = false;
    CAF_MESSAGE("test implemenation " << f.second);
    auto testee = sys.spawn(f.first, &had_timeout, sys.spawn<lazy_init>(pong));
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 1u);
    CAF_REQUIRE_EQUAL(sched.next_job<local_actor>().name(), string{"ping"});
    sched.run_once();
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 1u);
    CAF_REQUIRE_EQUAL(sched.next_job<local_actor>().name(), string{"pong"});
    sched.trigger_timeout();
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 2u);
    // now, the timeout message is already dispatched, while pong did
    // not respond to the message yet, i.e., timeout arrives before response
    sched.run();
    // dispatch second timeout
    CAF_REQUIRE_EQUAL(sched.trigger_timeout(), true);
    CAF_REQUIRE_EQUAL(sched.next_job<local_actor>().name(), string{"ping"});
    CAF_CHECK(!had_timeout);
    CAF_CHECK(sched.next_job<ping_actor>().state.had_first_timeout);
    sched.run();
    CAF_CHECK(had_timeout);
  }
}

CAF_TEST(multiplexed_timeout) {
  test_vec fs{{ping_multiplexed1, "ping_multiplexed1"},
              {ping_multiplexed2, "ping_multiplexed2"},
              {ping_multiplexed3, "ping_multiplexed3"}};
  for (auto f : fs) {
    bool had_timeout = false;
    CAF_MESSAGE("test implemenation " << f.second);
    auto testee = sys.spawn(f.first, &had_timeout, sys.spawn<lazy_init>(pong));
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 1u);
    CAF_REQUIRE_EQUAL(sched.next_job<local_actor>().name(), string{"ping"});
    sched.run_once();
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 1u);
    CAF_REQUIRE_EQUAL(sched.next_job<local_actor>().name(), string{"pong"});
    sched.trigger_timeouts();
    CAF_REQUIRE_EQUAL(sched.jobs.size(), 2u);
    // now, the timeout message is already dispatched, while pong did
    // not respond to the message yet, i.e., timeout arrives before response
    sched.run();
    CAF_CHECK(had_timeout);
  }
}

CAF_TEST_FIXTURE_SCOPE_END()