Minor improvements.

GitOrigin-RevId: da7abae6b60c23f15f131ed33d54045f5b59990b
This commit is contained in:
levlam 2020-06-12 03:53:04 +03:00
parent 00c30aa9dc
commit 13af837169
2 changed files with 34 additions and 30 deletions

View File

@ -17,28 +17,28 @@
#include <set> #include <set>
#include <utility> #include <utility>
struct Data { struct ListData {
td::MovableValue<td::uint64> value; td::MovableValue<td::uint64> value;
td::MovableValue<bool> in_list; td::MovableValue<bool> in_list;
Data() = default; ListData() = default;
Data(td::uint64 value, bool in_list) : value(value), in_list(in_list) { ListData(td::uint64 value, bool in_list) : value(value), in_list(in_list) {
} }
}; };
struct Node : public td::ListNode { struct Node : public td::ListNode {
Node() = default; Node() = default;
explicit Node(Data data) : data(std::move(data)) { explicit Node(ListData data) : data(std::move(data)) {
} }
Data data; ListData data;
}; };
static Data &get_data(Node &node) { static ListData &get_data(Node &node) {
return node.data; return node.data;
} }
static Data &get_data(td::TsListNode<Data> &node) { static ListData &get_data(td::TsListNode<ListData> &node) {
return node.get_data_unsafe(); return node.get_data_unsafe();
} }
@ -46,7 +46,7 @@ static std::unique_lock<std::mutex> lock(td::ListNode &node) {
return {}; return {};
} }
static std::unique_lock<std::mutex> lock(td::TsListNode<Data> &node) { static std::unique_lock<std::mutex> lock(td::TsListNode<ListData> &node) {
return node.lock(); return node.lock();
} }
@ -78,7 +78,7 @@ static void do_run_list_test(ListRootT &root, std::atomic<td::uint64> &id) {
} }
auto i = rnd.fast(0, (int)nodes.size() - 1); auto i = rnd.fast(0, (int)nodes.size() - 1);
nodes[i].remove(); nodes[i].remove();
get_data(nodes[i]) = Data(next_id(), true); get_data(nodes[i]) = ListData(next_id(), true);
root.put(&nodes[i]); root.put(&nodes[i]);
}; };
auto unlink_node = [&] { auto unlink_node = [&] {
@ -144,19 +144,19 @@ TEST(Misc, List) {
} }
TEST(Misc, TsList) { TEST(Misc, TsList) {
td::TsList<Data> root; td::TsList<ListData> root;
std::atomic<td::uint64> id{0}; std::atomic<td::uint64> id{0};
for (std::size_t i = 0; i < 4; i++) { for (std::size_t i = 0; i < 4; i++) {
do_run_list_test<td::TsListNode<Data>, td::TsList<Data>, td::TsListNode<Data>>(root, id); do_run_list_test<td::TsListNode<ListData>, td::TsList<ListData>, td::TsListNode<ListData>>(root, id);
} }
} }
TEST(Misc, TsListConcurrent) { TEST(Misc, TsListConcurrent) {
td::TsList<Data> root; td::TsList<ListData> root;
td::vector<td::thread> threads; td::vector<td::thread> threads;
std::atomic<td::uint64> id{0}; std::atomic<td::uint64> id{0};
for (std::size_t i = 0; i < 4; i++) { for (std::size_t i = 0; i < 4; i++) {
threads.emplace_back( threads.emplace_back(
[&] { do_run_list_test<td::TsListNode<Data>, td::TsList<Data>, td::TsListNode<Data>>(root, id); }); [&] { do_run_list_test<td::TsListNode<ListData>, td::TsList<ListData>, td::TsListNode<ListData>>(root, id); });
} }
} }

View File

@ -45,13 +45,14 @@ class TestTQueue {
public: public:
using EventId = td::TQueue::EventId; using EventId = td::TQueue::EventId;
td::CSlice binlog_path() { static td::CSlice binlog_path() {
return "test_binlog"; return td::CSlice("tqueue_binlog");
} }
TestTQueue() { TestTQueue() {
baseline_ = td::TQueue::create(); baseline_ = td::TQueue::create();
memory_ = td::TQueue::create();
memory_ = td::TQueue::create();
auto memory_storage = td::make_unique<td::TQueueMemoryStorage>(); auto memory_storage = td::make_unique<td::TQueueMemoryStorage>();
memory_storage_ = memory_storage.get(); memory_storage_ = memory_storage.get();
memory_->set_callback(std::move(memory_storage)); memory_->set_callback(std::move(memory_storage));
@ -61,7 +62,7 @@ class TestTQueue {
td::Binlog::destroy(binlog_path()).ensure(); td::Binlog::destroy(binlog_path()).ensure();
auto binlog = std::make_shared<td::Binlog>(); auto binlog = std::make_shared<td::Binlog>();
binlog->init(binlog_path().str(), [&](const td::BinlogEvent &event) { UNREACHABLE(); }).ensure(); binlog->init(binlog_path().str(), [&](const td::BinlogEvent &event) { UNREACHABLE(); }).ensure();
tqueue_binlog->set_binlog(binlog); tqueue_binlog->set_binlog(std::move(binlog));
binlog_->set_callback(std::move(tqueue_binlog)); binlog_->set_callback(std::move(tqueue_binlog));
} }
@ -79,7 +80,7 @@ class TestTQueue {
memory_->run_gc(now); memory_->run_gc(now);
} }
if (rnd.fast(0, 100) != 0) { if (rnd.fast(0, 30) != 0) {
return; return;
} }
@ -89,9 +90,9 @@ class TestTQueue {
auto binlog = std::make_shared<td::Binlog>(); auto binlog = std::make_shared<td::Binlog>();
binlog->init(binlog_path().str(), [&](const td::BinlogEvent &event) { tqueue_binlog->replay(event, *binlog_); }) binlog->init(binlog_path().str(), [&](const td::BinlogEvent &event) { tqueue_binlog->replay(event, *binlog_); })
.ensure(); .ensure();
tqueue_binlog->set_binlog(binlog); tqueue_binlog->set_binlog(std::move(binlog));
binlog_->set_callback(std::move(tqueue_binlog)); binlog_->set_callback(std::move(tqueue_binlog));
if (rnd.fast(0, 10) == 0) { if (rnd.fast(0, 2) == 0) {
binlog_->run_gc(now); binlog_->run_gc(now);
} }
} }
@ -153,33 +154,36 @@ class TestTQueue {
TEST(TQueue, random) { TEST(TQueue, random) {
using EventId = td::TQueue::EventId; using EventId = td::TQueue::EventId;
td::Random::Xorshift128plus rnd(123); td::Random::Xorshift128plus rnd(123);
auto next_qid = [&] { auto next_queue_id = [&rnd] {
return rnd.fast(1, 10); return rnd.fast(1, 10);
}; };
auto next_first_id = [&] { auto next_first_id = [&rnd] {
if (rnd.fast(0, 3) == 0) {
return EventId::from_int32(EventId::MAX_ID - 20).move_as_ok(); return EventId::from_int32(EventId::MAX_ID - 20).move_as_ok();
//if (rnd.fast(0, 3) == 0) { }
//return EventId::from_int32(EventId::MAX_ID - 20).move_as_ok(); return EventId::from_int32(rnd.fast(1000000000, 1500000000)).move_as_ok();
//}
//return EventId::from_int32(rnd.fast(1000000000, 1500000000)).move_as_ok();
}; };
TestTQueue q; TestTQueue q;
double now = 0; double now = 0;
auto push_event = [&] { auto push_event = [&] {
auto data = PSTRING() << rnd(); auto data = PSTRING() << rnd();
q.push(next_qid(), data, now + rnd.fast(-10, 10) * 10 + 5, next_first_id()); if (rnd.fast(0, 10000) == 0) {
data = td::string(1 << 19, '\0');
}
q.push(next_queue_id(), data, now + rnd.fast(-10, 10) * 10 + 5, next_first_id());
}; };
auto inc_now = [&] { auto inc_now = [&] {
now += 10; now += 10;
}; };
auto check_head_tail = [&] { auto check_head_tail = [&] {
q.check_head_tail(next_qid(), now); q.check_head_tail(next_queue_id(), now);
}; };
auto restart = [&] { auto restart = [&] {
q.restart(rnd, now); q.restart(rnd, now);
}; };
auto get = [&] { auto get = [&] {
q.check_get(next_qid(), rnd, now); q.check_get(next_queue_id(), rnd, now);
}; };
td::RandomSteps steps({{push_event, 100}, {check_head_tail, 10}, {get, 40}, {inc_now, 5}, {restart, 1}}); td::RandomSteps steps({{push_event, 100}, {check_head_tail, 10}, {get, 40}, {inc_now, 5}, {restart, 1}});
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {