| 1 |
// Copyright 2022 gRPC 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 "src/core/lib/gprpp/single_set_ptr.h" |
| 16 |
|
| 17 |
#include <thread> |
| 18 |
#include <vector> |
| 19 |
|
| 20 |
#include "gtest/gtest.h" |
| 21 |
|
| 22 |
namespace grpc_core { |
| 23 |
namespace testing { |
| 24 |
|
| 25 |
TEST(SingleSetPtrTest, NoOp) { SingleSetPtr<int>(); } |
| 26 |
|
| 27 |
TEST(SingleSetPtrTest, CanSet) { |
| 28 |
SingleSetPtr<int> p; |
| 29 |
EXPECT_FALSE(p.is_set()); |
| 30 |
EXPECT_DEATH_IF_SUPPORTED(gpr_log(GPR_ERROR, "%d", *p), ""); |
| 31 |
p.Set(new int(42)); |
| 32 |
EXPECT_EQ(*p, 42); |
| 33 |
} |
| 34 |
|
| 35 |
TEST(SingleSetPtrTest, CanReset) { |
| 36 |
SingleSetPtr<int> p; |
| 37 |
EXPECT_FALSE(p.is_set()); |
| 38 |
p.Set(new int(42)); |
| 39 |
EXPECT_TRUE(p.is_set()); |
| 40 |
p.Set(new int(43)); |
| 41 |
EXPECT_EQ(*p, 42); |
| 42 |
p.Reset(); |
| 43 |
EXPECT_FALSE(p.is_set()); |
| 44 |
} |
| 45 |
|
| 46 |
TEST(SingleSetPtrTest, LotsOfSetters) { |
| 47 |
SingleSetPtr<int> p; |
| 48 |
std::vector<std::thread> threads; |
| 49 |
threads.reserve(10); |
| 50 |
for (int i = 0; i < 10; i++) { |
| 51 |
threads.emplace_back([&p, i]() { p.Set(new int(i)); }); |
| 52 |
} |
| 53 |
for (auto& t : threads) { |
| 54 |
t.join(); |
| 55 |
} |
| 56 |
} |
| 57 |
|
| 58 |
} // namespace testing |
| 59 |
} // namespace grpc_core |
| 60 |
|
| 61 |
int main(int argc, char** argv) { |
| 62 |
::testing::InitGoogleTest(&argc, argv); |
| 63 |
return RUN_ALL_TESTS(); |
| 64 |
} |