2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/actor/actor.h"
|
2020-11-23 01:24:36 +01:00
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/actor/MultiPromise.h"
|
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
#include "td/actor/SleepActor.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/logging.h"
|
2018-09-10 03:08:15 +02:00
|
|
|
#include "td/utils/MpscPollableQueue.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Observer.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
2023-04-23 01:01:51 +02:00
|
|
|
#include "td/utils/port/path.h"
|
2018-09-07 02:41:21 +02:00
|
|
|
#include "td/utils/port/thread.h"
|
2022-06-27 12:30:18 +02:00
|
|
|
#include "td/utils/Promise.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Slice.h"
|
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/StringBuilder.h"
|
2021-09-18 23:47:05 +02:00
|
|
|
#include "td/utils/tests.h"
|
2018-09-27 15:37:15 +02:00
|
|
|
#include "td/utils/Time.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
#include <memory>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <tuple>
|
|
|
|
|
|
|
|
static const size_t BUF_SIZE = 1024 * 1024;
|
|
|
|
static char buf[BUF_SIZE];
|
|
|
|
static char buf2[BUF_SIZE];
|
2021-10-15 13:21:30 +02:00
|
|
|
static td::StringBuilder sb(td::MutableSlice(buf, BUF_SIZE - 1));
|
|
|
|
static td::StringBuilder sb2(td::MutableSlice(buf2, BUF_SIZE - 1));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-10-25 08:44:57 +02:00
|
|
|
static td::vector<std::shared_ptr<td::MpscPollableQueue<td::EventFull>>> create_queues() {
|
|
|
|
#if TD_THREAD_UNSUPPORTED || TD_EVENTFD_UNSUPPORTED
|
|
|
|
return {};
|
|
|
|
#else
|
2021-10-15 13:21:30 +02:00
|
|
|
auto res = std::make_shared<td::MpscPollableQueue<td::EventFull>>();
|
2018-09-13 12:26:14 +02:00
|
|
|
res->init();
|
2021-10-25 08:44:57 +02:00
|
|
|
return {res};
|
|
|
|
#endif
|
2018-09-13 12:26:14 +02:00
|
|
|
}
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
TEST(Actors, SendLater) {
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler scheduler;
|
2021-10-25 08:44:57 +02:00
|
|
|
scheduler.init(0, create_queues(), nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto guard = scheduler.get_guard();
|
2021-10-15 13:21:30 +02:00
|
|
|
class Worker final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
void f() {
|
|
|
|
sb << "A";
|
|
|
|
}
|
|
|
|
};
|
2021-10-15 13:21:30 +02:00
|
|
|
auto id = td::create_actor<Worker>("Worker");
|
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
|
|
|
td::send_closure(id, &Worker::f);
|
|
|
|
td::send_closure_later(id, &Worker::f);
|
|
|
|
td::send_closure(id, &Worker::f);
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("A", sb.as_cslice().c_str());
|
2021-10-15 13:21:30 +02:00
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("AAA", sb.as_cslice().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
class X {
|
|
|
|
public:
|
|
|
|
X() {
|
|
|
|
sb << "[cnstr_default]";
|
|
|
|
}
|
|
|
|
X(const X &) {
|
|
|
|
sb << "[cnstr_copy]";
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
X(X &&) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "[cnstr_move]";
|
|
|
|
}
|
|
|
|
X &operator=(const X &) {
|
|
|
|
sb << "[set_copy]";
|
|
|
|
return *this;
|
|
|
|
}
|
2021-10-18 13:36:15 +02:00
|
|
|
X &operator=(X &&) noexcept {
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "[set_move]";
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
~X() = default;
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class XReceiver final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
void by_const_ref(const X &) {
|
|
|
|
sb << "[by_const_ref]";
|
|
|
|
}
|
|
|
|
void by_lvalue_ref(const X &) {
|
|
|
|
sb << "[by_lvalue_ref]";
|
|
|
|
}
|
|
|
|
void by_value(X) {
|
|
|
|
sb << "[by_value]";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, simple_pass_event_arguments) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler scheduler;
|
2021-10-25 08:44:57 +02:00
|
|
|
scheduler.init(0, create_queues(), nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
auto guard = scheduler.get_guard();
|
2021-10-15 13:21:30 +02:00
|
|
|
auto id = td::create_actor<XReceiver>("XR").release();
|
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
X x;
|
|
|
|
|
|
|
|
// check tuple
|
|
|
|
// std::tuple<X> tx;
|
|
|
|
// sb.clear();
|
|
|
|
// std::tuple<X> ty(std::move(tx));
|
|
|
|
// tx = std::move(ty);
|
|
|
|
// ASSERT_STREQ("[cnstr_move]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Send temporary object
|
|
|
|
|
|
|
|
// Tmp-->ConstRef
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(id, &XReceiver::by_const_ref, X());
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_default][by_const_ref]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Tmp-->ConstRef (Delayed)
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(id, &XReceiver::by_const_ref, X());
|
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
// LOG(ERROR) << sb.as_cslice();
|
|
|
|
ASSERT_STREQ("[cnstr_default][cnstr_move][by_const_ref]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Tmp-->LvalueRef
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(id, &XReceiver::by_lvalue_ref, X());
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_default][by_lvalue_ref]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Tmp-->LvalueRef (Delayed)
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(id, &XReceiver::by_lvalue_ref, X());
|
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_default][cnstr_move][by_lvalue_ref]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Tmp-->Value
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(id, &XReceiver::by_value, X());
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_default][cnstr_move][by_value]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Tmp-->Value (Delayed)
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(id, &XReceiver::by_value, X());
|
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_default][cnstr_move][cnstr_move][by_value]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Var-->ConstRef
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(id, &XReceiver::by_const_ref, x);
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[by_const_ref]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Var-->ConstRef (Delayed)
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(id, &XReceiver::by_const_ref, x);
|
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_copy][by_const_ref]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Var-->LvalueRef
|
|
|
|
// Var-->LvalueRef (Delayed)
|
2023-10-18 18:12:31 +02:00
|
|
|
// CE or strange behaviour
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
// Var-->Value
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(id, &XReceiver::by_value, x);
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_copy][by_value]", sb.as_cslice().c_str());
|
|
|
|
|
|
|
|
// Var-->Value (Delayed)
|
|
|
|
sb.clear();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(id, &XReceiver::by_value, x);
|
|
|
|
scheduler.run_no_guard(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_STREQ("[cnstr_copy][cnstr_move][by_value]", sb.as_cslice().c_str());
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class PrintChar final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
PrintChar(char c, int cnt) : char_(c), cnt_(cnt) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void wakeup() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cnt_ == 0) {
|
|
|
|
stop();
|
|
|
|
} else {
|
|
|
|
sb << char_;
|
|
|
|
cnt_--;
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
char char_;
|
|
|
|
int cnt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
//
|
|
|
|
// Yield must add actor to the end of queue
|
|
|
|
//
|
|
|
|
TEST(Actors, simple_hand_yield) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler scheduler;
|
2021-10-25 08:44:57 +02:00
|
|
|
scheduler.init(0, create_queues(), nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
sb.clear();
|
|
|
|
int cnt = 1000;
|
|
|
|
{
|
|
|
|
auto guard = scheduler.get_guard();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::create_actor<PrintChar>("PrintA", 'A', cnt).release();
|
|
|
|
td::create_actor<PrintChar>("PrintB", 'B', cnt).release();
|
|
|
|
td::create_actor<PrintChar>("PrintC", 'C', cnt).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
scheduler.run(td::Timestamp::in(1));
|
|
|
|
td::string expected;
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < cnt; i++) {
|
|
|
|
expected += "ABC";
|
|
|
|
}
|
|
|
|
ASSERT_STREQ(expected.c_str(), sb.as_cslice().c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
class Ball {
|
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
friend void start_migrate(Ball &ball, td::int32 sched_id) {
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "start";
|
|
|
|
}
|
|
|
|
friend void finish_migrate(Ball &ball) {
|
|
|
|
sb2 << "finish";
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class Pong final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
void pong(Ball ball) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class Ping final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
explicit Ping(td::ActorId<Pong> pong) : pong_(pong) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(pong_, &Pong::pong, Ball());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorId<Pong> pong_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, simple_migrate) {
|
|
|
|
sb.clear();
|
|
|
|
sb2.clear();
|
|
|
|
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(2, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto pong = scheduler.create_actor_unsafe<Pong>(2, "Pong").release();
|
|
|
|
scheduler.create_actor_unsafe<Ping>(1, "Ping", pong).release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
#if TD_THREAD_UNSUPPORTED || TD_EVENTFD_UNSUPPORTED
|
|
|
|
ASSERT_STREQ("", sb.as_cslice().c_str());
|
|
|
|
ASSERT_STREQ("", sb2.as_cslice().c_str());
|
|
|
|
#else
|
|
|
|
ASSERT_STREQ("start", sb.as_cslice().c_str());
|
|
|
|
ASSERT_STREQ("finish", sb2.as_cslice().c_str());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class OpenClose final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit OpenClose(int cnt) : cnt_(cnt) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void wakeup() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto observer = reinterpret_cast<td::ObserverBase *>(123);
|
2023-04-23 01:01:51 +02:00
|
|
|
td::CSlice file_name = "server";
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cnt_ > 0) {
|
2023-04-23 01:01:51 +02:00
|
|
|
auto r_file_fd = td::FileFd::open(file_name, td::FileFd::Read | td::FileFd::Create);
|
2019-02-12 17:17:20 +01:00
|
|
|
LOG_CHECK(r_file_fd.is_ok()) << r_file_fd.error();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto file_fd = r_file_fd.move_as_ok();
|
2021-10-15 13:21:30 +02:00
|
|
|
{ auto pollable_fd = file_fd.get_poll_info().extract_pollable_fd(observer); }
|
2018-12-31 20:04:05 +01:00
|
|
|
file_fd.close();
|
|
|
|
cnt_--;
|
|
|
|
yield();
|
|
|
|
} else {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int cnt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, open_close) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(2, 0);
|
2022-01-01 01:35:39 +01:00
|
|
|
int cnt = 10000; // TODO(perf) optimize
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler.create_actor_unsafe<OpenClose>(1, "A", cnt).release();
|
|
|
|
scheduler.create_actor_unsafe<OpenClose>(2, "B", cnt).release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
2023-10-05 23:29:08 +02:00
|
|
|
td::unlink("server").ignore();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class MsgActor : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
virtual void msg() = 0;
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class Slave final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorId<MsgActor> msg;
|
|
|
|
explicit Slave(td::ActorId<MsgActor> msg) : msg(msg) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(msg, &MsgActor::msg);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class MasterActor final : public MsgActor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
alive_ = true;
|
2022-06-14 15:11:38 +02:00
|
|
|
slave = td::create_actor<Slave>("Slave", static_cast<td::ActorId<MsgActor>>(actor_id(this)));
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorOwn<Slave> slave;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
MasterActor() = default;
|
|
|
|
MasterActor(const MasterActor &) = delete;
|
|
|
|
MasterActor &operator=(const MasterActor &) = delete;
|
|
|
|
MasterActor(MasterActor &&) = delete;
|
|
|
|
MasterActor &operator=(MasterActor &&) = delete;
|
2021-07-03 22:51:36 +02:00
|
|
|
~MasterActor() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
alive_ = 987654321;
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void msg() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(alive_ == 123456789);
|
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
td::uint64 alive_ = 123456789;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, call_after_destruct) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler scheduler;
|
2021-10-25 08:44:57 +02:00
|
|
|
scheduler.init(0, create_queues(), nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
{
|
|
|
|
auto guard = scheduler.get_guard();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::create_actor<MasterActor>("Master").release();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
scheduler.run(td::Timestamp::in(1));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class LinkTokenSlave final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
explicit LinkTokenSlave(td::ActorShared<> parent) : parent_(std::move(parent)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-10-15 13:21:30 +02:00
|
|
|
void add(td::uint64 link_token) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(link_token == get_link_token());
|
|
|
|
}
|
|
|
|
void close() {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorShared<> parent_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class LinkTokenMasterActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
explicit LinkTokenMasterActor(int cnt) : cnt_(cnt) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
child_ = td::create_actor<LinkTokenSlave>("Slave", actor_shared(this, 123)).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
yield();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < 100 && cnt_ > 0; cnt_--, i++) {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto token = static_cast<td::uint64>(cnt_) + 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (i % 4) {
|
|
|
|
case 0: {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(td::ActorShared<LinkTokenSlave>(child_, token), &LinkTokenSlave::add, token);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 1: {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure_later(td::ActorShared<LinkTokenSlave>(child_, token), &LinkTokenSlave::add, token);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 2: {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::EventCreator::closure(td::ActorShared<LinkTokenSlave>(child_, token), &LinkTokenSlave::add, token)
|
|
|
|
.try_emit();
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case 3: {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::EventCreator::closure(td::ActorShared<LinkTokenSlave>(child_, token), &LinkTokenSlave::add, token)
|
2018-12-31 20:04:05 +01:00
|
|
|
.try_emit_later();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (cnt_ == 0) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(child_, &LinkTokenSlave::close);
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
|
|
|
yield();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup_shared() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(get_link_token() == 123);
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int cnt_;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorId<LinkTokenSlave> child_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, link_token) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(0, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto cnt = 100000;
|
|
|
|
scheduler.create_actor_unsafe<LinkTokenMasterActor>(0, "A", cnt).release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Actors, promise) {
|
|
|
|
int value = -1;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Promise<int> p1 = td::PromiseCreator::lambda([&](int x) { value = x; });
|
|
|
|
p1.set_error(td::Status::Error("Test error"));
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_EQ(0, value);
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Promise<td::int32> p2 = td::PromiseCreator::lambda([&](td::Result<td::int32> x) { value = 1; });
|
|
|
|
p2.set_error(td::Status::Error("Test error"));
|
2018-12-31 20:04:05 +01:00
|
|
|
ASSERT_EQ(1, value);
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class LaterSlave final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-10-15 13:21:30 +02:00
|
|
|
explicit LaterSlave(td::ActorShared<> parent) : parent_(std::move(parent)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-15 13:21:30 +02:00
|
|
|
td::ActorShared<> parent_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
sb << "A";
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(actor_id(this), &LaterSlave::finish);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void finish() {
|
|
|
|
sb << "B";
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class LaterMasterActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
int cnt_ = 3;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::vector<td::ActorOwn<LaterSlave>> children_;
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < cnt_; i++) {
|
2021-10-15 13:21:30 +02:00
|
|
|
children_.push_back(td::create_actor<LaterSlave>("B", actor_shared(this)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
yield();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
children_.clear();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void hangup_shared() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!--cnt_) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, later) {
|
|
|
|
sb.clear();
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(0, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler.create_actor_unsafe<LaterMasterActor>(0, "A").release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
ASSERT_STREQ(sb.as_cslice().c_str(), "AAABBB");
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class MultiPromise2 final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto promise = td::PromiseCreator::lambda([](td::Result<td::Unit> result) {
|
2018-12-31 20:04:05 +01:00
|
|
|
result.ensure();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
td::MultiPromiseActorSafe multi_promise{"MultiPromiseActor2"};
|
2018-12-31 20:04:05 +01:00
|
|
|
multi_promise.add_promise(std::move(promise));
|
|
|
|
for (int i = 0; i < 10; i++) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::create_actor<td::SleepActor>("Sleep", 0.1, multi_promise.get_promise()).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class MultiPromise1 final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
auto promise = td::PromiseCreator::lambda([](td::Result<td::Unit> result) {
|
2018-12-31 20:04:05 +01:00
|
|
|
CHECK(result.is_error());
|
2021-10-15 13:21:30 +02:00
|
|
|
td::create_actor<MultiPromise2>("B").release();
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
2021-10-15 13:21:30 +02:00
|
|
|
td::MultiPromiseActorSafe multi_promise{"MultiPromiseActor1"};
|
2018-12-31 20:04:05 +01:00
|
|
|
multi_promise.add_promise(std::move(promise));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, MultiPromise) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(0, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler.create_actor_unsafe<MultiPromise1>(0, "A").release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class FastPromise final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::PromiseFuture<int> pf;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto promise = pf.move_promise();
|
|
|
|
auto future = pf.move_future();
|
|
|
|
promise.set_value(123);
|
|
|
|
CHECK(future.move_as_ok() == 123);
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, FastPromise) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(0, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler.create_actor_unsafe<FastPromise>(0, "A").release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
}
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class StopInTeardown final : public td::Actor {
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, stop_in_teardown) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(0, 0);
|
2018-12-31 20:04:05 +01:00
|
|
|
scheduler.create_actor_unsafe<StopInTeardown>(0, "A").release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
}
|
2018-02-01 14:01:16 +01:00
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class AlwaysWaitForMailbox final : public td::Actor {
|
2018-02-01 14:01:16 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::create_actor<td::SleepActor>("Sleep", 0.1,
|
|
|
|
td::PromiseCreator::lambda([actor_id = actor_id(this), ptr = this](td::Unit) {
|
|
|
|
td::send_closure(actor_id, &AlwaysWaitForMailbox::g);
|
|
|
|
td::send_closure(actor_id, &AlwaysWaitForMailbox::g);
|
|
|
|
CHECK(!ptr->was_f_);
|
|
|
|
}))
|
2018-02-01 14:01:16 +01:00
|
|
|
.release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
was_f_ = true;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-02-01 14:01:16 +01:00
|
|
|
}
|
|
|
|
void g() {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(actor_id(this), &AlwaysWaitForMailbox::f);
|
2018-02-01 14:01:16 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool was_f_{false};
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, always_wait_for_mailbox) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(0, 0);
|
2018-02-01 14:01:16 +01:00
|
|
|
scheduler.create_actor_unsafe<AlwaysWaitForMailbox>(0, "A").release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
}
|
2018-08-16 15:56:16 +02:00
|
|
|
|
2018-09-07 02:41:21 +02:00
|
|
|
#if !TD_THREAD_UNSUPPORTED && !TD_EVENTFD_UNSUPPORTED
|
2018-08-16 15:56:16 +02:00
|
|
|
TEST(Actors, send_from_other_threads) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(1, 0);
|
2018-08-16 15:56:16 +02:00
|
|
|
int thread_n = 10;
|
2021-10-15 13:21:30 +02:00
|
|
|
class Listener final : public td::Actor {
|
2018-08-16 15:56:16 +02:00
|
|
|
public:
|
2018-09-07 02:41:21 +02:00
|
|
|
explicit Listener(int cnt) : cnt_(cnt) {
|
2018-08-16 15:56:16 +02:00
|
|
|
}
|
|
|
|
void dec() {
|
|
|
|
if (--cnt_ == 0) {
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-08-16 15:56:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int cnt_;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto A = scheduler.create_actor_unsafe<Listener>(1, "A", thread_n).release();
|
|
|
|
scheduler.start();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::vector<td::thread> threads(thread_n);
|
2018-08-16 15:56:16 +02:00
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread = td::thread([&A, &scheduler] {
|
|
|
|
auto guard = scheduler.get_send_guard();
|
2021-10-15 13:21:30 +02:00
|
|
|
td::send_closure(A, &Listener::dec);
|
2018-08-16 15:56:16 +02:00
|
|
|
});
|
|
|
|
}
|
|
|
|
while (scheduler.run_main(10)) {
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
}
|
2018-09-07 02:41:21 +02:00
|
|
|
#endif
|
2021-10-10 22:56:23 +02:00
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class DelayedCall final : public td::Actor {
|
2021-10-10 22:56:23 +02:00
|
|
|
public:
|
2021-10-21 11:51:16 +02:00
|
|
|
void on_called(int *step) {
|
|
|
|
CHECK(*step == 0);
|
|
|
|
*step = 1;
|
2021-10-10 22:56:23 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-10-15 13:21:30 +02:00
|
|
|
class MultiPromiseSendClosureLaterTest final : public td::Actor {
|
2021-10-10 22:56:23 +02:00
|
|
|
public:
|
|
|
|
void start_up() final {
|
2021-10-15 13:21:30 +02:00
|
|
|
delayed_call_ = td::create_actor<DelayedCall>("DelayedCall").release();
|
|
|
|
mpa_.add_promise(td::PromiseCreator::lambda([this](td::Unit) {
|
2021-10-21 11:51:16 +02:00
|
|
|
CHECK(step_ == 1);
|
|
|
|
step_++;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2021-10-10 22:56:23 +02:00
|
|
|
}));
|
|
|
|
auto lock = mpa_.get_promise();
|
2021-10-21 11:51:16 +02:00
|
|
|
td::send_closure_later(delayed_call_, &DelayedCall::on_called, &step_);
|
2021-10-15 13:21:30 +02:00
|
|
|
lock.set_value(td::Unit());
|
2021-10-10 22:56:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void tear_down() final {
|
2021-10-21 11:51:16 +02:00
|
|
|
CHECK(step_ == 2);
|
2021-10-10 22:56:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2021-10-21 11:51:16 +02:00
|
|
|
int step_ = 0;
|
2021-10-15 13:21:30 +02:00
|
|
|
td::MultiPromiseActor mpa_{"MultiPromiseActor"};
|
|
|
|
td::ActorId<DelayedCall> delayed_call_;
|
2021-10-10 22:56:23 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST(Actors, MultiPromiseSendClosureLater) {
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler scheduler(0, 0);
|
2021-10-10 22:56:23 +02:00
|
|
|
scheduler.create_actor_unsafe<MultiPromiseSendClosureLaterTest>(0, "MultiPromiseSendClosureLaterTest").release();
|
|
|
|
scheduler.start();
|
|
|
|
while (scheduler.run_main(1)) {
|
|
|
|
}
|
|
|
|
scheduler.finish();
|
|
|
|
}
|