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