|
| 1 | +// Copyright 2026 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/bigtable/internal/channel_usage.h" |
| 16 | +#include "google/cloud/bigtable/testing/mock_bigtable_stub.h" |
| 17 | +#include "google/cloud/internal/make_status.h" |
| 18 | +#include "google/cloud/testing_util/fake_clock.h" |
| 19 | +#include "google/cloud/testing_util/status_matchers.h" |
| 20 | +#include <gmock/gmock.h> |
| 21 | + |
| 22 | +namespace google { |
| 23 | +namespace cloud { |
| 24 | +namespace bigtable_internal { |
| 25 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_BEGIN |
| 26 | +namespace { |
| 27 | + |
| 28 | +using ::google::cloud::bigtable::testing::MockBigtableStub; |
| 29 | +using ::google::cloud::testing_util::IsOkAndHolds; |
| 30 | +using ::google::cloud::testing_util::StatusIs; |
| 31 | +using ::testing::Eq; |
| 32 | + |
| 33 | +TEST(ChannelUsageTest, SetChannel) { |
| 34 | + auto mock = std::make_shared<MockBigtableStub>(); |
| 35 | + auto channel = std::make_shared<ChannelUsage<BigtableStub>>(); |
| 36 | + EXPECT_THAT(channel->AcquireStub(), Eq(nullptr)); |
| 37 | + channel->set_stub(mock); |
| 38 | + EXPECT_THAT(channel->AcquireStub(), Eq(mock)); |
| 39 | + auto mock2 = std::make_shared<MockBigtableStub>(); |
| 40 | + channel->set_stub(mock2); |
| 41 | + EXPECT_THAT(channel->AcquireStub(), Eq(mock)); |
| 42 | +} |
| 43 | + |
| 44 | +TEST(ChannelUsageTest, InstantOutstandingRpcs) { |
| 45 | + auto mock = std::make_shared<MockBigtableStub>(); |
| 46 | + auto channel = std::make_shared<ChannelUsage<BigtableStub>>(mock); |
| 47 | + |
| 48 | + auto stub = channel->AcquireStub(); |
| 49 | + EXPECT_THAT(stub, Eq(mock)); |
| 50 | + EXPECT_THAT(channel->instant_outstanding_rpcs(), IsOkAndHolds(1)); |
| 51 | + stub = channel->AcquireStub(); |
| 52 | + EXPECT_THAT(stub, Eq(mock)); |
| 53 | + stub = channel->AcquireStub(); |
| 54 | + EXPECT_THAT(stub, Eq(mock)); |
| 55 | + EXPECT_THAT(channel->instant_outstanding_rpcs(), IsOkAndHolds(3)); |
| 56 | + channel->ReleaseStub(); |
| 57 | + EXPECT_THAT(channel->instant_outstanding_rpcs(), IsOkAndHolds(2)); |
| 58 | + channel->ReleaseStub(); |
| 59 | + channel->ReleaseStub(); |
| 60 | + EXPECT_THAT(channel->instant_outstanding_rpcs(), IsOkAndHolds(0)); |
| 61 | +} |
| 62 | + |
| 63 | +TEST(ChannelUsageTest, SetLastRefreshStatus) { |
| 64 | + auto mock = std::make_shared<MockBigtableStub>(); |
| 65 | + auto channel = std::make_shared<ChannelUsage<BigtableStub>>(mock); |
| 66 | + Status expected_status = internal::InternalError("uh oh"); |
| 67 | + (void)channel->AcquireStub(); |
| 68 | + EXPECT_THAT(channel->instant_outstanding_rpcs(), IsOkAndHolds(1)); |
| 69 | + channel->set_last_refresh_status(expected_status); |
| 70 | + EXPECT_THAT(channel->instant_outstanding_rpcs(), |
| 71 | + StatusIs(expected_status.code())); |
| 72 | + EXPECT_THAT(channel->average_outstanding_rpcs(), |
| 73 | + StatusIs(expected_status.code())); |
| 74 | +} |
| 75 | + |
| 76 | +TEST(ChannelUsageTest, AverageOutstandingRpcs) { |
| 77 | + auto clock = std::make_shared<testing_util::FakeSteadyClock>(); |
| 78 | + auto mock = std::make_shared<MockBigtableStub>(); |
| 79 | + auto channel = std::make_shared<ChannelUsage<BigtableStub>>(mock, clock); |
| 80 | + EXPECT_THAT(channel->instant_outstanding_rpcs(), IsOkAndHolds(0)); |
| 81 | + |
| 82 | + auto start = std::chrono::steady_clock::now(); |
| 83 | + clock->SetTime(start); |
| 84 | + |
| 85 | + for (int i = 0; i < 10; ++i) (void)channel->AcquireStub(); |
| 86 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(10)); |
| 87 | + |
| 88 | + clock->AdvanceTime(std::chrono::seconds(1)); |
| 89 | + // sum=10 total_weight=1 |
| 90 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(10)); |
| 91 | + |
| 92 | + for (int i = 0; i < 10; ++i) (void)channel->AcquireStub(); |
| 93 | + clock->AdvanceTime(std::chrono::seconds(1)); |
| 94 | + // sum=30, total_weight=2 |
| 95 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(15)); |
| 96 | + |
| 97 | + for (int i = 0; i < 20; ++i) channel->ReleaseStub(); |
| 98 | + clock->AdvanceTime(std::chrono::seconds(1)); |
| 99 | + // sum=30, total_weight=3 |
| 100 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(10)); |
| 101 | + |
| 102 | + clock->AdvanceTime(std::chrono::seconds(2)); |
| 103 | + // sum=30, total_weight=5 |
| 104 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(6)); |
| 105 | + |
| 106 | + for (int i = 0; i < 100; ++i) (void)channel->AcquireStub(); |
| 107 | + clock->AdvanceTime(std::chrono::seconds(25)); |
| 108 | + // sum=2530, total_weight=84 |
| 109 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(84)); |
| 110 | + |
| 111 | + clock->AdvanceTime(std::chrono::seconds(35)); |
| 112 | + // First 5s of measurements have aged out. |
| 113 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(100)); |
| 114 | + |
| 115 | + clock->AdvanceTime(std::chrono::seconds(60)); |
| 116 | + // All measurements have aged out. |
| 117 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(100)); |
| 118 | + |
| 119 | + clock->AdvanceTime(std::chrono::seconds(3600)); |
| 120 | + // All measurements have aged out. |
| 121 | + EXPECT_THAT(channel->average_outstanding_rpcs(), IsOkAndHolds(100)); |
| 122 | +} |
| 123 | + |
| 124 | +TEST(ChannelUsageTest, MakeWeak) { |
| 125 | + auto channel = std::make_shared<ChannelUsage<BigtableStub>>(); |
| 126 | + auto weak = channel->MakeWeak(); |
| 127 | + EXPECT_THAT(weak.lock(), Eq(channel)); |
| 128 | + channel.reset(); |
| 129 | + EXPECT_THAT(weak.lock(), Eq(nullptr)); |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace |
| 133 | +GOOGLE_CLOUD_CPP_INLINE_NAMESPACE_END |
| 134 | +} // namespace bigtable_internal |
| 135 | +} // namespace cloud |
| 136 | +} // namespace google |
0 commit comments