| #include "blocking_queue.h" | |
| #include <vector> | |
| #include "event.h" | |
| #include "gmock/gmock.h" | |
| #include "gtest/gtest.h" | |
| #include "absl/functional/any_invocable.h" | |
| #include "absl/status/status.h" | |
| #include "absl/time/time.h" | |
| #include "bmc/test_util.h" | |
| namespace safepower_agent { | |
| namespace { | |
| using ::testing::ElementsAre; | |
| using ::testing::status::StatusIs; | |
| TEST(BlockingQueueTest, PushAndPopSingleEvent) { | |
| BlockingQueue<Event> queue; | |
| queue.Push(Event{}); | |
| ASSERT_OK_AND_ASSIGN(auto events, | |
| queue.PopAllWithTimeout(absl::ZeroDuration())); | |
| EXPECT_EQ(events.size(), 1); | |
| } | |
| TEST(BlockingQueueTest, PopSingleWithTimeout) { | |
| BlockingQueue<absl::AnyInvocable<void()>> queue; | |
| bool executed = false; | |
| queue.Push([&executed]() { executed = true; }); | |
| ASSERT_OK_AND_ASSIGN(auto task, queue.PopWithTimeout(absl::ZeroDuration())); | |
| task(); | |
| EXPECT_TRUE(executed); | |
| EXPECT_THAT(queue.PopWithTimeout(absl::Milliseconds(10)), | |
| StatusIs(absl::StatusCode::kDeadlineExceeded)); | |
| queue.Close(); | |
| EXPECT_THAT(queue.PopWithTimeout(absl::ZeroDuration()), | |
| StatusIs(absl::StatusCode::kAborted)); | |
| } | |
| TEST(BlockingQueueTest, PopSingleMultipleTimes) { | |
| BlockingQueue<absl::AnyInvocable<void()>> queue; | |
| std::vector<int> order; | |
| queue.Push([&order]() { order.push_back(1); }); | |
| queue.Push([&order]() { order.push_back(2); }); | |
| queue.Push([&order]() { order.push_back(3); }); | |
| for (int i = 0; i < 3; ++i) { | |
| ASSERT_OK_AND_ASSIGN(auto task, queue.PopWithTimeout(absl::ZeroDuration())); | |
| task(); | |
| } | |
| EXPECT_THAT(order, ElementsAre(1, 2, 3)); | |
| EXPECT_THAT(queue.PopWithTimeout(absl::ZeroDuration()), | |
| StatusIs(absl::StatusCode::kDeadlineExceeded)); | |
| } | |
| TEST(BlockingQueueTest, PopSingleThenPopAll) { | |
| BlockingQueue<absl::AnyInvocable<void()>> queue; | |
| std::vector<int> order; | |
| queue.Push([&order]() { order.push_back(1); }); | |
| queue.Push([&order]() { order.push_back(2); }); | |
| queue.Push([&order]() { order.push_back(3); }); | |
| ASSERT_OK_AND_ASSIGN(auto first_task, | |
| queue.PopWithTimeout(absl::ZeroDuration())); | |
| first_task(); | |
| EXPECT_THAT(order, ElementsAre(1)); | |
| ASSERT_OK_AND_ASSIGN(auto remaining_tasks, | |
| queue.PopAllWithTimeout(absl::ZeroDuration())); | |
| ASSERT_EQ(remaining_tasks.size(), 2); | |
| for (auto& task : remaining_tasks) { | |
| task(); | |
| } | |
| EXPECT_THAT(order, ElementsAre(1, 2, 3)); | |
| } | |
| TEST(BlockingQueueTest, PopAllThenPopSingle) { | |
| BlockingQueue<absl::AnyInvocable<void()>> queue; | |
| std::vector<int> order; | |
| queue.Push([&order]() { order.push_back(1); }); | |
| queue.Push([&order]() { order.push_back(2); }); | |
| ASSERT_OK_AND_ASSIGN(auto batch, | |
| queue.PopAllWithTimeout(absl::ZeroDuration())); | |
| ASSERT_EQ(batch.size(), 2); | |
| for (auto& task : batch) { | |
| task(); | |
| } | |
| EXPECT_THAT(order, ElementsAre(1, 2)); | |
| queue.Push([&order]() { order.push_back(3); }); | |
| ASSERT_OK_AND_ASSIGN(auto single_task, | |
| queue.PopWithTimeout(absl::ZeroDuration())); | |
| single_task(); | |
| EXPECT_THAT(order, ElementsAre(1, 2, 3)); | |
| } | |
| TEST(BlockingQueueTest, PopAllDrainsQueue) { | |
| BlockingQueue<Event> queue; | |
| queue.Push(Event{}); | |
| queue.Push(Event{}); | |
| queue.Push(Event{}); | |
| ASSERT_OK_AND_ASSIGN(auto events, | |
| queue.PopAllWithTimeout(absl::ZeroDuration())); | |
| EXPECT_EQ(events.size(), 3); | |
| // Subsequent pop should be empty. | |
| ASSERT_OK_AND_ASSIGN(auto empty_events, | |
| queue.PopAllWithTimeout(absl::ZeroDuration())); | |
| EXPECT_TRUE(empty_events.empty()); | |
| } | |
| TEST(BlockingQueueTest, PopAllWithTimeoutTimesOut) { | |
| BlockingQueue<Event> queue; | |
| ASSERT_OK_AND_ASSIGN(auto events, | |
| queue.PopAllWithTimeout(absl::Milliseconds(10))); | |
| EXPECT_TRUE(events.empty()); | |
| } | |
| TEST(BlockingQueueTest, PopAfterCloseReturnsError) { | |
| BlockingQueue<Event> queue; | |
| queue.Close(); | |
| EXPECT_THAT(queue.PopAllWithTimeout(absl::ZeroDuration()), | |
| StatusIs(absl::StatusCode::kAborted)); | |
| } | |
| TEST(BlockingQueueTest, PopDrainsRemainingEventsAfterClose) { | |
| BlockingQueue<Event> queue; | |
| queue.Push(Event{}); | |
| queue.Close(); | |
| // First pop should succeed and return the pushed event. | |
| ASSERT_OK_AND_ASSIGN(auto first_events, | |
| queue.PopAllWithTimeout(absl::ZeroDuration())); | |
| EXPECT_EQ(first_events.size(), 1); | |
| // Second pop should return error because it is empty and closed. | |
| EXPECT_THAT(queue.PopAllWithTimeout(absl::ZeroDuration()), | |
| StatusIs(absl::StatusCode::kAborted)); | |
| } | |
| } // namespace | |
| } // namespace safepower_agent |