|
| 1 | +// Copyright 2022 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +#include "google/cloud/pubsublite/internal/alarm_registry_impl.h" |
| 16 | +#include "google/cloud/future.h" |
| 17 | +#include "google/cloud/status_or.h" |
| 18 | +#include "google/cloud/testing_util/async_sequencer.h" |
| 19 | +#include "google/cloud/testing_util/mock_completion_queue_impl.h" |
| 20 | +#include <gmock/gmock.h> |
| 21 | +#include <chrono> |
| 22 | +#include <deque> |
| 23 | +#include <memory> |
| 24 | +#include <thread> |
| 25 | + |
| 26 | +namespace google { |
| 27 | +namespace cloud { |
| 28 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 29 | +namespace pubsublite_internal { |
| 30 | +namespace { |
| 31 | + |
| 32 | +using google::cloud::CompletionQueue; |
| 33 | +using google::cloud::testing_util::AsyncSequencer; |
| 34 | +using google::cloud::testing_util::MockCompletionQueueImpl; |
| 35 | +using ::testing::ByMove; |
| 36 | +using ::testing::InSequence; |
| 37 | +using ::testing::MockFunction; |
| 38 | +using ::testing::Return; |
| 39 | +using ::testing::StrictMock; |
| 40 | + |
| 41 | +// 10,000 seconds to ensure this test has no time dependence |
| 42 | +auto constexpr kAlarmPeriod = |
| 43 | + std::chrono::milliseconds{10000 * std::chrono::seconds(1)}; |
| 44 | + |
| 45 | +class AlarmRegistryImplTest : public ::testing::Test { |
| 46 | + protected: |
| 47 | + AlarmRegistryImplTest() |
| 48 | + : cq_{std::make_shared<StrictMock<MockCompletionQueueImpl>>()}, |
| 49 | + alarm_{CompletionQueue{cq_}} {} |
| 50 | + std::shared_ptr<StrictMock<MockCompletionQueueImpl>> cq_; |
| 51 | + AlarmRegistryImpl alarm_; |
| 52 | + StrictMock<MockFunction<void()>> fun_; |
| 53 | + AsyncSequencer<StatusOr<std::chrono::system_clock::time_point>> sequencer_; |
| 54 | +}; |
| 55 | + |
| 56 | +TEST_F(AlarmRegistryImplTest, TokenDestroyedBeforeRun) { |
| 57 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 58 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 59 | + auto token = alarm_.RegisterAlarm(kAlarmPeriod, fun_.AsStdFunction()); |
| 60 | + token = nullptr; |
| 61 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 62 | +} |
| 63 | + |
| 64 | +TEST_F(AlarmRegistryImplTest, TimerErrorBeforeRun) { |
| 65 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 66 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 67 | + alarm_.RegisterAlarm(kAlarmPeriod, fun_.AsStdFunction()); |
| 68 | + sequencer_.PopFront().set_value(Status{StatusCode::kCancelled, "cancelled"}); |
| 69 | +} |
| 70 | + |
| 71 | +TEST_F(AlarmRegistryImplTest, TokenDestroyedAfterSingleRun) { |
| 72 | + InSequence seq; |
| 73 | + |
| 74 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 75 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 76 | + auto token = alarm_.RegisterAlarm(kAlarmPeriod, fun_.AsStdFunction()); |
| 77 | + |
| 78 | + EXPECT_CALL(fun_, Call); |
| 79 | + |
| 80 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 81 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 82 | + |
| 83 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 84 | + |
| 85 | + token = nullptr; |
| 86 | + |
| 87 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 88 | +} |
| 89 | + |
| 90 | +TEST_F(AlarmRegistryImplTest, TokenDestroyedAfterFiveRuns) { |
| 91 | + InSequence seq; |
| 92 | + |
| 93 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 94 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 95 | + auto token = alarm_.RegisterAlarm(kAlarmPeriod, fun_.AsStdFunction()); |
| 96 | + |
| 97 | + for (unsigned int i = 0; i < 5; ++i) { |
| 98 | + EXPECT_CALL(fun_, Call); |
| 99 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 100 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 101 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 102 | + } |
| 103 | + |
| 104 | + token = nullptr; |
| 105 | + |
| 106 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 107 | +} |
| 108 | + |
| 109 | +TEST_F(AlarmRegistryImplTest, TokenDestroyedDuringSecondRun) { |
| 110 | + InSequence seq; |
| 111 | + |
| 112 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 113 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 114 | + auto token = alarm_.RegisterAlarm(kAlarmPeriod, fun_.AsStdFunction()); |
| 115 | + |
| 116 | + EXPECT_CALL(fun_, Call); |
| 117 | + |
| 118 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 119 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 120 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 121 | + |
| 122 | + promise<void> in_alarm_function; |
| 123 | + promise<void> destroy_finished; |
| 124 | + |
| 125 | + EXPECT_CALL(fun_, Call).WillOnce([&]() { |
| 126 | + in_alarm_function.set_value(); |
| 127 | + // Unable to destroy token while the alarm is outstanding |
| 128 | + EXPECT_EQ(destroy_finished.get_future().wait_for(std::chrono::seconds(2)), |
| 129 | + std::future_status::timeout); |
| 130 | + }); |
| 131 | + |
| 132 | + std::thread first{[&]() { |
| 133 | + // guarantee that alarm function is being invoked |
| 134 | + in_alarm_function.get_future().get(); |
| 135 | + token = nullptr; |
| 136 | + destroy_finished.set_value(); |
| 137 | + }}; |
| 138 | + |
| 139 | + EXPECT_CALL(*cq_, MakeRelativeTimer(std::chrono::nanoseconds(kAlarmPeriod))) |
| 140 | + .WillOnce(Return(ByMove(sequencer_.PushBack()))); |
| 141 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 142 | + |
| 143 | + first.join(); |
| 144 | + |
| 145 | + sequencer_.PopFront().set_value(std::chrono::system_clock::time_point{}); |
| 146 | +} |
| 147 | + |
| 148 | +} // namespace |
| 149 | +} // namespace pubsublite_internal |
| 150 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 151 | +} // namespace cloud |
| 152 | +} // namespace google |
0 commit comments