aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/yt/small_containers/unittests
diff options
context:
space:
mode:
authorGrigory Reznikov <gritukan@gmail.com>2023-12-05 07:40:45 +0300
committerrobot-piglet <robot-piglet@yandex-team.com>2023-12-05 09:48:20 +0300
commit7bf3df81755926d2fce972d4ac113e4c0886d008 (patch)
treee73e8b7e921a8193830d8b1436c2bc73eadbe890 /library/cpp/yt/small_containers/unittests
parente92c23fea0e3ebe3613fd2e0708e09abd35aa5fb (diff)
downloadydb-7bf3df81755926d2fce972d4ac113e4c0886d008.tar.gz
YT-18655 Support prerequisite transactions in dynamic tables write
We are building a service that uses dynamic tables as a metadata storage. Cypress is used for a leader election and prerequisite transactions are used to prevent races, however prerequisite transactions are not supported for dynamic table operations. This PR fixes that. I hereby agree to the terms of the CLA available at: https://yandex.ru/legal/cla/?lang=en --- Pull Request resolved: https://github.com/ytsaurus/ytsaurus/pull/162
Diffstat (limited to 'library/cpp/yt/small_containers/unittests')
-rw-r--r--library/cpp/yt/small_containers/unittests/compact_queue_ut.cpp114
-rw-r--r--library/cpp/yt/small_containers/unittests/ya.make1
2 files changed, 115 insertions, 0 deletions
diff --git a/library/cpp/yt/small_containers/unittests/compact_queue_ut.cpp b/library/cpp/yt/small_containers/unittests/compact_queue_ut.cpp
new file mode 100644
index 0000000000..acea1c5c71
--- /dev/null
+++ b/library/cpp/yt/small_containers/unittests/compact_queue_ut.cpp
@@ -0,0 +1,114 @@
+#include <library/cpp/yt/small_containers/compact_queue.h>
+
+#include <library/cpp/testing/gtest/gtest.h>
+
+#include <queue>
+#include <random>
+
+namespace NYT {
+namespace {
+
+////////////////////////////////////////////////////////////////////////////////
+
+TEST(TCompactQueueTest, Simple)
+{
+ TCompactQueue<int, 4> queue;
+ queue.Push(1);
+ queue.Push(2);
+ queue.Push(3);
+
+ for (int i = 1; i <= 10; i++) {
+ EXPECT_EQ(queue.Size(), 3u);
+ EXPECT_EQ(queue.Front(), i);
+ EXPECT_EQ(queue.Pop(), i);
+ queue.Push(i + 3);
+ }
+
+ for (int i = 11; i <= 13; i++) {
+ EXPECT_EQ(queue.Front(), i);
+ queue.Pop();
+ }
+
+ EXPECT_TRUE(queue.Empty());
+}
+
+TEST(TCompactQueueTest, Reallocation1)
+{
+ TCompactQueue<int, 2> queue;
+ queue.Push(1);
+ queue.Push(2);
+ queue.Push(3);
+
+ for (int i = 1; i <= 10; i++) {
+ EXPECT_EQ(queue.Size(), 3u);
+ EXPECT_EQ(queue.Front(), i);
+ EXPECT_EQ(queue.Pop(), i);
+ queue.Push(i + 3);
+ }
+
+ for (int i = 11; i <= 13; i++) {
+ EXPECT_EQ(queue.Front(), i);
+ queue.Pop();
+ }
+
+ EXPECT_TRUE(queue.Empty());
+}
+
+TEST(TCompactQueueTest, Reallocation2)
+{
+ TCompactQueue<int, 3> queue;
+ queue.Push(1);
+ queue.Push(2);
+ queue.Push(3);
+ EXPECT_EQ(queue.Pop(), 1);
+ queue.Push(4);
+ queue.Push(5);
+
+ EXPECT_EQ(queue.Size(), 4u);
+
+ for (int i = 2; i <= 10; i++) {
+ EXPECT_EQ(queue.Size(), 4u);
+ EXPECT_EQ(queue.Front(), i);
+ EXPECT_EQ(queue.Pop(), i);
+ queue.Push(i + 4);
+ }
+
+ for (int i = 11; i <= 14; i++) {
+ EXPECT_EQ(queue.Front(), i);
+ queue.Pop();
+ }
+
+ EXPECT_TRUE(queue.Empty());
+}
+
+TEST(TCompactQueueTest, Stress)
+{
+ std::mt19937_64 rng(42);
+
+ for (int iteration = 0; iteration < 1000; ++iteration) {
+ TCompactQueue<int, 4> queue1;
+ std::queue<int> queue2;
+
+ for (int step = 0; step < 10'000; ++step) {
+ EXPECT_EQ(queue1.Size(), queue2.size());
+ EXPECT_EQ(queue1.Empty(), queue2.empty());
+ if (!queue1.Empty()) {
+ EXPECT_EQ(queue1.Front(), queue2.front());
+ }
+
+ if (queue2.empty() || rng() % 2 == 0) {
+ int value = rng() % 1'000'000;
+ queue1.Push(value);
+ queue2.push(value);
+ } else {
+ queue1.Pop();
+ queue2.pop();
+ }
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////////////////////
+
+} // namespace
+} // namespace NYT
diff --git a/library/cpp/yt/small_containers/unittests/ya.make b/library/cpp/yt/small_containers/unittests/ya.make
index bf3deade90..b09ef44f8d 100644
--- a/library/cpp/yt/small_containers/unittests/ya.make
+++ b/library/cpp/yt/small_containers/unittests/ya.make
@@ -7,6 +7,7 @@ SRCS(
compact_heap_ut.cpp
compact_set_ut.cpp
compact_vector_ut.cpp
+ compact_queue_ut.cpp
)
PEERDIR(