From 13af8371690d6605b33e2b1d3d93991ea1770dda Mon Sep 17 00:00:00 2001 From: levlam Date: Fri, 12 Jun 2020 03:53:04 +0300 Subject: [PATCH] Minor improvements. GitOrigin-RevId: da7abae6b60c23f15f131ed33d54045f5b59990b --- tdutils/test/List.cpp | 26 +++++++++++++------------- test/tqueue.cpp | 38 +++++++++++++++++++++----------------- 2 files changed, 34 insertions(+), 30 deletions(-) diff --git a/tdutils/test/List.cpp b/tdutils/test/List.cpp index d84727bb4..6cfeb912a 100644 --- a/tdutils/test/List.cpp +++ b/tdutils/test/List.cpp @@ -17,28 +17,28 @@ #include #include -struct Data { +struct ListData { td::MovableValue value; td::MovableValue in_list; - Data() = default; - Data(td::uint64 value, bool in_list) : value(value), in_list(in_list) { + ListData() = default; + ListData(td::uint64 value, bool in_list) : value(value), in_list(in_list) { } }; struct Node : public td::ListNode { 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; } -static Data &get_data(td::TsListNode &node) { +static ListData &get_data(td::TsListNode &node) { return node.get_data_unsafe(); } @@ -46,7 +46,7 @@ static std::unique_lock lock(td::ListNode &node) { return {}; } -static std::unique_lock lock(td::TsListNode &node) { +static std::unique_lock lock(td::TsListNode &node) { return node.lock(); } @@ -78,7 +78,7 @@ static void do_run_list_test(ListRootT &root, std::atomic &id) { } auto i = rnd.fast(0, (int)nodes.size() - 1); nodes[i].remove(); - get_data(nodes[i]) = Data(next_id(), true); + get_data(nodes[i]) = ListData(next_id(), true); root.put(&nodes[i]); }; auto unlink_node = [&] { @@ -144,19 +144,19 @@ TEST(Misc, List) { } TEST(Misc, TsList) { - td::TsList root; + td::TsList root; std::atomic id{0}; for (std::size_t i = 0; i < 4; i++) { - do_run_list_test, td::TsList, td::TsListNode>(root, id); + do_run_list_test, td::TsList, td::TsListNode>(root, id); } } TEST(Misc, TsListConcurrent) { - td::TsList root; + td::TsList root; td::vector threads; std::atomic id{0}; for (std::size_t i = 0; i < 4; i++) { threads.emplace_back( - [&] { do_run_list_test, td::TsList, td::TsListNode>(root, id); }); + [&] { do_run_list_test, td::TsList, td::TsListNode>(root, id); }); } } diff --git a/test/tqueue.cpp b/test/tqueue.cpp index 7f6e23648..ca1d321a5 100644 --- a/test/tqueue.cpp +++ b/test/tqueue.cpp @@ -45,13 +45,14 @@ class TestTQueue { public: using EventId = td::TQueue::EventId; - td::CSlice binlog_path() { - return "test_binlog"; + static td::CSlice binlog_path() { + return td::CSlice("tqueue_binlog"); } + TestTQueue() { baseline_ = td::TQueue::create(); - memory_ = td::TQueue::create(); + memory_ = td::TQueue::create(); auto memory_storage = td::make_unique(); memory_storage_ = memory_storage.get(); memory_->set_callback(std::move(memory_storage)); @@ -61,7 +62,7 @@ class TestTQueue { td::Binlog::destroy(binlog_path()).ensure(); auto binlog = std::make_shared(); 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)); } @@ -79,7 +80,7 @@ class TestTQueue { memory_->run_gc(now); } - if (rnd.fast(0, 100) != 0) { + if (rnd.fast(0, 30) != 0) { return; } @@ -89,9 +90,9 @@ class TestTQueue { auto binlog = std::make_shared(); binlog->init(binlog_path().str(), [&](const td::BinlogEvent &event) { tqueue_binlog->replay(event, *binlog_); }) .ensure(); - tqueue_binlog->set_binlog(binlog); + tqueue_binlog->set_binlog(std::move(binlog)); binlog_->set_callback(std::move(tqueue_binlog)); - if (rnd.fast(0, 10) == 0) { + if (rnd.fast(0, 2) == 0) { binlog_->run_gc(now); } } @@ -153,33 +154,36 @@ class TestTQueue { TEST(TQueue, random) { using EventId = td::TQueue::EventId; td::Random::Xorshift128plus rnd(123); - auto next_qid = [&] { + auto next_queue_id = [&rnd] { return rnd.fast(1, 10); }; - auto next_first_id = [&] { - 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(); + 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(rnd.fast(1000000000, 1500000000)).move_as_ok(); }; + TestTQueue q; double now = 0; auto push_event = [&] { 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 = [&] { now += 10; }; auto check_head_tail = [&] { - q.check_head_tail(next_qid(), now); + q.check_head_tail(next_queue_id(), now); }; auto restart = [&] { q.restart(rnd, now); }; 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}}); for (int i = 0; i < 100000; i++) {