1 //
2 //
3 // Copyright 2024 gRPC authors.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 // http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 //
17 //
18
19 #include "src/core/lib/surface/connection_context.h"
20
21 #include <gtest/gtest.h>
22
23 #include "src/core/util/orphanable.h"
24 #include "test/core/test_util/test_config.h"
25
26 namespace grpc_core {
27 namespace {
28
29 class Foo {
30 public:
Foo(double value)31 explicit Foo(double value) : value_(value) {}
value() const32 double value() const { return value_; }
33
34 private:
35 double value_;
36 };
37
38 class Bar {
39 public:
Bar(int value)40 explicit Bar(int value) : value_(value) {}
value() const41 int value() const { return value_; }
42
43 private:
44 int value_;
45 };
46
47 } // namespace
48
49 template <>
50 struct ConnectionContextProperty<Foo> {};
51
52 template <>
53 struct ConnectionContextProperty<Bar> {};
54
TEST(ConnectionAuthContextTest,SimpleStaticPropertyAdditionContext)55 TEST(ConnectionAuthContextTest, SimpleStaticPropertyAdditionContext) {
56 OrphanablePtr<ConnectionContext> map = ConnectionContext::Create();
57 EXPECT_TRUE(map->EmplaceIfUnset<Foo>(3.0));
58 EXPECT_EQ(map->Get<Foo>()->value(), 3.0);
59 EXPECT_FALSE(map->EmplaceIfUnset<Foo>(1.0));
60 EXPECT_EQ(map->Get<Foo>()->value(), 3.0);
61 map->Update<Foo>(2.0);
62 EXPECT_EQ(map->Get<Foo>()->value(), 2.0);
63
64 EXPECT_TRUE(map->EmplaceIfUnset<Bar>(1));
65 EXPECT_EQ(map->Get<Bar>()->value(), 1);
66 EXPECT_FALSE(map->EmplaceIfUnset<Bar>(2));
67 EXPECT_EQ(map->Get<Bar>()->value(), 1);
68 map->Update<Bar>(1234);
69 EXPECT_EQ(map->Get<Bar>()->value(), 1234);
70 }
71
72 } // namespace grpc_core
73
main(int argc,char ** argv)74 int main(int argc, char** argv) {
75 grpc::testing::TestEnvironment env(&argc, argv);
76 ::testing::InitGoogleTest(&argc, argv);
77 return RUN_ALL_TESTS();
78 }
79