2018-01-29 11:29:24 +01:00
|
|
|
//
|
2018-12-31 23:02:34 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2019
|
2018-01-29 11:29:24 +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)
|
|
|
|
//
|
2019-09-04 21:38:51 +02:00
|
|
|
#include "td/utils/Random.h"
|
2018-01-29 11:29:24 +01:00
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
2018-07-03 21:29:04 +02:00
|
|
|
#include "td/actor/actor.h"
|
2018-01-29 11:29:24 +01:00
|
|
|
#include "td/actor/Timeout.h"
|
|
|
|
|
|
|
|
using namespace td;
|
|
|
|
|
|
|
|
TEST(MultiTimeout, bug) {
|
|
|
|
ConcurrentScheduler sched;
|
|
|
|
int threads_n = 0;
|
|
|
|
sched.init(threads_n);
|
|
|
|
|
|
|
|
sched.start();
|
2018-09-27 03:19:03 +02:00
|
|
|
unique_ptr<MultiTimeout> multi_timeout;
|
2018-01-29 11:29:24 +01:00
|
|
|
struct Data {
|
|
|
|
MultiTimeout *multi_timeout;
|
|
|
|
};
|
|
|
|
Data data;
|
|
|
|
|
|
|
|
{
|
2018-09-18 15:43:16 +02:00
|
|
|
auto guard = sched.get_main_guard();
|
2018-09-27 03:19:03 +02:00
|
|
|
multi_timeout = make_unique<MultiTimeout>("MultiTimeout");
|
2018-01-29 11:29:24 +01:00
|
|
|
data.multi_timeout = multi_timeout.get();
|
|
|
|
multi_timeout->set_callback([](void *void_data, int64 key) {
|
2018-01-29 23:48:35 +01:00
|
|
|
auto &data = *static_cast<Data *>(void_data);
|
2018-01-29 11:29:24 +01:00
|
|
|
if (key == 1) {
|
|
|
|
data.multi_timeout->cancel_timeout(key + 1);
|
|
|
|
data.multi_timeout->set_timeout_in(key + 2, 1);
|
|
|
|
} else {
|
|
|
|
Scheduler::instance()->finish();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
multi_timeout->set_callback_data(&data);
|
|
|
|
multi_timeout->set_timeout_in(1, 1);
|
|
|
|
multi_timeout->set_timeout_in(2, 2);
|
|
|
|
}
|
|
|
|
|
|
|
|
while (sched.run_main(10)) {
|
|
|
|
// empty
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|
2019-09-04 21:38:51 +02:00
|
|
|
|
|
|
|
class TimeoutManager : public Actor {
|
|
|
|
public:
|
|
|
|
static int32 count;
|
|
|
|
|
|
|
|
TimeoutManager() {
|
|
|
|
count++;
|
|
|
|
|
|
|
|
test_timeout_.set_callback(on_test_timeout_callback);
|
|
|
|
test_timeout_.set_callback_data(static_cast<void *>(this));
|
|
|
|
}
|
|
|
|
TimeoutManager(const TimeoutManager &) = delete;
|
|
|
|
TimeoutManager &operator=(const TimeoutManager &) = delete;
|
|
|
|
TimeoutManager(TimeoutManager &&) = delete;
|
|
|
|
TimeoutManager &operator=(TimeoutManager &&) = delete;
|
|
|
|
~TimeoutManager() override {
|
|
|
|
count--;
|
|
|
|
LOG(INFO) << "Destroy TimeoutManager";
|
|
|
|
}
|
|
|
|
|
|
|
|
static void on_test_timeout_callback(void *timeout_manager_ptr, int64 id) {
|
|
|
|
CHECK(count >= 0);
|
|
|
|
if (count == 0) {
|
|
|
|
LOG(ERROR) << "Receive timeout after manager was closed";
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto manager = static_cast<TimeoutManager *>(timeout_manager_ptr);
|
|
|
|
send_closure_later(manager->actor_id(manager), &TimeoutManager::test_timeout);
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_timeout() {
|
|
|
|
CHECK(count > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
MultiTimeout test_timeout_{"TestTimeout"};
|
|
|
|
};
|
|
|
|
|
|
|
|
int32 TimeoutManager::count;
|
|
|
|
|
|
|
|
TEST(MultiTimeout, Destroy) {
|
|
|
|
SET_VERBOSITY_LEVEL(VERBOSITY_NAME(ERROR));
|
|
|
|
ConcurrentScheduler sched;
|
|
|
|
int threads_n = 0;
|
|
|
|
sched.init(threads_n);
|
|
|
|
|
|
|
|
ActorOwn<TimeoutManager> timeout_manager = sched.create_actor_unsafe<TimeoutManager>(0, "TimeoutManager");
|
|
|
|
TimeoutManager *manager = timeout_manager.get().get_actor_unsafe();
|
|
|
|
sched.start();
|
|
|
|
int cnt = 100;
|
|
|
|
while (sched.run_main(cnt == 100 || cnt <= 0 ? 0.001 : 10)) {
|
|
|
|
auto guard = sched.get_main_guard();
|
|
|
|
cnt--;
|
|
|
|
if (cnt > 0) {
|
|
|
|
for (int i = 0; i < 2; i++) {
|
|
|
|
manager->test_timeout_.set_timeout_in(Random::fast(0, 1000000000), Random::fast(2, 5) / 1000.0);
|
|
|
|
}
|
|
|
|
} else if (cnt == 0) {
|
|
|
|
timeout_manager.reset();
|
|
|
|
} else if (cnt == -10) {
|
|
|
|
Scheduler::instance()->finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sched.finish();
|
|
|
|
}
|