Move tdclient test out of namespace td.

GitOrigin-RevId: 17183698257c0d93553d8f7e61eff1f7f79818fc
This commit is contained in:
levlam 2020-10-08 13:27:00 +03:00
parent 582206d641
commit b4f358de36
3 changed files with 282 additions and 281 deletions

View File

@ -6,8 +6,6 @@
// //
#include "data.h" #include "data.h"
namespace td {
static const char thumbnail_arr[] = static const char thumbnail_arr[] =
"_9j_4AAQSkZJRgABAQEASABIAAD_2wBDAAICAgICAQICAgIDAgIDAwYEAwMDAwcFBQQGCAcJCAgHCAgJCg0LCQoMCggICw8LDA0ODg8OCQsQERAOEQ" "_9j_4AAQSkZJRgABAQEASABIAAD_2wBDAAICAgICAQICAgIDAgIDAwYEAwMDAwcFBQQGCAcJCAgHCAgJCg0LCQoMCggICw8LDA0ODg8OCQsQERAOEQ"
"0ODg7_2wBDAQIDAwMDAwcEBAcOCQgJDg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg7_wAARCAAyADIDASIA" "0ODg7_2wBDAQIDAwMDAwcEBAcOCQgJDg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg4ODg7_wAARCAAyADIDASIA"
@ -157,5 +155,3 @@ static const char sqlite_sample_db_arr[] =
const char *sqlite_sample_db = sqlite_sample_db_arr; const char *sqlite_sample_db = sqlite_sample_db_arr;
const size_t sqlite_sample_db_size = sizeof(sqlite_sample_db_arr) - 1; const size_t sqlite_sample_db_size = sizeof(sqlite_sample_db_arr) - 1;
} // namespace td

View File

@ -8,8 +8,6 @@
#include "td/utils/common.h" #include "td/utils/common.h"
namespace td {
extern const char *thumbnail; extern const char *thumbnail;
extern const size_t thumbnail_size; extern const size_t thumbnail_size;
@ -18,5 +16,3 @@ extern const size_t gzip_bomb_size;
extern const char *sqlite_sample_db; extern const char *sqlite_sample_db;
extern const size_t sqlite_sample_db_size; extern const size_t sqlite_sample_db_size;
} // namespace td

View File

@ -40,21 +40,19 @@
REGISTER_TESTS(tdclient); REGISTER_TESTS(tdclient);
namespace td {
template <class T> template <class T>
static void check_td_error(T &result) { static void check_td_error(T &result) {
LOG_CHECK(result->get_id() != td_api::error::ID) << to_string(result); LOG_CHECK(result->get_id() != td::td_api::error::ID) << to_string(result);
} }
class TestClient : public Actor { class TestClient : public td::Actor {
public: public:
explicit TestClient(string name) : name_(std::move(name)) { explicit TestClient(td::string name) : name_(std::move(name)) {
} }
struct Update { struct Update {
uint64 id; td::uint64 id;
tl_object_ptr<td_api::Object> object; td::tl_object_ptr<td::td_api::Object> object;
Update(uint64 id, tl_object_ptr<td_api::Object> object) : id(id), object(std::move(object)) { Update(td::uint64 id, td::tl_object_ptr<td::td_api::Object> object) : id(id), object(std::move(object)) {
} }
}; };
class Listener { class Listener {
@ -71,20 +69,20 @@ class TestClient : public Actor {
} }
virtual void on_update(std::shared_ptr<Update> update) = 0; virtual void on_update(std::shared_ptr<Update> update) = 0;
}; };
void close(Promise<> close_promise) { void close(td::Promise<> close_promise) {
close_promise_ = std::move(close_promise); close_promise_ = std::move(close_promise);
td_client_.reset(); td_client_.reset();
} }
unique_ptr<TdCallback> make_td_callback() { td::unique_ptr<td::TdCallback> make_td_callback() {
class TdCallbackImpl : public TdCallback { class TdCallbackImpl : public td::TdCallback {
public: public:
explicit TdCallbackImpl(ActorId<TestClient> client) : client_(client) { explicit TdCallbackImpl(td::ActorId<TestClient> client) : client_(client) {
} }
void on_result(uint64 id, tl_object_ptr<td_api::Object> result) override { void on_result(td::uint64 id, td::tl_object_ptr<td::td_api::Object> result) override {
send_closure(client_, &TestClient::on_result, id, std::move(result)); send_closure(client_, &TestClient::on_result, id, std::move(result));
} }
void on_error(uint64 id, tl_object_ptr<td_api::error> error) override { void on_error(td::uint64 id, td::tl_object_ptr<td::td_api::error> error) override {
send_closure(client_, &TestClient::on_error, id, std::move(error)); send_closure(client_, &TestClient::on_error, id, std::move(error));
} }
TdCallbackImpl(const TdCallbackImpl &) = delete; TdCallbackImpl(const TdCallbackImpl &) = delete;
@ -96,12 +94,12 @@ class TestClient : public Actor {
} }
private: private:
ActorId<TestClient> client_; td::ActorId<TestClient> client_;
}; };
return make_unique<TdCallbackImpl>(actor_id(this)); return td::make_unique<TdCallbackImpl>(actor_id(this));
} }
void add_listener(unique_ptr<Listener> listener) { void add_listener(td::unique_ptr<Listener> listener) {
auto *ptr = listener.get(); auto *ptr = listener.get();
listeners_.push_back(std::move(listener)); listeners_.push_back(std::move(listener));
ptr->start_listen(this); ptr->start_listen(this);
@ -125,10 +123,10 @@ class TestClient : public Actor {
} }
} }
void on_result(uint64 id, tl_object_ptr<td_api::Object> result) { void on_result(td::uint64 id, td::tl_object_ptr<td::td_api::Object> result) {
on_update(std::make_shared<Update>(id, std::move(result))); on_update(std::make_shared<Update>(id, std::move(result)));
} }
void on_error(uint64 id, tl_object_ptr<td_api::error> error) { void on_error(td::uint64 id, td::tl_object_ptr<td::td_api::error> error) {
on_update(std::make_shared<Update>(id, std::move(error))); on_update(std::make_shared<Update>(id, std::move(error)));
} }
void on_update(std::shared_ptr<Update> update) { void on_update(std::shared_ptr<Update> update) {
@ -143,23 +141,23 @@ class TestClient : public Actor {
} }
void start_up() override { void start_up() override {
rmrf(name_).ignore(); td::rmrf(name_).ignore();
auto old_context = set_context(std::make_shared<td::ActorContext>()); auto old_context = set_context(std::make_shared<td::ActorContext>());
set_tag(name_); set_tag(name_);
LOG(INFO) << "START UP!"; LOG(INFO) << "START UP!";
td_client_ = create_actor<ClientActor>("Td-proxy", make_td_callback()); td_client_ = td::create_actor<td::ClientActor>("Td-proxy", make_td_callback());
} }
ActorOwn<ClientActor> td_client_; td::ActorOwn<td::ClientActor> td_client_;
string name_; td::string name_;
private: private:
std::vector<unique_ptr<Listener>> listeners_; td::vector<td::unique_ptr<Listener>> listeners_;
std::vector<Listener *> pending_remove_; td::vector<Listener *> pending_remove_;
Promise<> close_promise_; td::Promise<> close_promise_;
}; };
class Task : public TestClient::Listener { class Task : public TestClient::Listener {
@ -180,16 +178,16 @@ class Task : public TestClient::Listener {
} }
template <class F> template <class F>
void send_query(tl_object_ptr<td_api::Function> function, F callback) { void send_query(td::tl_object_ptr<td::td_api::Function> function, F callback) {
auto id = current_query_id_++; auto id = current_query_id_++;
sent_queries_[id] = std::forward<F>(callback); sent_queries_[id] = std::forward<F>(callback);
send_closure(client_->td_client_, &ClientActor::request, id, std::move(function)); send_closure(client_->td_client_, &td::ClientActor::request, id, std::move(function));
} }
protected: protected:
std::map<uint64, std::function<void(tl_object_ptr<td_api::Object>)>> sent_queries_; std::map<td::uint64, std::function<void(td::tl_object_ptr<td::td_api::Object>)>> sent_queries_;
TestClient *client_ = nullptr; TestClient *client_ = nullptr;
uint64 current_query_id_ = 1; td::uint64 current_query_id_ = 1;
virtual void start_up() { virtual void start_up() {
} }
@ -200,31 +198,31 @@ class Task : public TestClient::Listener {
class DoAuthentication : public Task { class DoAuthentication : public Task {
public: public:
DoAuthentication(string name, string phone, string code, Promise<> promise) DoAuthentication(td::string name, td::string phone, td::string code, td::Promise<> promise)
: name_(std::move(name)), phone_(std::move(phone)), code_(std::move(code)), promise_(std::move(promise)) { : name_(std::move(name)), phone_(std::move(phone)), code_(std::move(code)), promise_(std::move(promise)) {
} }
void start_up() override { void start_up() override {
send_query(make_tl_object<td_api::getAuthorizationState>(), send_query(td::make_tl_object<td::td_api::getAuthorizationState>(),
[this](auto res) { this->process_authorization_state(std::move(res)); }); [this](auto res) { this->process_authorization_state(std::move(res)); });
} }
void process_authorization_state(tl_object_ptr<td_api::Object> authorization_state) { void process_authorization_state(td::tl_object_ptr<td::td_api::Object> authorization_state) {
start_flag_ = true; start_flag_ = true;
tl_object_ptr<td_api::Function> function; td::tl_object_ptr<td::td_api::Function> function;
switch (authorization_state->get_id()) { switch (authorization_state->get_id()) {
case td_api::authorizationStateWaitEncryptionKey::ID: case td::td_api::authorizationStateWaitEncryptionKey::ID:
function = make_tl_object<td_api::checkDatabaseEncryptionKey>(); function = td::make_tl_object<td::td_api::checkDatabaseEncryptionKey>();
break; break;
case td_api::authorizationStateWaitPhoneNumber::ID: case td::td_api::authorizationStateWaitPhoneNumber::ID:
function = make_tl_object<td_api::setAuthenticationPhoneNumber>(phone_, nullptr); function = td::make_tl_object<td::td_api::setAuthenticationPhoneNumber>(phone_, nullptr);
break; break;
case td_api::authorizationStateWaitCode::ID: case td::td_api::authorizationStateWaitCode::ID:
function = make_tl_object<td_api::checkAuthenticationCode>(code_); function = td::make_tl_object<td::td_api::checkAuthenticationCode>(code_);
break; break;
case td_api::authorizationStateWaitRegistration::ID: case td::td_api::authorizationStateWaitRegistration::ID:
function = make_tl_object<td_api::registerUser>(name_, ""); function = td::make_tl_object<td::td_api::registerUser>(name_, "");
break; break;
case td_api::authorizationStateWaitTdlibParameters::ID: { case td::td_api::authorizationStateWaitTdlibParameters::ID: {
auto parameters = td_api::make_object<td_api::tdlibParameters>(); auto parameters = td::td_api::make_object<td::td_api::tdlibParameters>();
parameters->use_test_dc_ = true; parameters->use_test_dc_ = true;
parameters->database_directory_ = name_ + TD_DIR_SLASH; parameters->database_directory_ = name_ + TD_DIR_SLASH;
parameters->use_message_database_ = true; parameters->use_message_database_ = true;
@ -236,17 +234,17 @@ class DoAuthentication : public Task {
parameters->application_version_ = "tdclient-test"; parameters->application_version_ = "tdclient-test";
parameters->ignore_file_names_ = false; parameters->ignore_file_names_ = false;
parameters->enable_storage_optimizer_ = true; parameters->enable_storage_optimizer_ = true;
function = td_api::make_object<td_api::setTdlibParameters>(std::move(parameters)); function = td::td_api::make_object<td::td_api::setTdlibParameters>(std::move(parameters));
break; break;
} }
case td_api::authorizationStateReady::ID: case td::td_api::authorizationStateReady::ID:
on_authorization_ready(); on_authorization_ready();
return; return;
default: default:
LOG(ERROR) << "Unexpected authorization state " << to_string(authorization_state); LOG(ERROR) << "Unexpected authorization state " << to_string(authorization_state);
UNREACHABLE(); UNREACHABLE();
} }
send_query(std::move(function), [](auto res) { LOG_CHECK(res->get_id() == td_api::ok::ID) << to_string(res); }); send_query(std::move(function), [](auto res) { LOG_CHECK(res->get_id() == td::td_api::ok::ID) << to_string(res); });
} }
void on_authorization_ready() { void on_authorization_ready() {
LOG(INFO) << "GOT AUTHORIZED"; LOG(INFO) << "GOT AUTHORIZED";
@ -254,11 +252,12 @@ class DoAuthentication : public Task {
} }
private: private:
string name_; td::string name_;
string phone_; td::string phone_;
string code_; td::string code_;
Promise<> promise_; td::Promise<> promise_;
bool start_flag_{false}; bool start_flag_{false};
void process_update(std::shared_ptr<TestClient::Update> update) override { void process_update(std::shared_ptr<TestClient::Update> update) override {
if (!start_flag_) { if (!start_flag_) {
return; return;
@ -266,53 +265,55 @@ class DoAuthentication : public Task {
if (!update->object) { if (!update->object) {
return; return;
} }
if (update->object->get_id() == td_api::updateAuthorizationState::ID) { if (update->object->get_id() == td::td_api::updateAuthorizationState::ID) {
auto o = std::move(update->object); auto update_authorization_state = td::move_tl_object_as<td::td_api::updateAuthorizationState>(update->object);
process_authorization_state(std::move(static_cast<td_api::updateAuthorizationState &>(*o).authorization_state_)); process_authorization_state(std::move(update_authorization_state->authorization_state_));
} }
} }
}; };
class SetUsername : public Task { class SetUsername : public Task {
public: public:
SetUsername(string username, Promise<> promise) : username_(std::move(username)), promise_(std::move(promise)) { SetUsername(td::string username, td::Promise<> promise)
: username_(std::move(username)), promise_(std::move(promise)) {
} }
private: private:
string username_; td::string username_;
Promise<> promise_; td::Promise<> promise_;
int32 self_id_ = 0; td::int32 self_id_ = 0;
string tag_; td::string tag_;
void start_up() override { void start_up() override {
send_query(make_tl_object<td_api::getMe>(), [this](auto res) { this->process_me_user(std::move(res)); }); send_query(td::make_tl_object<td::td_api::getMe>(), [this](auto res) { this->process_me_user(std::move(res)); });
} }
void process_me_user(tl_object_ptr<td_api::Object> res) { void process_me_user(td::tl_object_ptr<td::td_api::Object> res) {
CHECK(res->get_id() == td_api::user::ID); CHECK(res->get_id() == td::td_api::user::ID);
auto user = move_tl_object_as<td_api::user>(res); auto user = td::move_tl_object_as<td::td_api::user>(res);
self_id_ = user->id_; self_id_ = user->id_;
if (user->username_ != username_) { if (user->username_ != username_) {
LOG(INFO) << "SET USERNAME: " << username_; LOG(INFO) << "SET USERNAME: " << username_;
send_query(make_tl_object<td_api::setUsername>(username_), [this](auto res) { send_query(td::make_tl_object<td::td_api::setUsername>(username_), [this](auto res) {
CHECK(res->get_id() == td_api::ok::ID); CHECK(res->get_id() == td::td_api::ok::ID);
this->send_self_message(); this->send_self_message();
}); });
} else { } else {
send_self_message(); send_self_message();
} }
} }
void send_self_message() {
tag_ = PSTRING() << format::as_hex(Random::secure_int64());
send_query(make_tl_object<td_api::createPrivateChat>(self_id_, false), [this](auto res) { void send_self_message() {
CHECK(res->get_id() == td_api::chat::ID); tag_ = PSTRING() << td::format::as_hex(td::Random::secure_int64());
auto chat = move_tl_object_as<td_api::chat>(res);
this->send_query( send_query(td::make_tl_object<td::td_api::createPrivateChat>(self_id_, false), [this](auto res) {
make_tl_object<td_api::sendMessage>( 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>(
chat->id_, 0, 0, nullptr, nullptr, chat->id_, 0, 0, nullptr, nullptr,
make_tl_object<td_api::inputMessageText>( td::make_tl_object<td::td_api::inputMessageText>(
make_tl_object<td_api::formattedText>(PSTRING() << tag_ << " INIT", Auto()), false, false)), td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " INIT", td::Auto()),
false, false)),
[](auto res) {}); [](auto res) {});
}); });
} }
@ -321,11 +322,11 @@ class SetUsername : public Task {
if (!update->object) { if (!update->object) {
return; return;
} }
if (update->object->get_id() == td_api::updateMessageSendSucceeded::ID) { if (update->object->get_id() == td::td_api::updateMessageSendSucceeded::ID) {
auto updateNewMessage = move_tl_object_as<td_api::updateMessageSendSucceeded>(update->object); auto updateNewMessage = td::move_tl_object_as<td::td_api::updateMessageSendSucceeded>(update->object);
auto &message = updateNewMessage->message_; auto &message = updateNewMessage->message_;
if (message->content_->get_id() == td_api::messageText::ID) { if (message->content_->get_id() == td::td_api::messageText::ID) {
auto messageText = move_tl_object_as<td_api::messageText>(message->content_); auto messageText = td::move_tl_object_as<td::td_api::messageText>(message->content_);
auto text = messageText->text_->text_; auto text = messageText->text_->text_;
if (text.substr(0, tag_.size()) == tag_) { if (text.substr(0, tag_.size()) == tag_) {
LOG(INFO) << "GOT SELF MESSAGE"; LOG(INFO) << "GOT SELF MESSAGE";
@ -338,26 +339,27 @@ class SetUsername : public Task {
class CheckTestA : public Task { class CheckTestA : public Task {
public: public:
CheckTestA(string tag, Promise<> promise) : tag_(std::move(tag)), promise_(std::move(promise)) { CheckTestA(td::string tag, td::Promise<> promise) : tag_(std::move(tag)), promise_(std::move(promise)) {
} }
private: private:
string tag_; td::string tag_;
Promise<> promise_; td::Promise<> promise_;
string previous_text_; td::string previous_text_;
int cnt_ = 20; int cnt_ = 20;
void process_update(std::shared_ptr<TestClient::Update> update) override { void process_update(std::shared_ptr<TestClient::Update> update) override {
if (update->object->get_id() == td_api::updateNewMessage::ID) { if (update->object->get_id() == td::td_api::updateNewMessage::ID) {
auto updateNewMessage = move_tl_object_as<td_api::updateNewMessage>(update->object); auto updateNewMessage = td::move_tl_object_as<td::td_api::updateNewMessage>(update->object);
auto &message = updateNewMessage->message_; auto &message = updateNewMessage->message_;
if (message->content_->get_id() == td_api::messageText::ID) { if (message->content_->get_id() == td::td_api::messageText::ID) {
auto messageText = move_tl_object_as<td_api::messageText>(message->content_); auto messageText = td::move_tl_object_as<td::td_api::messageText>(message->content_);
auto text = messageText->text_->text_; auto text = messageText->text_->text_;
if (text.substr(0, tag_.size()) == tag_) { if (text.substr(0, tag_.size()) == tag_) {
LOG_CHECK(text > previous_text_) << tag("now", text) << tag("previous", previous_text_); LOG_CHECK(text > previous_text_) << td::tag("now", text) << td::tag("previous", previous_text_);
previous_text_ = text; previous_text_ = text;
cnt_--; cnt_--;
LOG(INFO) << "GOT " << tag("text", text) << tag("left", cnt_); LOG(INFO) << "GOT " << td::tag("text", text) << td::tag("left", cnt_);
if (cnt_ == 0) { if (cnt_ == 0) {
return stop(); return stop();
} }
@ -369,17 +371,19 @@ class CheckTestA : public Task {
class TestA : public Task { class TestA : public Task {
public: public:
TestA(string tag, string username) : tag_(std::move(tag)), username_(std::move(username)) { TestA(td::string tag, td::string username) : tag_(std::move(tag)), username_(std::move(username)) {
} }
void start_up() override { void start_up() override {
send_query(make_tl_object<td_api::searchPublicChat>(username_), [this](auto res) { send_query(td::make_tl_object<td::td_api::searchPublicChat>(username_), [this](auto res) {
CHECK(res->get_id() == td_api::chat::ID); CHECK(res->get_id() == td::td_api::chat::ID);
auto chat = move_tl_object_as<td_api::chat>(res); auto chat = td::move_tl_object_as<td::td_api::chat>(res);
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
this->send_query(make_tl_object<td_api::sendMessage>( this->send_query(
td::make_tl_object<td::td_api::sendMessage>(
chat->id_, 0, 0, nullptr, nullptr, chat->id_, 0, 0, nullptr, nullptr,
make_tl_object<td_api::inputMessageText>( td::make_tl_object<td::td_api::inputMessageText>(
make_tl_object<td_api::formattedText>(PSTRING() << tag_ << " " << (1000 + i), Auto()), td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " " << (1000 + i), td::Auto()),
false, false)), false, false)),
[&](auto res) { this->stop(); }); [&](auto res) { this->stop(); });
} }
@ -387,28 +391,28 @@ class TestA : public Task {
} }
private: private:
string tag_; td::string tag_;
string username_; td::string username_;
}; };
class TestSecretChat : public Task { class TestSecretChat : public Task {
public: public:
TestSecretChat(string tag, string username) : tag_(std::move(tag)), username_(std::move(username)) { TestSecretChat(td::string tag, td::string username) : tag_(std::move(tag)), username_(std::move(username)) {
} }
void start_up() override { void start_up() override {
auto f = [this](auto res) { auto f = [this](auto res) {
CHECK(res->get_id() == td_api::chat::ID); CHECK(res->get_id() == td::td_api::chat::ID);
auto chat = move_tl_object_as<td_api::chat>(res); auto chat = td::move_tl_object_as<td::td_api::chat>(res);
this->chat_id_ = chat->id_; this->chat_id_ = chat->id_;
this->secret_chat_id_ = move_tl_object_as<td_api::chatTypeSecret>(chat->type_)->secret_chat_id_; this->secret_chat_id_ = td::move_tl_object_as<td::td_api::chatTypeSecret>(chat->type_)->secret_chat_id_;
}; };
send_query(make_tl_object<td_api::searchPublicChat>(username_), [this, f = std::move(f)](auto res) mutable { send_query(td::make_tl_object<td::td_api::searchPublicChat>(username_), [this, f = std::move(f)](auto res) mutable {
CHECK(res->get_id() == td_api::chat::ID); CHECK(res->get_id() == td::td_api::chat::ID);
auto chat = move_tl_object_as<td_api::chat>(res); auto chat = td::move_tl_object_as<td::td_api::chat>(res);
CHECK(chat->type_->get_id() == td_api::chatTypePrivate::ID); CHECK(chat->type_->get_id() == td::td_api::chatTypePrivate::ID);
auto info = move_tl_object_as<td_api::chatTypePrivate>(chat->type_); auto info = td::move_tl_object_as<td::td_api::chatTypePrivate>(chat->type_);
this->send_query(make_tl_object<td_api::createNewSecretChat>(info->user_id_), std::move(f)); this->send_query(td::make_tl_object<td::td_api::createNewSecretChat>(info->user_id_), std::move(f));
}); });
} }
@ -416,34 +420,35 @@ class TestSecretChat : public Task {
if (!update->object) { if (!update->object) {
return; return;
} }
if (update->object->get_id() == td_api::updateSecretChat::ID) { if (update->object->get_id() == td::td_api::updateSecretChat::ID) {
auto update_secret_chat = move_tl_object_as<td_api::updateSecretChat>(update->object); auto update_secret_chat = td::move_tl_object_as<td::td_api::updateSecretChat>(update->object);
if (update_secret_chat->secret_chat_->id_ != secret_chat_id_ || if (update_secret_chat->secret_chat_->id_ != secret_chat_id_ ||
update_secret_chat->secret_chat_->state_->get_id() != td_api::secretChatStateReady::ID) { update_secret_chat->secret_chat_->state_->get_id() != td::td_api::secretChatStateReady::ID) {
return; return;
} }
LOG(INFO) << "SEND ENCRYPTED MESSAGES"; LOG(INFO) << "SEND ENCRYPTED MESSAGES";
for (int i = 0; i < 20; i++) { for (int i = 0; i < 20; i++) {
send_query(make_tl_object<td_api::sendMessage>( send_query(
td::make_tl_object<td::td_api::sendMessage>(
chat_id_, 0, 0, nullptr, nullptr, chat_id_, 0, 0, nullptr, nullptr,
make_tl_object<td_api::inputMessageText>( td::make_tl_object<td::td_api::inputMessageText>(
make_tl_object<td_api::formattedText>(PSTRING() << tag_ << " " << (1000 + i), Auto()), false, td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " " << (1000 + i), td::Auto()),
false)), false, false)),
[](auto res) {}); [](auto res) {});
} }
} }
} }
private: private:
string tag_; td::string tag_;
string username_; td::string username_;
int64 secret_chat_id_ = 0; td::int64 secret_chat_id_ = 0;
int64 chat_id_ = 0; td::int64 chat_id_ = 0;
}; };
class TestFileGenerated : public Task { class TestFileGenerated : public Task {
public: public:
TestFileGenerated(string tag, string username) : tag_(std::move(tag)), username_(std::move(username)) { TestFileGenerated(td::string tag, td::string username) : tag_(std::move(tag)), username_(std::move(username)) {
} }
void start_up() override { void start_up() override {
@ -453,12 +458,12 @@ class TestFileGenerated : public Task {
if (!update->object) { if (!update->object) {
return; return;
} }
if (update->object->get_id() == td_api::updateNewMessage::ID) { if (update->object->get_id() == td::td_api::updateNewMessage::ID) {
auto updateNewMessage = move_tl_object_as<td_api::updateNewMessage>(update->object); auto updateNewMessage = td::move_tl_object_as<td::td_api::updateNewMessage>(update->object);
auto &message = updateNewMessage->message_; auto &message = updateNewMessage->message_;
chat_id_ = message->chat_id_; chat_id_ = message->chat_id_;
if (message->content_->get_id() == td_api::messageText::ID) { if (message->content_->get_id() == td::td_api::messageText::ID) {
auto messageText = move_tl_object_as<td_api::messageText>(message->content_); auto messageText = td::move_tl_object_as<td::td_api::messageText>(message->content_);
auto text = messageText->text_->text_; auto text = messageText->text_->text_;
if (text.substr(0, tag_.size()) == tag_) { if (text.substr(0, tag_.size()) == tag_) {
if (text.substr(tag_.size() + 1) == "ONE_FILE") { if (text.substr(tag_.size() + 1) == "ONE_FILE") {
@ -466,47 +471,49 @@ class TestFileGenerated : public Task {
} }
} }
} }
} else if (update->object->get_id() == td_api::updateFileGenerationStart::ID) { } else if (update->object->get_id() == td::td_api::updateFileGenerationStart::ID) {
auto info = move_tl_object_as<td_api::updateFileGenerationStart>(update->object); auto info = td::move_tl_object_as<td::td_api::updateFileGenerationStart>(update->object);
generate_file(info->generation_id_, info->original_path_, info->destination_path_, info->conversion_); generate_file(info->generation_id_, info->original_path_, info->destination_path_, info->conversion_);
} else if (update->object->get_id() == td_api::updateFile::ID) { } else if (update->object->get_id() == td::td_api::updateFile::ID) {
auto file = move_tl_object_as<td_api::updateFile>(update->object); auto file = td::move_tl_object_as<td::td_api::updateFile>(update->object);
LOG(INFO) << to_string(file); LOG(INFO) << to_string(file);
} }
} }
void one_file() { void one_file() {
LOG(ERROR) << "Start ONE_FILE test"; LOG(ERROR) << "Start ONE_FILE test";
string file_path = string("test_documents") + TD_DIR_SLASH + "a.txt"; auto file_path = PSTRING() << "test_documents" << TD_DIR_SLASH << "a.txt";
mkpath(file_path).ensure(); td::mkpath(file_path).ensure();
auto raw_file = auto raw_file =
FileFd::open(file_path, FileFd::Flags::Create | FileFd::Flags::Truncate | FileFd::Flags::Write).move_as_ok(); td::FileFd::open(file_path, td::FileFd::Flags::Create | td::FileFd::Flags::Truncate | td::FileFd::Flags::Write)
auto file = BufferedFd<FileFd>(std::move(raw_file)); .move_as_ok();
auto file = td::BufferedFd<td::FileFd>(std::move(raw_file));
for (int i = 1; i < 100000; i++) { for (int i = 1; i < 100000; i++) {
file.write(PSLICE() << i << "\n").ensure(); file.write(PSLICE() << i << "\n").ensure();
} }
file.flush_write().ensure(); // important file.flush_write().ensure(); // important
file.close(); file.close();
this->send_query(make_tl_object<td_api::sendMessage>( send_query(td::make_tl_object<td::td_api::sendMessage>(
chat_id_, 0, 0, nullptr, nullptr, chat_id_, 0, 0, nullptr, nullptr,
make_tl_object<td_api::inputMessageDocument>( td::make_tl_object<td::td_api::inputMessageDocument>(
make_tl_object<td_api::inputFileGenerated>(file_path, "square", 0), td::make_tl_object<td::td_api::inputFileGenerated>(file_path, "square", 0),
make_tl_object<td_api::inputThumbnail>( td::make_tl_object<td::td_api::inputThumbnail>(
make_tl_object<td_api::inputFileGenerated>(file_path, "thumbnail", 0), 0, 0), td::make_tl_object<td::td_api::inputFileGenerated>(file_path, "thumbnail", 0), 0, 0),
true, make_tl_object<td_api::formattedText>(tag_, Auto()))), true, td::make_tl_object<td::td_api::formattedText>(tag_, td::Auto()))),
[](auto res) { check_td_error(res); }); [](auto res) { check_td_error(res); });
this->send_query( send_query(td::make_tl_object<td::td_api::sendMessage>(
make_tl_object<td_api::sendMessage>(chat_id_, 0, 0, nullptr, nullptr, chat_id_, 0, 0, nullptr, nullptr,
make_tl_object<td_api::inputMessageDocument>( td::make_tl_object<td::td_api::inputMessageDocument>(
make_tl_object<td_api::inputFileGenerated>(file_path, "square", 0), td::make_tl_object<td::td_api::inputFileGenerated>(file_path, "square", 0), nullptr, true,
nullptr, true, make_tl_object<td_api::formattedText>(tag_, Auto()))), td::make_tl_object<td::td_api::formattedText>(tag_, td::Auto()))),
[](auto res) { check_td_error(res); }); [](auto res) { check_td_error(res); });
} }
friend class GenerateFile; class GenerateFile : public td::Actor {
class GenerateFile : public Actor {
public: public:
GenerateFile(Task *parent, int64 id, string original_path, string destination_path, string conversion) GenerateFile(Task *parent, td::int64 id, td::string original_path, td::string destination_path,
td::string conversion)
: parent_(parent) : parent_(parent)
, id_(id) , id_(id)
, original_path_(std::move(original_path)) , original_path_(std::move(original_path))
@ -516,10 +523,10 @@ class TestFileGenerated : public Task {
private: private:
Task *parent_; Task *parent_;
int64 id_; td::int64 id_;
string original_path_; td::string original_path_;
string destination_path_; td::string destination_path_;
string conversion_; td::string conversion_;
FILE *from = nullptr; FILE *from = nullptr;
FILE *to = nullptr; FILE *to = nullptr;
@ -531,10 +538,11 @@ class TestFileGenerated : public Task {
CHECK(to); CHECK(to);
yield(); yield();
} }
void loop() override { void loop() override {
int cnt = 0; int cnt = 0;
while (true) { while (true) {
uint32 x; td::uint32 x;
auto r = std::fscanf(from, "%u", &x); auto r = std::fscanf(from, "%u", &x);
if (r != 1) { if (r != 1) {
return stop(); return stop();
@ -546,65 +554,66 @@ class TestFileGenerated : public Task {
} }
auto ready = std::ftell(to); auto ready = std::ftell(to);
LOG(ERROR) << "READY: " << ready; LOG(ERROR) << "READY: " << ready;
parent_->send_query(make_tl_object<td_api::setFileGenerationProgress>( parent_->send_query(td::make_tl_object<td::td_api::setFileGenerationProgress>(
id_, 1039823 /*yeah, exact size of this file*/, narrow_cast<int32>(ready)), id_, 1039823 /*yeah, exact size of this file*/, td::narrow_cast<td::int32>(ready)),
[](auto result) { check_td_error(result); }); [](auto result) { check_td_error(result); });
set_timeout_in(0.02); set_timeout_in(0.02);
} }
void tear_down() override { void tear_down() override {
std::fclose(from); std::fclose(from);
std::fclose(to); std::fclose(to);
parent_->send_query(make_tl_object<td_api::finishFileGeneration>(id_, nullptr), parent_->send_query(td::make_tl_object<td::td_api::finishFileGeneration>(id_, nullptr),
[](auto result) { check_td_error(result); }); [](auto result) { check_td_error(result); });
} }
}; };
void generate_file(int64 id, string original_path, string destination_path, string conversion) {
LOG(ERROR) << "Generate file " << tag("id", id) << tag("original_path", original_path) void generate_file(td::int64 id, td::string original_path, td::string destination_path, td::string conversion) {
<< tag("destination_path", destination_path) << tag("conversion", conversion); LOG(ERROR) << "Generate file " << td::tag("id", id) << td::tag("original_path", original_path)
<< td::tag("destination_path", destination_path) << td::tag("conversion", conversion);
if (conversion == "square") { if (conversion == "square") {
create_actor<GenerateFile>("GenerateFile", this, id, original_path, destination_path, conversion).release(); td::create_actor<GenerateFile>("GenerateFile", this, id, original_path, destination_path, conversion).release();
} else if (conversion == "thumbnail") { } else if (conversion == "thumbnail") {
write_file(destination_path, base64url_decode(Slice(thumbnail, thumbnail_size)).ok()).ensure(); td::write_file(destination_path, td::base64url_decode(td::Slice(thumbnail, thumbnail_size)).ok()).ensure();
this->send_query(make_tl_object<td_api::finishFileGeneration>(id, nullptr), send_query(td::make_tl_object<td::td_api::finishFileGeneration>(id, nullptr),
[](auto result) { check_td_error(result); }); [](auto result) { check_td_error(result); });
} else { } else {
LOG(FATAL) << "Unknown " << tag("conversion", conversion); LOG(FATAL) << "Unknown " << td::tag("conversion", conversion);
} }
} }
private: private:
string tag_; td::string tag_;
string username_; td::string username_;
int64 chat_id_ = 0; td::int64 chat_id_ = 0;
}; };
class CheckTestC : public Task { class CheckTestC : public Task {
public: public:
CheckTestC(string username, string tag, Promise<> promise) CheckTestC(td::string username, td::string tag, td::Promise<> promise)
: username_(std::move(username)), tag_(std::move(tag)), promise_(std::move(promise)) { : username_(std::move(username)), tag_(std::move(tag)), promise_(std::move(promise)) {
} }
void start_up() override { void start_up() override {
send_query(make_tl_object<td_api::searchPublicChat>(username_), [this](auto res) { send_query(td::make_tl_object<td::td_api::searchPublicChat>(username_), [this](auto res) {
CHECK(res->get_id() == td_api::chat::ID); CHECK(res->get_id() == td::td_api::chat::ID);
auto chat = move_tl_object_as<td_api::chat>(res); auto chat = td::move_tl_object_as<td::td_api::chat>(res);
chat_id_ = chat->id_; chat_id_ = chat->id_;
this->one_file(); this->one_file();
}); });
} }
private: private:
string username_; td::string username_;
string tag_; td::string tag_;
Promise<> promise_; td::Promise<> promise_;
int64 chat_id_ = 0; td::int64 chat_id_ = 0;
void one_file() { void one_file() {
this->send_query( send_query(td::make_tl_object<td::td_api::sendMessage>(
make_tl_object<td_api::sendMessage>(
chat_id_, 0, 0, nullptr, nullptr, chat_id_, 0, 0, nullptr, nullptr,
make_tl_object<td_api::inputMessageText>( td::make_tl_object<td::td_api::inputMessageText>(
make_tl_object<td_api::formattedText>(PSTRING() << tag_ << " ONE_FILE", Auto()), false, false)), td::make_tl_object<td::td_api::formattedText>(PSTRING() << tag_ << " ONE_FILE", td::Auto()),
false, false)),
[](auto res) { check_td_error(res); }); [](auto res) { check_td_error(res); });
} }
@ -612,32 +621,32 @@ class CheckTestC : public Task {
if (!update->object) { if (!update->object) {
return; return;
} }
if (update->object->get_id() == td_api::updateNewMessage::ID) { if (update->object->get_id() == td::td_api::updateNewMessage::ID) {
auto updateNewMessage = move_tl_object_as<td_api::updateNewMessage>(update->object); auto updateNewMessage = td::move_tl_object_as<td::td_api::updateNewMessage>(update->object);
auto &message = updateNewMessage->message_; auto &message = updateNewMessage->message_;
if (message->content_->get_id() == td_api::messageDocument::ID) { if (message->content_->get_id() == td::td_api::messageDocument::ID) {
auto messageDocument = move_tl_object_as<td_api::messageDocument>(message->content_); auto messageDocument = td::move_tl_object_as<td::td_api::messageDocument>(message->content_);
auto text = messageDocument->caption_->text_; auto text = messageDocument->caption_->text_;
if (text.substr(0, tag_.size()) == tag_) { if (text.substr(0, tag_.size()) == tag_) {
file_id_to_check_ = messageDocument->document_->document_->id_; file_id_to_check_ = messageDocument->document_->document_->id_;
LOG(ERROR) << "GOT FILE " << to_string(messageDocument->document_->document_); LOG(ERROR) << "GOT FILE " << to_string(messageDocument->document_->document_);
this->send_query(make_tl_object<td_api::downloadFile>(file_id_to_check_, 1, 0, 0, false), send_query(td::make_tl_object<td::td_api::downloadFile>(file_id_to_check_, 1, 0, 0, false),
[](auto res) { check_td_error(res); }); [](auto res) { check_td_error(res); });
} }
} }
} else if (update->object->get_id() == td_api::updateFile::ID) { } else if (update->object->get_id() == td::td_api::updateFile::ID) {
auto updateFile = move_tl_object_as<td_api::updateFile>(update->object); auto updateFile = td::move_tl_object_as<td::td_api::updateFile>(update->object);
if (updateFile->file_->id_ == file_id_to_check_ && (updateFile->file_->local_->is_downloading_completed_)) { if (updateFile->file_->id_ == file_id_to_check_ && (updateFile->file_->local_->is_downloading_completed_)) {
check_file(updateFile->file_->local_->path_); check_file(updateFile->file_->local_->path_);
} }
} }
} }
void check_file(CSlice path) { void check_file(td::CSlice path) {
FILE *from = std::fopen(path.c_str(), "rb"); FILE *from = std::fopen(path.c_str(), "rb");
CHECK(from); CHECK(from);
uint32 x; td::uint32 x;
uint32 y = 1; td::uint32 y = 1;
while (std::fscanf(from, "%u", &x) == 1) { while (std::fscanf(from, "%u", &x) == 1) {
CHECK(x == y * y); CHECK(x == y * y);
y++; y++;
@ -645,28 +654,28 @@ class CheckTestC : public Task {
std::fclose(from); std::fclose(from);
stop(); stop();
} }
int32 file_id_to_check_ = 0; td::int32 file_id_to_check_ = 0;
}; };
class LoginTestActor : public Actor { class LoginTestActor : public td::Actor {
public: public:
explicit LoginTestActor(Status *status) : status_(status) { explicit LoginTestActor(td::Status *status) : status_(status) {
*status_ = Status::OK(); *status_ = td::Status::OK();
} }
private: private:
Status *status_; td::Status *status_;
ActorOwn<TestClient> alice_; td::ActorOwn<TestClient> alice_;
ActorOwn<TestClient> bob_; td::ActorOwn<TestClient> bob_;
string alice_phone_ = "9996636437"; td::string alice_phone_ = "9996636437";
string bob_phone_ = "9996636438"; td::string bob_phone_ = "9996636438";
string alice_username_ = "alice_" + alice_phone_; td::string alice_username_ = "alice_" + alice_phone_;
string bob_username_ = "bob_" + bob_phone_; td::string bob_username_ = "bob_" + bob_phone_;
string stage_name_; td::string stage_name_;
void begin_stage(string stage_name, double timeout) { void begin_stage(td::string stage_name, double timeout) {
LOG(WARNING) << "Begin stage '" << stage_name << "'"; LOG(WARNING) << "Begin stage '" << stage_name << "'";
stage_name_ = std::move(stage_name); stage_name_ = std::move(stage_name);
set_timeout_in(timeout); set_timeout_in(timeout);
@ -674,18 +683,18 @@ class LoginTestActor : public Actor {
void start_up() override { void start_up() override {
begin_stage("Logging in", 160); begin_stage("Logging in", 160);
alice_ = create_actor<TestClient>("AliceClient", "alice"); alice_ = td::create_actor<TestClient>("AliceClient", "alice");
bob_ = create_actor<TestClient>("BobClient", "bob"); bob_ = td::create_actor<TestClient>("BobClient", "bob");
send_closure(alice_, &TestClient::add_listener, td::send_closure(alice_, &TestClient::add_listener,
td::make_unique<DoAuthentication>( td::make_unique<DoAuthentication>(
"alice", alice_phone_, "33333", "alice", alice_phone_, "33333",
PromiseCreator::event(self_closure(this, &LoginTestActor::start_up_fence_dec)))); td::PromiseCreator::event(self_closure(this, &LoginTestActor::start_up_fence_dec))));
send_closure(bob_, &TestClient::add_listener, td::send_closure(bob_, &TestClient::add_listener,
td::make_unique<DoAuthentication>( td::make_unique<DoAuthentication>(
"bob", bob_phone_, "33333", "bob", bob_phone_, "33333",
PromiseCreator::event(self_closure(this, &LoginTestActor::start_up_fence_dec)))); td::PromiseCreator::event(self_closure(this, &LoginTestActor::start_up_fence_dec))));
} }
int start_up_fence_ = 3; int start_up_fence_ = 3;
@ -695,9 +704,9 @@ class LoginTestActor : public Actor {
init(); init();
} else if (start_up_fence_ == 1) { } else if (start_up_fence_ == 1) {
return init(); return init();
class WaitActor : public Actor { class WaitActor : public td::Actor {
public: public:
WaitActor(double timeout, Promise<> promise) : timeout_(timeout), promise_(std::move(promise)) { WaitActor(double timeout, td::Promise<> promise) : timeout_(timeout), promise_(std::move(promise)) {
} }
void start_up() override { void start_up() override {
set_timeout_in(timeout_); set_timeout_in(timeout_);
@ -708,21 +717,21 @@ class LoginTestActor : public Actor {
private: private:
double timeout_; double timeout_;
Promise<> promise_; td::Promise<> promise_;
}; };
create_actor<WaitActor>("WaitActor", 2, td::create_actor<WaitActor>("WaitActor", 2,
PromiseCreator::event(self_closure(this, &LoginTestActor::start_up_fence_dec))) td::PromiseCreator::event(self_closure(this, &LoginTestActor::start_up_fence_dec)))
.release(); .release();
} }
} }
void init() { void init() {
send_closure(alice_, &TestClient::add_listener, td::send_closure(alice_, &TestClient::add_listener,
td::make_unique<SetUsername>( td::make_unique<SetUsername>(alice_username_, td::PromiseCreator::event(self_closure(
alice_username_, PromiseCreator::event(self_closure(this, &LoginTestActor::init_fence_dec)))); this, &LoginTestActor::init_fence_dec))));
send_closure(bob_, &TestClient::add_listener, td::send_closure(bob_, &TestClient::add_listener,
td::make_unique<SetUsername>( td::make_unique<SetUsername>(bob_username_, td::PromiseCreator::event(self_closure(
bob_username_, PromiseCreator::event(self_closure(this, &LoginTestActor::init_fence_dec)))); this, &LoginTestActor::init_fence_dec))));
} }
int init_fence_ = 2; int init_fence_ = 2;
@ -732,7 +741,7 @@ class LoginTestActor : public Actor {
} }
} }
int32 test_a_fence_ = 2; int test_a_fence_ = 2;
void test_a_fence() { void test_a_fence() {
if (--test_a_fence_ == 0) { if (--test_a_fence_ == 0) {
test_b(); test_b();
@ -741,33 +750,33 @@ class LoginTestActor : public Actor {
void test_a() { void test_a() {
begin_stage("Ready to create chats", 80); begin_stage("Ready to create chats", 80);
string alice_tag = PSTRING() << format::as_hex(Random::secure_int64()); td::string alice_tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
string bob_tag = PSTRING() << format::as_hex(Random::secure_int64()); td::string bob_tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
send_closure(bob_, &TestClient::add_listener, td::send_closure(bob_, &TestClient::add_listener,
td::make_unique<CheckTestA>(alice_tag, td::make_unique<CheckTestA>(
PromiseCreator::event(self_closure(this, &LoginTestActor::test_a_fence)))); alice_tag, td::PromiseCreator::event(self_closure(this, &LoginTestActor::test_a_fence))));
send_closure( td::send_closure(alice_, &TestClient::add_listener,
alice_, &TestClient::add_listener, td::make_unique<CheckTestA>(
td::make_unique<CheckTestA>(bob_tag, PromiseCreator::event(self_closure(this, &LoginTestActor::test_a_fence)))); bob_tag, td::PromiseCreator::event(self_closure(this, &LoginTestActor::test_a_fence))));
send_closure(alice_, &TestClient::add_listener, td::make_unique<TestA>(alice_tag, bob_username_)); td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestA>(alice_tag, bob_username_));
send_closure(bob_, &TestClient::add_listener, td::make_unique<TestA>(bob_tag, alice_username_)); td::send_closure(bob_, &TestClient::add_listener, td::make_unique<TestA>(bob_tag, alice_username_));
// send_closure(alice_, &TestClient::add_listener, td::make_unique<TestChat>(bob_username_)); // td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestChat>(bob_username_));
} }
void timeout_expired() override { void timeout_expired() override {
LOG(FATAL) << "Timeout expired in stage '" << stage_name_ << "'"; LOG(FATAL) << "Timeout expired in stage '" << stage_name_ << "'";
} }
int32 test_b_fence_ = 1; int test_b_fence_ = 1;
void test_b_fence() { void test_b_fence() {
if (--test_b_fence_ == 0) { if (--test_b_fence_ == 0) {
test_c(); test_c();
} }
} }
int32 test_c_fence_ = 1; int test_c_fence_ = 1;
void test_c_fence() { void test_c_fence() {
if (--test_c_fence_ == 0) { if (--test_c_fence_ == 0) {
finish(); finish();
@ -776,39 +785,43 @@ class LoginTestActor : public Actor {
void test_b() { void test_b() {
begin_stage("Create secret chat", 40); begin_stage("Create secret chat", 40);
string tag = PSTRING() << format::as_hex(Random::secure_int64()); td::string tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
send_closure( td::send_closure(
bob_, &TestClient::add_listener, bob_, &TestClient::add_listener,
td::make_unique<CheckTestA>(tag, PromiseCreator::event(self_closure(this, &LoginTestActor::test_b_fence)))); td::make_unique<CheckTestA>(tag, td::PromiseCreator::event(self_closure(this, &LoginTestActor::test_b_fence))));
send_closure(alice_, &TestClient::add_listener, td::make_unique<TestSecretChat>(tag, bob_username_)); td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestSecretChat>(tag, bob_username_));
} }
void test_c() { void test_c() {
begin_stage("Send generated file", 240); begin_stage("Send generated file", 240);
string tag = PSTRING() << format::as_hex(Random::secure_int64()); td::string tag = PSTRING() << td::format::as_hex(td::Random::secure_int64());
send_closure(bob_, &TestClient::add_listener, td::send_closure(
bob_, &TestClient::add_listener,
td::make_unique<CheckTestC>(alice_username_, tag, td::make_unique<CheckTestC>(alice_username_, tag,
PromiseCreator::event(self_closure(this, &LoginTestActor::test_c_fence)))); td::PromiseCreator::event(self_closure(this, &LoginTestActor::test_c_fence))));
send_closure(alice_, &TestClient::add_listener, td::make_unique<TestFileGenerated>(tag, bob_username_)); td::send_closure(alice_, &TestClient::add_listener, td::make_unique<TestFileGenerated>(tag, bob_username_));
} }
int32 finish_fence_ = 2; int finish_fence_ = 2;
void finish_fence() { void finish_fence() {
finish_fence_--; finish_fence_--;
if (finish_fence_ == 0) { if (finish_fence_ == 0) {
Scheduler::instance()->finish(); td::Scheduler::instance()->finish();
stop(); stop();
} }
} }
void finish() { void finish() {
send_closure(alice_, &TestClient::close, PromiseCreator::event(self_closure(this, &LoginTestActor::finish_fence))); td::send_closure(alice_, &TestClient::close,
send_closure(bob_, &TestClient::close, PromiseCreator::event(self_closure(this, &LoginTestActor::finish_fence))); td::PromiseCreator::event(self_closure(this, &LoginTestActor::finish_fence)));
td::send_closure(bob_, &TestClient::close,
td::PromiseCreator::event(self_closure(this, &LoginTestActor::finish_fence)));
} }
}; };
class Tdclient_login : public Test { class Tdclient_login : public td::Test {
public: public:
using Test::Test; using Test::Test;
bool step() final { bool step() final {
@ -834,8 +847,8 @@ class Tdclient_login : public Test {
private: private:
bool is_inited_ = false; bool is_inited_ = false;
ConcurrentScheduler sched_; td::ConcurrentScheduler sched_;
Status result_; td::Status result_;
}; };
//RegisterTest<Tdclient_login> Tdclient_login("Tdclient_login"); //RegisterTest<Tdclient_login> Tdclient_login("Tdclient_login");
@ -861,7 +874,7 @@ TEST(Client, SimpleMulti) {
for (size_t i = 0; i < clients.size(); i++) { for (size_t i = 0; i < clients.size(); i++) {
clients[i].send({i + 2, td::make_tl_object<td::td_api::testSquareInt>(3)}); clients[i].send({i + 2, td::make_tl_object<td::td_api::testSquareInt>(3)});
if (Random::fast_bool()) { if (td::Random::fast_bool()) {
clients[i].send({1, td::make_tl_object<td::td_api::close>()}); clients[i].send({1, td::make_tl_object<td::td_api::close>()});
} }
} }
@ -937,15 +950,15 @@ TEST(Client, Manager) {
thread.join(); thread.join();
} }
std::set<int32> ids; std::set<td::int32> ids;
while (ids.size() != static_cast<size_t>(threads_n) * clients_n) { while (ids.size() != static_cast<size_t>(threads_n) * clients_n) {
auto event = client.receive(10); auto event = client.receive(10);
if (event.client_id == 0 || event.client_id == -1) { if (event.client_id == 0 || event.client_id == -1) {
ASSERT_EQ(td_api::error::ID, event.object->get_id()); ASSERT_EQ(td::td_api::error::ID, event.object->get_id());
continue; continue;
} }
if (event.request_id == 3) { if (event.request_id == 3) {
ASSERT_EQ(td_api::testInt::ID, event.object->get_id()); ASSERT_EQ(td::td_api::testInt::ID, event.object->get_id());
ASSERT_TRUE(ids.insert(event.client_id).second); ASSERT_TRUE(ids.insert(event.client_id).second);
} }
} }
@ -1070,17 +1083,13 @@ TEST(Client, ManagerClose) {
#endif #endif
TEST(PartsManager, hands) { TEST(PartsManager, hands) {
//Status init(int64 size, int64 expected_size, bool is_size_final, size_t part_size,
//const std::vector<int> &ready_parts, bool use_part_count_limit) TD_WARN_UNUSED_RESULT;
{ {
PartsManager pm; td::PartsManager pm;
pm.init(0, 100000, false, 10, {0, 1, 2}, false, true).ensure_error(); pm.init(0, 100000, false, 10, {0, 1, 2}, false, true).ensure_error();
//pm.set_known_prefix(0, false).ensure(); //pm.set_known_prefix(0, false).ensure();
} }
{ {
PartsManager pm; td::PartsManager pm;
pm.init(1, 100000, true, 10, {0, 1, 2}, false, true).ensure_error(); pm.init(1, 100000, true, 10, {0, 1, 2}, false, true).ensure_error();
} }
} }
} // namespace td