2018-12-31 20:04:05 +01:00
|
|
|
//
|
2024-01-01 01:07:21 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2024
|
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 "data.h"
|
|
|
|
|
2019-03-21 10:59:20 +01:00
|
|
|
#include "td/telegram/Client.h"
|
2019-04-26 01:21:12 +02:00
|
|
|
#include "td/telegram/ClientActor.h"
|
2019-07-31 17:04:38 +02:00
|
|
|
#include "td/telegram/files/PartsManager.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "td/actor/actor.h"
|
2020-11-23 01:24:36 +01:00
|
|
|
#include "td/actor/ConcurrentScheduler.h"
|
2020-08-13 15:57:28 +02:00
|
|
|
#include "td/actor/PromiseFuture.h"
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/base64.h"
|
|
|
|
#include "td/utils/BufferedFd.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/filesystem.h"
|
|
|
|
#include "td/utils/format.h"
|
|
|
|
#include "td/utils/logging.h"
|
|
|
|
#include "td/utils/misc.h"
|
|
|
|
#include "td/utils/port/FileFd.h"
|
|
|
|
#include "td/utils/port/path.h"
|
2020-10-08 00:28:01 +02:00
|
|
|
#include "td/utils/port/sleep.h"
|
2019-05-22 20:17:24 +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/Random.h"
|
|
|
|
#include "td/utils/Slice.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/Status.h"
|
|
|
|
#include "td/utils/tests.h"
|
|
|
|
|
2020-08-14 21:51:10 +02:00
|
|
|
#include <atomic>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <cstdio>
|
|
|
|
#include <functional>
|
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2020-10-08 17:30:36 +02:00
|
|
|
#include <mutex>
|
2020-07-30 03:04:57 +02:00
|
|
|
#include <set>
|
2018-12-31 20:04:05 +01:00
|
|
|
#include <utility>
|
|
|
|
|
|
|
|
template <class T>
|
|
|
|
static void check_td_error(T &result) {
|
2020-10-08 12:27:00 +02:00
|
|
|
LOG_CHECK(result->get_id() != td::td_api::error::ID) << to_string(result);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class TestClient final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
explicit TestClient(td::string name) : name_(std::move(name)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
struct Update {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::uint64 id;
|
|
|
|
td::tl_object_ptr<td::td_api::Object> object;
|
|
|
|
Update(td::uint64 id, td::tl_object_ptr<td::td_api::Object> object) : id(id), object(std::move(object)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
class Listener {
|
|
|
|
public:
|
|
|
|
Listener() = default;
|
|
|
|
Listener(const Listener &) = delete;
|
|
|
|
Listener &operator=(const Listener &) = delete;
|
|
|
|
Listener(Listener &&) = delete;
|
|
|
|
Listener &operator=(Listener &&) = delete;
|
|
|
|
virtual ~Listener() = default;
|
|
|
|
virtual void start_listen(TestClient *client) {
|
|
|
|
}
|
|
|
|
virtual void stop_listen() {
|
|
|
|
}
|
|
|
|
virtual void on_update(std::shared_ptr<Update> update) = 0;
|
|
|
|
};
|
2020-10-08 12:27:00 +02:00
|
|
|
void close(td::Promise<> close_promise) {
|
2018-12-31 20:04:05 +01:00
|
|
|
close_promise_ = std::move(close_promise);
|
2018-10-28 18:30:47 +01:00
|
|
|
td_client_.reset();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::unique_ptr<td::TdCallback> make_td_callback() {
|
2021-07-04 04:58:54 +02:00
|
|
|
class TdCallbackImpl final : public td::TdCallback {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
explicit TdCallbackImpl(td::ActorId<TestClient> client) : client_(client) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_result(td::uint64 id, td::tl_object_ptr<td::td_api::Object> result) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(client_, &TestClient::on_result, id, std::move(result));
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_error(td::uint64 id, td::tl_object_ptr<td::td_api::error> error) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(client_, &TestClient::on_error, id, std::move(error));
|
|
|
|
}
|
2018-09-15 18:33:27 +02:00
|
|
|
TdCallbackImpl(const TdCallbackImpl &) = delete;
|
|
|
|
TdCallbackImpl &operator=(const TdCallbackImpl &) = delete;
|
|
|
|
TdCallbackImpl(TdCallbackImpl &&) = delete;
|
|
|
|
TdCallbackImpl &operator=(TdCallbackImpl &&) = delete;
|
2021-07-03 22:51:36 +02:00
|
|
|
~TdCallbackImpl() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
send_closure(client_, &TestClient::on_closed);
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::ActorId<TestClient> client_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2020-10-08 12:27:00 +02:00
|
|
|
return td::make_unique<TdCallbackImpl>(actor_id(this));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
void add_listener(td::unique_ptr<Listener> listener) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto *ptr = listener.get();
|
|
|
|
listeners_.push_back(std::move(listener));
|
|
|
|
ptr->start_listen(this);
|
|
|
|
}
|
|
|
|
void remove_listener(Listener *listener) {
|
|
|
|
pending_remove_.push_back(listener);
|
|
|
|
}
|
|
|
|
void do_pending_remove_listeners() {
|
|
|
|
for (auto listener : pending_remove_) {
|
|
|
|
do_remove_listener(listener);
|
|
|
|
}
|
|
|
|
pending_remove_.clear();
|
|
|
|
}
|
|
|
|
void do_remove_listener(Listener *listener) {
|
|
|
|
for (size_t i = 0; i < listeners_.size(); i++) {
|
|
|
|
if (listeners_[i].get() == listener) {
|
|
|
|
listener->stop_listen();
|
|
|
|
listeners_.erase(listeners_.begin() + i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
void on_result(td::uint64 id, td::tl_object_ptr<td::td_api::Object> result) {
|
2018-12-31 20:04:05 +01:00
|
|
|
on_update(std::make_shared<Update>(id, std::move(result)));
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
void on_error(td::uint64 id, td::tl_object_ptr<td::td_api::error> error) {
|
2018-12-31 20:04:05 +01:00
|
|
|
on_update(std::make_shared<Update>(id, std::move(error)));
|
|
|
|
}
|
|
|
|
void on_update(std::shared_ptr<Update> update) {
|
|
|
|
for (auto &listener : listeners_) {
|
|
|
|
listener->on_update(update);
|
|
|
|
}
|
|
|
|
do_pending_remove_listeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
void on_closed() {
|
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::rmrf(name_).ignore();
|
2019-09-05 17:32:45 +02:00
|
|
|
auto old_context = set_context(std::make_shared<td::ActorContext>());
|
2018-12-31 20:04:05 +01:00
|
|
|
set_tag(name_);
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(INFO) << "Start up!";
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td_client_ = td::create_actor<td::ClientActor>("Td-proxy", make_td_callback());
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::ActorOwn<td::ClientActor> td_client_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string name_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::vector<td::unique_ptr<Listener>> listeners_;
|
|
|
|
td::vector<Listener *> pending_remove_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::Promise<> close_promise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2020-12-22 13:51:57 +01:00
|
|
|
class TestClinetTask : public TestClient::Listener {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2021-07-03 22:51:36 +02:00
|
|
|
void on_update(std::shared_ptr<TestClient::Update> update) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto it = sent_queries_.find(update->id);
|
|
|
|
if (it != sent_queries_.end()) {
|
|
|
|
it->second(std::move(update->object));
|
|
|
|
sent_queries_.erase(it);
|
|
|
|
}
|
|
|
|
process_update(update);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_listen(TestClient *client) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
client_ = client;
|
|
|
|
start_up();
|
|
|
|
}
|
|
|
|
virtual void process_update(std::shared_ptr<TestClient::Update> update) {
|
|
|
|
}
|
|
|
|
|
|
|
|
template <class F>
|
2020-10-08 12:27:00 +02:00
|
|
|
void send_query(td::tl_object_ptr<td::td_api::Function> function, F callback) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto id = current_query_id_++;
|
|
|
|
sent_queries_[id] = std::forward<F>(callback);
|
2020-10-08 12:27:00 +02:00
|
|
|
send_closure(client_->td_client_, &td::ClientActor::request, id, std::move(function));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
2020-10-08 12:27:00 +02:00
|
|
|
std::map<td::uint64, std::function<void(td::tl_object_ptr<td::td_api::Object>)>> sent_queries_;
|
2018-10-26 16:11:20 +02:00
|
|
|
TestClient *client_ = nullptr;
|
2020-10-08 12:27:00 +02:00
|
|
|
td::uint64 current_query_id_ = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
virtual void start_up() {
|
|
|
|
}
|
|
|
|
void stop() {
|
|
|
|
client_->remove_listener(this);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class DoAuthentication final : public TestClinetTask {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
DoAuthentication(td::string name, td::string phone, td::string code, td::Promise<> promise)
|
2018-12-31 20:04:05 +01:00
|
|
|
: name_(std::move(name)), phone_(std::move(phone)), code_(std::move(code)), promise_(std::move(promise)) {
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2023-01-12 20:25:47 +01:00
|
|
|
send_query(td::make_tl_object<td::td_api::getOption>("version"),
|
|
|
|
[](auto res) { LOG(INFO) << td::td_api::to_string(res); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
void process_authorization_state(td::tl_object_ptr<td::td_api::Object> authorization_state) {
|
|
|
|
td::tl_object_ptr<td::td_api::Function> function;
|
2018-12-31 20:04:05 +01:00
|
|
|
switch (authorization_state->get_id()) {
|
2020-10-08 12:27:00 +02:00
|
|
|
case td::td_api::authorizationStateWaitPhoneNumber::ID:
|
|
|
|
function = td::make_tl_object<td::td_api::setAuthenticationPhoneNumber>(phone_, nullptr);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2022-09-08 07:48:11 +02:00
|
|
|
case td::td_api::authorizationStateWaitEmailAddress::ID:
|
|
|
|
function = td::make_tl_object<td::td_api::setAuthenticationEmailAddress>("alice_test@gmail.com");
|
|
|
|
break;
|
|
|
|
case td::td_api::authorizationStateWaitEmailCode::ID:
|
|
|
|
function = td::make_tl_object<td::td_api::checkAuthenticationEmailCode>(
|
|
|
|
td::make_tl_object<td::td_api::emailAddressAuthenticationCode>(code_));
|
|
|
|
break;
|
2020-10-08 12:27:00 +02:00
|
|
|
case td::td_api::authorizationStateWaitCode::ID:
|
|
|
|
function = td::make_tl_object<td::td_api::checkAuthenticationCode>(code_);
|
2019-07-16 21:08:34 +02:00
|
|
|
break;
|
2020-10-08 12:27:00 +02:00
|
|
|
case td::td_api::authorizationStateWaitRegistration::ID:
|
|
|
|
function = td::make_tl_object<td::td_api::registerUser>(name_, "");
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
2020-10-08 12:27:00 +02:00
|
|
|
case td::td_api::authorizationStateWaitTdlibParameters::ID: {
|
2022-09-07 21:19:08 +02:00
|
|
|
auto request = td::td_api::make_object<td::td_api::setTdlibParameters>();
|
|
|
|
request->use_test_dc_ = true;
|
|
|
|
request->database_directory_ = name_ + TD_DIR_SLASH;
|
|
|
|
request->use_message_database_ = true;
|
|
|
|
request->use_secret_chats_ = true;
|
|
|
|
request->api_id_ = 94575;
|
|
|
|
request->api_hash_ = "a3406de8d171bb422bb6ddf3bbd800e2";
|
|
|
|
request->system_language_code_ = "en";
|
|
|
|
request->device_model_ = "Desktop";
|
|
|
|
request->application_version_ = "tdclient-test";
|
|
|
|
request->enable_storage_optimizer_ = true;
|
|
|
|
function = std::move(request);
|
2018-12-31 20:04:05 +01:00
|
|
|
break;
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
case td::td_api::authorizationStateReady::ID:
|
2018-12-31 20:04:05 +01:00
|
|
|
on_authorization_ready();
|
|
|
|
return;
|
|
|
|
default:
|
2019-02-12 22:47:48 +01:00
|
|
|
LOG(ERROR) << "Unexpected authorization state " << to_string(authorization_state);
|
|
|
|
UNREACHABLE();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(std::move(function), [](auto res) { LOG_CHECK(res->get_id() == td::td_api::ok::ID) << to_string(res); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
void on_authorization_ready() {
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(INFO) << "Authorization is completed";
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string name_;
|
|
|
|
td::string phone_;
|
|
|
|
td::string code_;
|
|
|
|
td::Promise<> promise_;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void process_update(std::shared_ptr<TestClient::Update> update) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!update->object) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
if (update->object->get_id() == td::td_api::updateAuthorizationState::ID) {
|
|
|
|
auto update_authorization_state = td::move_tl_object_as<td::td_api::updateAuthorizationState>(update->object);
|
|
|
|
process_authorization_state(std::move(update_authorization_state->authorization_state_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class SetUsername final : public TestClinetTask {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
SetUsername(td::string username, td::Promise<> promise)
|
|
|
|
: username_(std::move(username)), promise_(std::move(promise)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string username_;
|
|
|
|
td::Promise<> promise_;
|
2021-09-03 11:27:59 +02:00
|
|
|
td::int64 self_id_ = 0;
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string tag_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::getMe>(), [this](auto res) { this->process_me_user(std::move(res)); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
void process_me_user(td::tl_object_ptr<td::td_api::Object> res) {
|
|
|
|
CHECK(res->get_id() == td::td_api::user::ID);
|
|
|
|
auto user = td::move_tl_object_as<td::td_api::user>(res);
|
2018-12-31 20:04:05 +01:00
|
|
|
self_id_ = user->id_;
|
2022-10-12 20:04:18 +02:00
|
|
|
auto current_username = user->usernames_ != nullptr ? user->usernames_->editable_username_ : td::string();
|
|
|
|
if (current_username != username_) {
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(INFO) << "Set username: " << username_;
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::setUsername>(username_), [this](auto res) {
|
|
|
|
CHECK(res->get_id() == td::td_api::ok::ID);
|
2018-12-31 20:04:05 +01:00
|
|
|
this->send_self_message();
|
|
|
|
});
|
|
|
|
} else {
|
|
|
|
send_self_message();
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void send_self_message() {
|
2020-10-08 12:27:00 +02:00
|
|
|
tag_ = PSTRING() << td::format::as_hex(td::Random::secure_int64());
|
|
|
|
|
|
|
|
send_query(td::make_tl_object<td::td_api::createPrivateChat>(self_id_, false), [this](auto res) {
|
|
|
|
CHECK(res->get_id() == td::td_api::chat::ID);
|
|
|
|
auto chat = td::move_tl_object_as<td::td_api::chat>(res);
|
|
|
|
this->send_query(td::make_tl_object<td::td_api::sendMessage>(
|
2023-06-12 14:30:07 +02:00
|
|
|
chat->id_, 0, nullptr, nullptr, nullptr,
|
2020-10-08 12:27:00 +02:00
|
|
|
td::make_tl_object<td::td_api::inputMessageText>(
|
|
|
|
td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " INIT", td::Auto()),
|
2023-10-16 19:12:33 +02:00
|
|
|
nullptr, false)),
|
2020-10-08 12:27:00 +02:00
|
|
|
[](auto res) {});
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void process_update(std::shared_ptr<TestClient::Update> update) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!update->object) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
if (update->object->get_id() == td::td_api::updateMessageSendSucceeded::ID) {
|
|
|
|
auto updateNewMessage = td::move_tl_object_as<td::td_api::updateMessageSendSucceeded>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &message = updateNewMessage->message_;
|
2020-10-08 12:27:00 +02:00
|
|
|
if (message->content_->get_id() == td::td_api::messageText::ID) {
|
|
|
|
auto messageText = td::move_tl_object_as<td::td_api::messageText>(message->content_);
|
2018-01-30 18:06:54 +01:00
|
|
|
auto text = messageText->text_->text_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (text.substr(0, tag_.size()) == tag_) {
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(INFO) << "Receive self-message";
|
2018-12-31 20:04:05 +01:00
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class CheckTestA final : public TestClinetTask {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
CheckTestA(td::string tag, td::Promise<> promise) : tag_(std::move(tag)), promise_(std::move(promise)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string tag_;
|
|
|
|
td::Promise<> promise_;
|
|
|
|
td::string previous_text_;
|
2018-12-31 20:04:05 +01:00
|
|
|
int cnt_ = 20;
|
2020-10-08 12:27:00 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void process_update(std::shared_ptr<TestClient::Update> update) final {
|
2020-10-08 12:27:00 +02:00
|
|
|
if (update->object->get_id() == td::td_api::updateNewMessage::ID) {
|
|
|
|
auto updateNewMessage = td::move_tl_object_as<td::td_api::updateNewMessage>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &message = updateNewMessage->message_;
|
2020-10-08 12:27:00 +02:00
|
|
|
if (message->content_->get_id() == td::td_api::messageText::ID) {
|
|
|
|
auto messageText = td::move_tl_object_as<td::td_api::messageText>(message->content_);
|
2018-01-30 18:06:54 +01:00
|
|
|
auto text = messageText->text_->text_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (text.substr(0, tag_.size()) == tag_) {
|
2020-10-08 12:27:00 +02:00
|
|
|
LOG_CHECK(text > previous_text_) << td::tag("now", text) << td::tag("previous", previous_text_);
|
2018-12-31 20:04:05 +01:00
|
|
|
previous_text_ = text;
|
|
|
|
cnt_--;
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(INFO) << "Receive " << td::tag("text", text) << td::tag("left", cnt_);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (cnt_ == 0) {
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class TestA final : public TestClinetTask {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
TestA(td::string tag, td::string username) : tag_(std::move(tag)), username_(std::move(username)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::searchPublicChat>(username_), [this](auto res) {
|
|
|
|
CHECK(res->get_id() == td::td_api::chat::ID);
|
|
|
|
auto chat = td::move_tl_object_as<td::td_api::chat>(res);
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < 20; i++) {
|
2020-10-08 12:27:00 +02:00
|
|
|
this->send_query(
|
|
|
|
td::make_tl_object<td::td_api::sendMessage>(
|
2023-06-12 14:30:07 +02:00
|
|
|
chat->id_, 0, nullptr, nullptr, nullptr,
|
2020-10-08 12:27:00 +02:00
|
|
|
td::make_tl_object<td::td_api::inputMessageText>(
|
|
|
|
td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " " << (1000 + i), td::Auto()),
|
2023-10-16 19:12:33 +02:00
|
|
|
nullptr, false)),
|
2020-10-08 12:27:00 +02:00
|
|
|
[&](auto res) { this->stop(); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string tag_;
|
|
|
|
td::string username_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class TestSecretChat final : public TestClinetTask {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
TestSecretChat(td::string tag, td::string username) : tag_(std::move(tag)), username_(std::move(username)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto f = [this](auto res) {
|
2020-10-08 12:27:00 +02:00
|
|
|
CHECK(res->get_id() == td::td_api::chat::ID);
|
|
|
|
auto chat = td::move_tl_object_as<td::td_api::chat>(res);
|
2018-12-31 20:04:05 +01:00
|
|
|
this->chat_id_ = chat->id_;
|
2020-10-08 12:27:00 +02:00
|
|
|
this->secret_chat_id_ = td::move_tl_object_as<td::td_api::chatTypeSecret>(chat->type_)->secret_chat_id_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::searchPublicChat>(username_), [this, f = std::move(f)](auto res) mutable {
|
|
|
|
CHECK(res->get_id() == td::td_api::chat::ID);
|
|
|
|
auto chat = td::move_tl_object_as<td::td_api::chat>(res);
|
|
|
|
CHECK(chat->type_->get_id() == td::td_api::chatTypePrivate::ID);
|
|
|
|
auto info = td::move_tl_object_as<td::td_api::chatTypePrivate>(chat->type_);
|
|
|
|
this->send_query(td::make_tl_object<td::td_api::createNewSecretChat>(info->user_id_), std::move(f));
|
2018-12-31 20:04:05 +01:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void process_update(std::shared_ptr<TestClient::Update> update) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!update->object) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
if (update->object->get_id() == td::td_api::updateSecretChat::ID) {
|
|
|
|
auto update_secret_chat = td::move_tl_object_as<td::td_api::updateSecretChat>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (update_secret_chat->secret_chat_->id_ != secret_chat_id_ ||
|
2020-10-08 12:27:00 +02:00
|
|
|
update_secret_chat->secret_chat_->state_->get_id() != td::td_api::secretChatStateReady::ID) {
|
2018-12-31 20:04:05 +01:00
|
|
|
return;
|
|
|
|
}
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(INFO) << "Send encrypted messages";
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 0; i < 20; i++) {
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(
|
|
|
|
td::make_tl_object<td::td_api::sendMessage>(
|
2023-06-12 14:30:07 +02:00
|
|
|
chat_id_, 0, nullptr, nullptr, nullptr,
|
2020-10-08 12:27:00 +02:00
|
|
|
td::make_tl_object<td::td_api::inputMessageText>(
|
|
|
|
td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " " << (1000 + i), td::Auto()),
|
2023-10-16 19:12:33 +02:00
|
|
|
nullptr, false)),
|
2020-10-08 12:27:00 +02:00
|
|
|
[](auto res) {});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string tag_;
|
|
|
|
td::string username_;
|
|
|
|
td::int64 secret_chat_id_ = 0;
|
|
|
|
td::int64 chat_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class TestFileGenerated final : public TestClinetTask {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
TestFileGenerated(td::string tag, td::string username) : tag_(std::move(tag)), username_(std::move(username)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void process_update(std::shared_ptr<TestClient::Update> update) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!update->object) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
if (update->object->get_id() == td::td_api::updateNewMessage::ID) {
|
|
|
|
auto updateNewMessage = td::move_tl_object_as<td::td_api::updateNewMessage>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &message = updateNewMessage->message_;
|
|
|
|
chat_id_ = message->chat_id_;
|
2020-10-08 12:27:00 +02:00
|
|
|
if (message->content_->get_id() == td::td_api::messageText::ID) {
|
|
|
|
auto messageText = td::move_tl_object_as<td::td_api::messageText>(message->content_);
|
2018-01-30 18:06:54 +01:00
|
|
|
auto text = messageText->text_->text_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (text.substr(0, tag_.size()) == tag_) {
|
|
|
|
if (text.substr(tag_.size() + 1) == "ONE_FILE") {
|
|
|
|
return one_file();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
} else if (update->object->get_id() == td::td_api::updateFileGenerationStart::ID) {
|
|
|
|
auto info = td::move_tl_object_as<td::td_api::updateFileGenerationStart>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
generate_file(info->generation_id_, info->original_path_, info->destination_path_, info->conversion_);
|
2020-10-08 12:27:00 +02:00
|
|
|
} else if (update->object->get_id() == td::td_api::updateFile::ID) {
|
|
|
|
auto file = td::move_tl_object_as<td::td_api::updateFile>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(INFO) << to_string(file);
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void one_file() {
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(ERROR) << "Start one_file test";
|
2020-10-08 12:27:00 +02:00
|
|
|
auto file_path = PSTRING() << "test_documents" << TD_DIR_SLASH << "a.txt";
|
|
|
|
td::mkpath(file_path).ensure();
|
2018-12-31 20:04:05 +01:00
|
|
|
auto raw_file =
|
2020-10-08 12:27:00 +02:00
|
|
|
td::FileFd::open(file_path, td::FileFd::Flags::Create | td::FileFd::Flags::Truncate | td::FileFd::Flags::Write)
|
|
|
|
.move_as_ok();
|
|
|
|
auto file = td::BufferedFd<td::FileFd>(std::move(raw_file));
|
2018-12-31 20:04:05 +01:00
|
|
|
for (int i = 1; i < 100000; i++) {
|
|
|
|
file.write(PSLICE() << i << "\n").ensure();
|
|
|
|
}
|
|
|
|
file.flush_write().ensure(); // important
|
|
|
|
file.close();
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::sendMessage>(
|
2023-06-12 14:30:07 +02:00
|
|
|
chat_id_, 0, nullptr, nullptr, nullptr,
|
2020-10-08 12:27:00 +02:00
|
|
|
td::make_tl_object<td::td_api::inputMessageDocument>(
|
|
|
|
td::make_tl_object<td::td_api::inputFileGenerated>(file_path, "square", 0),
|
|
|
|
td::make_tl_object<td::td_api::inputThumbnail>(
|
|
|
|
td::make_tl_object<td::td_api::inputFileGenerated>(file_path, "thumbnail", 0), 0, 0),
|
|
|
|
true, td::make_tl_object<td::td_api::formattedText>(tag_, td::Auto()))),
|
|
|
|
[](auto res) { check_td_error(res); });
|
|
|
|
|
|
|
|
send_query(td::make_tl_object<td::td_api::sendMessage>(
|
2023-06-12 14:30:07 +02:00
|
|
|
chat_id_, 0, nullptr, nullptr, nullptr,
|
2020-10-08 12:27:00 +02:00
|
|
|
td::make_tl_object<td::td_api::inputMessageDocument>(
|
|
|
|
td::make_tl_object<td::td_api::inputFileGenerated>(file_path, "square", 0), nullptr, true,
|
|
|
|
td::make_tl_object<td::td_api::formattedText>(tag_, td::Auto()))),
|
|
|
|
[](auto res) { check_td_error(res); });
|
|
|
|
}
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class GenerateFile final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-12-22 13:51:57 +01:00
|
|
|
GenerateFile(TestClinetTask *parent, td::int64 id, td::string original_path, td::string destination_path,
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string conversion)
|
2018-12-31 20:04:05 +01:00
|
|
|
: parent_(parent)
|
|
|
|
, id_(id)
|
|
|
|
, original_path_(std::move(original_path))
|
|
|
|
, destination_path_(std::move(destination_path))
|
|
|
|
, conversion_(std::move(conversion)) {
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-12-22 13:51:57 +01:00
|
|
|
TestClinetTask *parent_;
|
2020-10-08 12:27:00 +02:00
|
|
|
td::int64 id_;
|
|
|
|
td::string original_path_;
|
|
|
|
td::string destination_path_;
|
|
|
|
td::string conversion_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
FILE *from = nullptr;
|
|
|
|
FILE *to = nullptr;
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
from = std::fopen(original_path_.c_str(), "rb");
|
|
|
|
CHECK(from);
|
|
|
|
to = std::fopen(destination_path_.c_str(), "wb");
|
|
|
|
CHECK(to);
|
|
|
|
yield();
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void loop() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
int cnt = 0;
|
|
|
|
while (true) {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::uint32 x;
|
2018-12-31 20:04:05 +01:00
|
|
|
auto r = std::fscanf(from, "%u", &x);
|
|
|
|
if (r != 1) {
|
|
|
|
return stop();
|
|
|
|
}
|
|
|
|
std::fprintf(to, "%u\n", x * x);
|
|
|
|
if (++cnt >= 10000) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
auto ready = std::ftell(to);
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(ERROR) << "Ready: " << ready;
|
2020-10-08 12:27:00 +02:00
|
|
|
parent_->send_query(td::make_tl_object<td::td_api::setFileGenerationProgress>(
|
|
|
|
id_, 1039823 /*yeah, exact size of this file*/, td::narrow_cast<td::int32>(ready)),
|
2018-12-31 20:04:05 +01:00
|
|
|
[](auto result) { check_td_error(result); });
|
|
|
|
set_timeout_in(0.02);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void tear_down() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
std::fclose(from);
|
|
|
|
std::fclose(to);
|
2020-10-08 12:27:00 +02:00
|
|
|
parent_->send_query(td::make_tl_object<td::td_api::finishFileGeneration>(id_, nullptr),
|
2018-12-31 20:04:05 +01:00
|
|
|
[](auto result) { check_td_error(result); });
|
|
|
|
}
|
|
|
|
};
|
2020-10-08 12:27:00 +02:00
|
|
|
|
2021-10-21 11:51:16 +02:00
|
|
|
void generate_file(td::int64 id, const td::string &original_path, const td::string &destination_path,
|
|
|
|
const td::string &conversion) {
|
2020-10-08 12:27:00 +02:00
|
|
|
LOG(ERROR) << "Generate file " << td::tag("id", id) << td::tag("original_path", original_path)
|
|
|
|
<< td::tag("destination_path", destination_path) << td::tag("conversion", conversion);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (conversion == "square") {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::create_actor<GenerateFile>("GenerateFile", this, id, original_path, destination_path, conversion).release();
|
2018-12-31 20:04:05 +01:00
|
|
|
} else if (conversion == "thumbnail") {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::write_file(destination_path, td::base64url_decode(td::Slice(thumbnail, thumbnail_size)).ok()).ensure();
|
|
|
|
send_query(td::make_tl_object<td::td_api::finishFileGeneration>(id, nullptr),
|
|
|
|
[](auto result) { check_td_error(result); });
|
2018-12-31 20:04:05 +01:00
|
|
|
} else {
|
2020-10-08 12:27:00 +02:00
|
|
|
LOG(FATAL) << "Unknown " << td::tag("conversion", conversion);
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string tag_;
|
|
|
|
td::string username_;
|
|
|
|
td::int64 chat_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class CheckTestC final : public TestClinetTask {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
CheckTestC(td::string username, td::string tag, td::Promise<> promise)
|
2018-12-31 20:04:05 +01:00
|
|
|
: username_(std::move(username)), tag_(std::move(tag)), promise_(std::move(promise)) {
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::searchPublicChat>(username_), [this](auto res) {
|
|
|
|
CHECK(res->get_id() == td::td_api::chat::ID);
|
|
|
|
auto chat = td::move_tl_object_as<td::td_api::chat>(res);
|
2018-12-31 20:04:05 +01:00
|
|
|
chat_id_ = chat->id_;
|
|
|
|
this->one_file();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string username_;
|
|
|
|
td::string tag_;
|
|
|
|
td::Promise<> promise_;
|
|
|
|
td::int64 chat_id_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
|
|
|
void one_file() {
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::sendMessage>(
|
2023-06-12 14:30:07 +02:00
|
|
|
chat_id_, 0, nullptr, nullptr, nullptr,
|
2020-10-08 12:27:00 +02:00
|
|
|
td::make_tl_object<td::td_api::inputMessageText>(
|
|
|
|
td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " ONE_FILE", td::Auto()),
|
2023-10-16 19:12:33 +02:00
|
|
|
nullptr, false)),
|
2020-10-08 12:27:00 +02:00
|
|
|
[](auto res) { check_td_error(res); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void process_update(std::shared_ptr<TestClient::Update> update) final {
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!update->object) {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
if (update->object->get_id() == td::td_api::updateNewMessage::ID) {
|
|
|
|
auto updateNewMessage = td::move_tl_object_as<td::td_api::updateNewMessage>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
auto &message = updateNewMessage->message_;
|
2020-10-08 12:27:00 +02:00
|
|
|
if (message->content_->get_id() == td::td_api::messageDocument::ID) {
|
|
|
|
auto messageDocument = td::move_tl_object_as<td::td_api::messageDocument>(message->content_);
|
2018-01-30 18:06:54 +01:00
|
|
|
auto text = messageDocument->caption_->text_;
|
2018-12-31 20:04:05 +01:00
|
|
|
if (text.substr(0, tag_.size()) == tag_) {
|
|
|
|
file_id_to_check_ = messageDocument->document_->document_->id_;
|
2023-02-26 12:22:31 +01:00
|
|
|
LOG(ERROR) << "Receive file " << to_string(messageDocument->document_->document_);
|
2020-10-08 12:27:00 +02:00
|
|
|
send_query(td::make_tl_object<td::td_api::downloadFile>(file_id_to_check_, 1, 0, 0, false),
|
|
|
|
[](auto res) { check_td_error(res); });
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
} else if (update->object->get_id() == td::td_api::updateFile::ID) {
|
|
|
|
auto updateFile = td::move_tl_object_as<td::td_api::updateFile>(update->object);
|
2018-12-31 20:04:05 +01:00
|
|
|
if (updateFile->file_->id_ == file_id_to_check_ && (updateFile->file_->local_->is_downloading_completed_)) {
|
|
|
|
check_file(updateFile->file_->local_->path_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
void check_file(td::CSlice path) {
|
2018-12-31 20:04:05 +01:00
|
|
|
FILE *from = std::fopen(path.c_str(), "rb");
|
|
|
|
CHECK(from);
|
2020-10-08 12:27:00 +02:00
|
|
|
td::uint32 x;
|
|
|
|
td::uint32 y = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
while (std::fscanf(from, "%u", &x) == 1) {
|
|
|
|
CHECK(x == y * y);
|
|
|
|
y++;
|
|
|
|
}
|
|
|
|
std::fclose(from);
|
|
|
|
stop();
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
td::int32 file_id_to_check_ = 0;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class LoginTestActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
explicit LoginTestActor(td::Status *status) : status_(status) {
|
|
|
|
*status_ = td::Status::OK();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2020-10-08 12:27:00 +02:00
|
|
|
td::Status *status_;
|
|
|
|
td::ActorOwn<TestClient> alice_;
|
|
|
|
td::ActorOwn<TestClient> bob_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string alice_phone_ = "9996636437";
|
|
|
|
td::string bob_phone_ = "9996636438";
|
|
|
|
td::string alice_username_ = "alice_" + alice_phone_;
|
|
|
|
td::string bob_username_ = "bob_" + bob_phone_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string stage_name_;
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
void begin_stage(td::string stage_name, double timeout) {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(WARNING) << "Begin stage '" << stage_name << "'";
|
|
|
|
stage_name_ = std::move(stage_name);
|
|
|
|
set_timeout_in(timeout);
|
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
begin_stage("Logging in", 160);
|
2020-10-08 12:27:00 +02:00
|
|
|
alice_ = td::create_actor<TestClient>("AliceClient", "alice");
|
|
|
|
bob_ = td::create_actor<TestClient>("BobClient", "bob");
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(alice_, &TestClient::add_listener,
|
|
|
|
td::make_unique<DoAuthentication>(
|
|
|
|
"alice", alice_phone_, "33333",
|
2022-06-27 01:57:56 +02:00
|
|
|
td::create_event_promise(self_closure(this, &LoginTestActor::start_up_fence_dec))));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(bob_, &TestClient::add_listener,
|
|
|
|
td::make_unique<DoAuthentication>(
|
|
|
|
"bob", bob_phone_, "33333",
|
2022-06-27 01:57:56 +02:00
|
|
|
td::create_event_promise(self_closure(this, &LoginTestActor::start_up_fence_dec))));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int start_up_fence_ = 3;
|
|
|
|
void start_up_fence_dec() {
|
|
|
|
--start_up_fence_;
|
|
|
|
if (start_up_fence_ == 0) {
|
|
|
|
init();
|
|
|
|
} else if (start_up_fence_ == 1) {
|
|
|
|
return init();
|
2021-07-04 04:58:54 +02:00
|
|
|
class WaitActor final : public td::Actor {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
2020-10-08 12:27:00 +02:00
|
|
|
WaitActor(double timeout, td::Promise<> promise) : timeout_(timeout), promise_(std::move(promise)) {
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void start_up() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
set_timeout_in(timeout_);
|
|
|
|
}
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
double timeout_;
|
2020-10-08 12:27:00 +02:00
|
|
|
td::Promise<> promise_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2020-10-08 12:27:00 +02:00
|
|
|
td::create_actor<WaitActor>("WaitActor", 2,
|
2022-06-27 01:57:56 +02:00
|
|
|
td::create_event_promise(self_closure(this, &LoginTestActor::start_up_fence_dec)))
|
2018-10-27 02:03:15 +02:00
|
|
|
.release();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void init() {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(alice_, &TestClient::add_listener,
|
2022-06-27 01:57:56 +02:00
|
|
|
td::make_unique<SetUsername>(alice_username_, td::create_event_promise(self_closure(
|
2020-10-08 12:27:00 +02:00
|
|
|
this, &LoginTestActor::init_fence_dec))));
|
|
|
|
td::send_closure(bob_, &TestClient::add_listener,
|
2022-06-27 01:57:56 +02:00
|
|
|
td::make_unique<SetUsername>(
|
|
|
|
bob_username_, td::create_event_promise(self_closure(this, &LoginTestActor::init_fence_dec))));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int init_fence_ = 2;
|
|
|
|
void init_fence_dec() {
|
|
|
|
if (--init_fence_ == 0) {
|
|
|
|
test_a();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
int test_a_fence_ = 2;
|
2018-12-31 20:04:05 +01:00
|
|
|
void test_a_fence() {
|
|
|
|
if (--test_a_fence_ == 0) {
|
|
|
|
test_b();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_a() {
|
|
|
|
begin_stage("Ready to create chats", 80);
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string alice_tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
|
|
|
|
td::string bob_tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(bob_, &TestClient::add_listener,
|
|
|
|
td::make_unique<CheckTestA>(
|
2022-06-27 01:57:56 +02:00
|
|
|
alice_tag, td::create_event_promise(self_closure(this, &LoginTestActor::test_a_fence))));
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(alice_, &TestClient::add_listener,
|
|
|
|
td::make_unique<CheckTestA>(
|
2022-06-27 01:57:56 +02:00
|
|
|
bob_tag, td::create_event_promise(self_closure(this, &LoginTestActor::test_a_fence))));
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestA>(alice_tag, bob_username_));
|
|
|
|
td::send_closure(bob_, &TestClient::add_listener, td::make_unique<TestA>(bob_tag, alice_username_));
|
|
|
|
// td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestChat>(bob_username_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2021-07-03 22:51:36 +02:00
|
|
|
void timeout_expired() final {
|
2018-12-31 20:04:05 +01:00
|
|
|
LOG(FATAL) << "Timeout expired in stage '" << stage_name_ << "'";
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
int test_b_fence_ = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
void test_b_fence() {
|
|
|
|
if (--test_b_fence_ == 0) {
|
|
|
|
test_c();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
int test_c_fence_ = 1;
|
2018-12-31 20:04:05 +01:00
|
|
|
void test_c_fence() {
|
|
|
|
if (--test_c_fence_ == 0) {
|
|
|
|
finish();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void test_b() {
|
|
|
|
begin_stage("Create secret chat", 40);
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(
|
2018-12-31 20:04:05 +01:00
|
|
|
bob_, &TestClient::add_listener,
|
2022-06-27 01:57:56 +02:00
|
|
|
td::make_unique<CheckTestA>(tag, td::create_event_promise(self_closure(this, &LoginTestActor::test_b_fence))));
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestSecretChat>(tag, bob_username_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void test_c() {
|
|
|
|
begin_stage("Send generated file", 240);
|
2020-10-08 12:27:00 +02:00
|
|
|
td::string tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(
|
|
|
|
bob_, &TestClient::add_listener,
|
|
|
|
td::make_unique<CheckTestC>(alice_username_, tag,
|
2022-06-27 01:57:56 +02:00
|
|
|
td::create_event_promise(self_closure(this, &LoginTestActor::test_c_fence))));
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestFileGenerated>(tag, bob_username_));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
int finish_fence_ = 2;
|
2018-12-31 20:04:05 +01:00
|
|
|
void finish_fence() {
|
|
|
|
finish_fence_--;
|
|
|
|
if (finish_fence_ == 0) {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::Scheduler::instance()->finish();
|
2018-12-31 20:04:05 +01:00
|
|
|
stop();
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 12:27:00 +02:00
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
void finish() {
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(alice_, &TestClient::close,
|
2022-06-27 01:57:56 +02:00
|
|
|
td::create_event_promise(self_closure(this, &LoginTestActor::finish_fence)));
|
2020-10-08 12:27:00 +02:00
|
|
|
td::send_closure(bob_, &TestClient::close,
|
2022-06-27 01:57:56 +02:00
|
|
|
td::create_event_promise(self_closure(this, &LoginTestActor::finish_fence)));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-04 04:58:54 +02:00
|
|
|
class Tdclient_login final : public td::Test {
|
2018-12-31 20:04:05 +01:00
|
|
|
public:
|
|
|
|
using Test::Test;
|
|
|
|
bool step() final {
|
|
|
|
if (!is_inited_) {
|
|
|
|
sched_.create_actor_unsafe<LoginTestActor>(0, "LoginTestActor", &result_).release();
|
|
|
|
sched_.start();
|
|
|
|
is_inited_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool ret = sched_.run_main(10);
|
|
|
|
if (ret) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
sched_.finish();
|
|
|
|
if (result_.is_error()) {
|
|
|
|
LOG(ERROR) << result_;
|
|
|
|
}
|
|
|
|
ASSERT_TRUE(result_.is_ok());
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
bool is_inited_ = false;
|
2022-09-14 14:06:52 +02:00
|
|
|
td::ConcurrentScheduler sched_{4, 0};
|
2020-10-08 12:27:00 +02:00
|
|
|
td::Status result_;
|
2018-12-31 20:04:05 +01:00
|
|
|
};
|
2018-12-28 01:54:54 +01:00
|
|
|
//RegisterTest<Tdclient_login> Tdclient_login("Tdclient_login");
|
2018-05-30 16:00:17 +02:00
|
|
|
|
2019-03-21 10:59:20 +01:00
|
|
|
TEST(Client, Simple) {
|
|
|
|
td::Client client;
|
2020-08-17 13:40:22 +02:00
|
|
|
// client.execute({1, td::td_api::make_object<td::td_api::setLogTagVerbosityLevel>("actor", 1)});
|
2019-03-21 10:59:20 +01:00
|
|
|
client.send({3, td::make_tl_object<td::td_api::testSquareInt>(3)});
|
|
|
|
while (true) {
|
|
|
|
auto result = client.receive(10);
|
|
|
|
if (result.id == 3) {
|
|
|
|
auto test_int = td::td_api::move_object_as<td::td_api::testInt>(result.object);
|
|
|
|
ASSERT_EQ(test_int->value_, 9);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Client, SimpleMulti) {
|
2021-12-24 12:11:36 +01:00
|
|
|
std::vector<td::Client> clients(7);
|
2019-03-21 10:59:20 +01:00
|
|
|
//for (auto &client : clients) {
|
|
|
|
//client.execute({1, td::td_api::make_object<td::td_api::setLogTagVerbosityLevel>("td_requests", 1)});
|
|
|
|
//}
|
|
|
|
|
2020-08-06 21:07:08 +02:00
|
|
|
for (size_t i = 0; i < clients.size(); i++) {
|
|
|
|
clients[i].send({i + 2, td::make_tl_object<td::td_api::testSquareInt>(3)});
|
2020-10-08 12:27:00 +02:00
|
|
|
if (td::Random::fast_bool()) {
|
2020-08-06 21:07:08 +02:00
|
|
|
clients[i].send({1, td::make_tl_object<td::td_api::close>()});
|
|
|
|
}
|
2019-03-21 10:59:20 +01:00
|
|
|
}
|
|
|
|
|
2020-08-06 21:07:08 +02:00
|
|
|
for (size_t i = 0; i < clients.size(); i++) {
|
2019-03-21 10:59:20 +01:00
|
|
|
while (true) {
|
2020-08-06 21:07:08 +02:00
|
|
|
auto result = clients[i].receive(10);
|
|
|
|
if (result.id == i + 2) {
|
|
|
|
CHECK(result.object->get_id() == td::td_api::testInt::ID);
|
2019-03-21 10:59:20 +01:00
|
|
|
auto test_int = td::td_api::move_object_as<td::td_api::testInt>(result.object);
|
|
|
|
ASSERT_EQ(test_int->value_, 9);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-17 19:41:23 +01:00
|
|
|
#if !TD_THREAD_UNSUPPORTED
|
2019-05-20 17:37:05 +02:00
|
|
|
TEST(Client, Multi) {
|
2020-07-30 03:04:57 +02:00
|
|
|
td::vector<td::thread> threads;
|
2020-08-06 21:07:08 +02:00
|
|
|
std::atomic<int> ok_count{0};
|
2019-05-20 17:37:05 +02:00
|
|
|
for (int i = 0; i < 4; i++) {
|
2020-08-06 21:07:08 +02:00
|
|
|
threads.emplace_back([i, &ok_count] {
|
|
|
|
for (int j = 0; j < 1000; j++) {
|
2019-05-20 17:37:05 +02:00
|
|
|
td::Client client;
|
2020-08-06 21:07:08 +02:00
|
|
|
auto request_id = static_cast<td::uint64>(j + 2 + 1000 * i);
|
|
|
|
client.send({request_id, td::make_tl_object<td::td_api::testSquareInt>(3)});
|
|
|
|
if (j & 1) {
|
|
|
|
client.send({1, td::make_tl_object<td::td_api::close>()});
|
|
|
|
}
|
2019-05-20 17:37:05 +02:00
|
|
|
while (true) {
|
|
|
|
auto result = client.receive(10);
|
2020-08-06 21:07:08 +02:00
|
|
|
if (result.id == request_id) {
|
|
|
|
ok_count++;
|
|
|
|
if ((j & 1) == 0) {
|
|
|
|
client.send({1, td::make_tl_object<td::td_api::close>()});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (result.id == 0 && result.object != nullptr &&
|
|
|
|
result.object->get_id() == td::td_api::updateAuthorizationState::ID &&
|
|
|
|
static_cast<const td::td_api::updateAuthorizationState *>(result.object.get())
|
|
|
|
->authorization_state_->get_id() == td::td_api::authorizationStateClosed::ID) {
|
|
|
|
ok_count++;
|
2019-05-20 17:37:05 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
2020-08-06 21:07:08 +02:00
|
|
|
ASSERT_EQ(8 * 1000, ok_count.load());
|
2019-05-20 17:37:05 +02:00
|
|
|
}
|
2020-07-30 03:04:57 +02:00
|
|
|
|
2020-10-05 13:32:23 +02:00
|
|
|
TEST(Client, Manager) {
|
2020-07-30 03:04:57 +02:00
|
|
|
td::vector<td::thread> threads;
|
2020-10-05 13:32:23 +02:00
|
|
|
td::ClientManager client;
|
2021-11-21 19:41:11 +01:00
|
|
|
#if !TD_EVENTFD_UNSUPPORTED // Client must be used from a single thread if there is no EventFd
|
2020-07-29 15:49:35 +02:00
|
|
|
int threads_n = 4;
|
2021-10-25 07:03:18 +02:00
|
|
|
#else
|
|
|
|
int threads_n = 1;
|
|
|
|
#endif
|
2020-07-29 15:49:35 +02:00
|
|
|
int clients_n = 1000;
|
2020-10-05 22:05:16 +02:00
|
|
|
client.send(0, 3, td::make_tl_object<td::td_api::testSquareInt>(3));
|
|
|
|
client.send(-1, 3, td::make_tl_object<td::td_api::testSquareInt>(3));
|
2020-07-29 15:49:35 +02:00
|
|
|
for (int i = 0; i < threads_n; i++) {
|
|
|
|
threads.emplace_back([&] {
|
2020-11-12 12:45:18 +01:00
|
|
|
for (int i = 0; i <= clients_n; i++) {
|
2020-11-14 23:13:11 +01:00
|
|
|
auto id = client.create_client_id();
|
2020-11-12 12:45:18 +01:00
|
|
|
if (i != 0) {
|
|
|
|
client.send(id, 3, td::make_tl_object<td::td_api::testSquareInt>(3));
|
|
|
|
}
|
2020-07-29 15:49:35 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
for (auto &thread : threads) {
|
|
|
|
thread.join();
|
|
|
|
}
|
|
|
|
|
2020-10-08 12:27:00 +02:00
|
|
|
std::set<td::int32> ids;
|
2020-07-30 22:39:10 +02:00
|
|
|
while (ids.size() != static_cast<size_t>(threads_n) * clients_n) {
|
2020-07-29 15:49:35 +02:00
|
|
|
auto event = client.receive(10);
|
2020-10-05 22:05:16 +02:00
|
|
|
if (event.client_id == 0 || event.client_id == -1) {
|
2020-10-08 12:27:00 +02:00
|
|
|
ASSERT_EQ(td::td_api::error::ID, event.object->get_id());
|
2020-10-08 12:59:03 +02:00
|
|
|
ASSERT_EQ(400, static_cast<td::td_api::error &>(*event.object).code_);
|
2020-10-05 22:05:16 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (event.request_id == 3) {
|
2020-10-08 12:27:00 +02:00
|
|
|
ASSERT_EQ(td::td_api::testInt::ID, event.object->get_id());
|
2020-10-05 22:05:16 +02:00
|
|
|
ASSERT_TRUE(ids.insert(event.client_id).second);
|
2020-07-29 15:49:35 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-10-08 00:28:01 +02:00
|
|
|
|
2021-11-21 19:41:11 +01:00
|
|
|
#if !TD_EVENTFD_UNSUPPORTED // Client must be used from a single thread if there is no EventFd
|
2020-10-08 00:28:01 +02:00
|
|
|
TEST(Client, Close) {
|
|
|
|
std::atomic<bool> stop_send{false};
|
|
|
|
std::atomic<bool> can_stop_receive{false};
|
|
|
|
std::atomic<td::int64> send_count{1};
|
|
|
|
std::atomic<td::int64> receive_count{0};
|
|
|
|
td::Client client;
|
|
|
|
|
|
|
|
std::mutex request_ids_mutex;
|
|
|
|
std::set<td::uint64> request_ids;
|
|
|
|
request_ids.insert(1);
|
|
|
|
td::thread send_thread([&] {
|
|
|
|
td::uint64 request_id = 2;
|
|
|
|
while (!stop_send.load()) {
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> guard(request_ids_mutex);
|
|
|
|
request_ids.insert(request_id);
|
|
|
|
}
|
|
|
|
client.send({request_id++, td::make_tl_object<td::td_api::testSquareInt>(3)});
|
|
|
|
send_count++;
|
|
|
|
}
|
|
|
|
can_stop_receive = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
td::thread receive_thread([&] {
|
2020-10-08 12:59:03 +02:00
|
|
|
auto max_continue_send = td::Random::fast_bool() ? 0 : 1000;
|
2020-10-08 00:28:01 +02:00
|
|
|
while (true) {
|
2020-10-13 09:42:18 +02:00
|
|
|
auto response = client.receive(10.0);
|
2020-10-08 12:59:03 +02:00
|
|
|
if (response.object == nullptr) {
|
|
|
|
if (!stop_send) {
|
|
|
|
stop_send = true;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
2020-10-08 00:28:01 +02:00
|
|
|
}
|
|
|
|
if (response.id > 0) {
|
|
|
|
if (!stop_send && response.object->get_id() == td::td_api::error::ID &&
|
|
|
|
static_cast<td::td_api::error &>(*response.object).code_ == 500 &&
|
|
|
|
td::Random::fast(0, max_continue_send) == 0) {
|
|
|
|
stop_send = true;
|
|
|
|
}
|
|
|
|
receive_count++;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> guard(request_ids_mutex);
|
2020-10-08 22:04:40 +02:00
|
|
|
size_t erased_count = request_ids.erase(response.id);
|
|
|
|
CHECK(erased_count > 0);
|
2020-10-08 00:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_stop_receive && receive_count == send_count) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-08 11:56:08 +02:00
|
|
|
td::usleep_for((td::Random::fast_bool() ? 0 : 1000) * (td::Random::fast_bool() ? 1 : 50));
|
2020-10-08 00:28:01 +02:00
|
|
|
client.send({1, td::make_tl_object<td::td_api::close>()});
|
|
|
|
|
|
|
|
send_thread.join();
|
|
|
|
receive_thread.join();
|
|
|
|
ASSERT_EQ(send_count.load(), receive_count.load());
|
|
|
|
ASSERT_TRUE(request_ids.empty());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(Client, ManagerClose) {
|
|
|
|
std::atomic<bool> stop_send{false};
|
|
|
|
std::atomic<bool> can_stop_receive{false};
|
|
|
|
std::atomic<td::int64> send_count{1};
|
|
|
|
std::atomic<td::int64> receive_count{0};
|
|
|
|
td::ClientManager client_manager;
|
2020-11-14 23:13:11 +01:00
|
|
|
auto client_id = client_manager.create_client_id();
|
2020-10-08 00:28:01 +02:00
|
|
|
|
|
|
|
std::mutex request_ids_mutex;
|
|
|
|
std::set<td::uint64> request_ids;
|
|
|
|
request_ids.insert(1);
|
|
|
|
td::thread send_thread([&] {
|
|
|
|
td::uint64 request_id = 2;
|
|
|
|
while (!stop_send.load()) {
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> guard(request_ids_mutex);
|
|
|
|
request_ids.insert(request_id);
|
|
|
|
}
|
|
|
|
client_manager.send(client_id, request_id++, td::make_tl_object<td::td_api::testSquareInt>(3));
|
|
|
|
send_count++;
|
|
|
|
}
|
|
|
|
can_stop_receive = true;
|
|
|
|
});
|
|
|
|
|
|
|
|
td::thread receive_thread([&] {
|
2020-10-08 12:59:03 +02:00
|
|
|
auto max_continue_send = td::Random::fast_bool() ? 0 : 1000;
|
|
|
|
bool can_stop_send = false;
|
2020-10-08 00:28:01 +02:00
|
|
|
while (true) {
|
2020-10-13 09:42:18 +02:00
|
|
|
auto response = client_manager.receive(10.0);
|
2020-10-08 12:59:03 +02:00
|
|
|
if (response.object == nullptr) {
|
|
|
|
if (!stop_send) {
|
|
|
|
can_stop_send = true;
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_stop_send && max_continue_send-- <= 0) {
|
|
|
|
stop_send = true;
|
2020-10-08 00:28:01 +02:00
|
|
|
}
|
|
|
|
if (response.request_id > 0) {
|
|
|
|
receive_count++;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> guard(request_ids_mutex);
|
2020-10-08 22:04:40 +02:00
|
|
|
size_t erased_count = request_ids.erase(response.request_id);
|
|
|
|
CHECK(erased_count > 0);
|
2020-10-08 00:28:01 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (can_stop_receive && receive_count == send_count) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-10-08 11:56:08 +02:00
|
|
|
td::usleep_for((td::Random::fast_bool() ? 0 : 1000) * (td::Random::fast_bool() ? 1 : 50));
|
2020-10-08 00:28:01 +02:00
|
|
|
client_manager.send(client_id, 1, td::make_tl_object<td::td_api::close>());
|
|
|
|
|
|
|
|
send_thread.join();
|
|
|
|
receive_thread.join();
|
|
|
|
ASSERT_EQ(send_count.load(), receive_count.load());
|
|
|
|
ASSERT_TRUE(request_ids.empty());
|
|
|
|
}
|
2019-11-17 19:41:23 +01:00
|
|
|
#endif
|
2021-10-25 07:03:18 +02:00
|
|
|
#endif
|
2019-05-20 17:37:05 +02:00
|
|
|
|
2020-10-08 13:40:25 +02:00
|
|
|
TEST(Client, ManagerCloseOneThread) {
|
|
|
|
td::ClientManager client_manager;
|
|
|
|
|
|
|
|
td::uint64 request_id = 2;
|
|
|
|
std::map<td::uint64, td::int32> sent_requests;
|
|
|
|
td::uint64 sent_count = 0;
|
|
|
|
td::uint64 receive_count = 0;
|
|
|
|
|
|
|
|
auto send_request = [&](td::int32 client_id, td::int32 expected_error_code) {
|
|
|
|
sent_count++;
|
|
|
|
sent_requests.emplace(request_id, expected_error_code);
|
|
|
|
client_manager.send(client_id, request_id++, td::make_tl_object<td::td_api::testSquareInt>(3));
|
|
|
|
};
|
|
|
|
|
|
|
|
auto receive = [&] {
|
|
|
|
while (receive_count != sent_count) {
|
|
|
|
auto response = client_manager.receive(1.0);
|
|
|
|
if (response.object == nullptr) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (response.request_id > 0) {
|
|
|
|
receive_count++;
|
|
|
|
auto it = sent_requests.find(response.request_id);
|
|
|
|
CHECK(it != sent_requests.end());
|
|
|
|
auto expected_error_code = it->second;
|
|
|
|
sent_requests.erase(it);
|
|
|
|
|
|
|
|
if (expected_error_code == 0) {
|
|
|
|
if (response.request_id == 1) {
|
|
|
|
ASSERT_EQ(td::td_api::ok::ID, response.object->get_id());
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(td::td_api::testInt::ID, response.object->get_id());
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
ASSERT_EQ(td::td_api::error::ID, response.object->get_id());
|
|
|
|
ASSERT_EQ(expected_error_code, static_cast<td::td_api::error &>(*response.object).code_);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
for (int t = 0; t < 3; t++) {
|
|
|
|
for (td::int32 i = -5; i <= 0; i++) {
|
|
|
|
send_request(i, 400);
|
|
|
|
}
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
receive();
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-11-14 23:13:11 +01:00
|
|
|
auto client_id = client_manager.create_client_id();
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
for (td::int32 i = -5; i < 5; i++) {
|
|
|
|
send_request(i, i == client_id ? 0 : (i > 0 && i < client_id ? 500 : 400));
|
|
|
|
}
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
receive();
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
send_request(client_id, 0);
|
|
|
|
}
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
receive();
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
sent_count++;
|
|
|
|
sent_requests.emplace(1, 0);
|
|
|
|
client_manager.send(client_id, 1, td::make_tl_object<td::td_api::close>());
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
send_request(client_id, 500);
|
|
|
|
}
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
receive();
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
for (int i = 0; i < 10; i++) {
|
|
|
|
send_request(client_id, 500);
|
|
|
|
}
|
2020-10-08 13:40:25 +02:00
|
|
|
|
2020-10-09 14:39:30 +02:00
|
|
|
receive();
|
|
|
|
}
|
2020-10-08 13:40:25 +02:00
|
|
|
|
|
|
|
ASSERT_TRUE(sent_requests.empty());
|
|
|
|
}
|
|
|
|
|
2019-07-31 17:04:38 +02:00
|
|
|
TEST(PartsManager, hands) {
|
|
|
|
{
|
2020-10-08 12:27:00 +02:00
|
|
|
td::PartsManager pm;
|
2019-07-31 17:04:38 +02:00
|
|
|
pm.init(0, 100000, false, 10, {0, 1, 2}, false, true).ensure_error();
|
|
|
|
//pm.set_known_prefix(0, false).ensure();
|
|
|
|
}
|
|
|
|
{
|
2020-10-08 12:27:00 +02:00
|
|
|
td::PartsManager pm;
|
2019-07-31 17:04:38 +02:00
|
|
|
pm.init(1, 100000, true, 10, {0, 1, 2}, false, true).ensure_error();
|
|
|
|
}
|
|
|
|
}
|