|
| 1 | +// Copyright 2018 The Abseil Authors. |
| 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 | +// http://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 <cstddef> |
| 16 | + |
| 17 | +#include "gmock/gmock.h" |
| 18 | +#include "gtest/gtest.h" |
| 19 | +#include "absl/container/internal/hashtablez_sampler.h" |
| 20 | + |
| 21 | +namespace absl { |
| 22 | +namespace container_internal { |
| 23 | + |
| 24 | +class HashtablezInfoHandlePeer { |
| 25 | + public: |
| 26 | + static bool IsSampled(const HashtablezInfoHandle& h) { |
| 27 | + return h.info_ != nullptr; |
| 28 | + } |
| 29 | +}; |
| 30 | + |
| 31 | +namespace { |
| 32 | + |
| 33 | +bool samples[3]{true, true, true}; |
| 34 | + |
| 35 | +// We do this test in a global object to test that this works even before main. |
| 36 | +struct Global { |
| 37 | + Global() { |
| 38 | + // By default it is sampled. |
| 39 | + samples[0] = HashtablezInfoHandlePeer::IsSampled(Sample()); |
| 40 | + |
| 41 | + // Even with a large parameter, it is sampled. |
| 42 | + SetHashtablezSampleParameter(100); |
| 43 | + samples[1] = HashtablezInfoHandlePeer::IsSampled(Sample()); |
| 44 | + |
| 45 | + // Even if we turn it off, it is still sampled. |
| 46 | + SetHashtablezEnabled(false); |
| 47 | + samples[2] = HashtablezInfoHandlePeer::IsSampled(Sample()); |
| 48 | + } |
| 49 | +} global; |
| 50 | + |
| 51 | +TEST(kAbslContainerInternalSampleEverything, Works) { |
| 52 | + EXPECT_THAT(samples, testing::Each(true)); |
| 53 | + EXPECT_TRUE(kAbslContainerInternalSampleEverything); |
| 54 | + // One more after main() |
| 55 | + EXPECT_TRUE(HashtablezInfoHandlePeer::IsSampled(Sample())); |
| 56 | +} |
| 57 | + |
| 58 | +} // namespace |
| 59 | +} // namespace container_internal |
| 60 | +} // namespace absl |
0 commit comments