blob: d0625d35b38f4fdfdc776f4b73d43f388222fa79 [file] [edit]
#include <thread> // NOLINT(build/c++11)
#include "blocking_queue.h"
#include "event.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/status/status.h"
#include "absl/time/clock.h"
#include "absl/time/time.h"
#include "bmc/test_util.h"
namespace safepower_agent {
namespace {
using ::testing::status::StatusIs;
TEST(BlockingQueueThreadTest, PopAllWithTimeoutBlocksUntilEvent) {
BlockingQueue<Event> queue;
std::thread writer_thread([&queue]() {
absl::SleepFor(absl::Milliseconds(50));
queue.Push(Event{});
});
ASSERT_OK_AND_ASSIGN(auto events, queue.PopAllWithTimeout(absl::Seconds(10)));
EXPECT_EQ(events.size(), 1);
writer_thread.join();
}
TEST(BlockingQueueThreadTest, PopAllWithTimeoutBlocksUntilClose) {
BlockingQueue<Event> queue;
std::thread writer_thread([&queue]() {
absl::SleepFor(absl::Milliseconds(50));
queue.Close();
});
EXPECT_THAT(queue.PopAllWithTimeout(absl::Seconds(10)),
StatusIs(absl::StatusCode::kAborted));
writer_thread.join();
}
TEST(BlockingQueueThreadTest, PopWithTimeoutBlocksUntilEvent) {
BlockingQueue<Event> queue;
std::thread writer_thread([&queue]() {
absl::SleepFor(absl::Milliseconds(50));
queue.Push(Event{});
});
ASSERT_OK_AND_ASSIGN(Event event, queue.PopWithTimeout(absl::Seconds(10)));
(void)event;
writer_thread.join();
}
TEST(BlockingQueueThreadTest, PopWithTimeoutBlocksUntilClose) {
BlockingQueue<Event> queue;
std::thread writer_thread([&queue]() {
absl::SleepFor(absl::Milliseconds(50));
queue.Close();
});
EXPECT_THAT(queue.PopWithTimeout(absl::Seconds(10)),
StatusIs(absl::StatusCode::kAborted));
writer_thread.join();
}
} // namespace
} // namespace safepower_agent