• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2024 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/filter/blackboard.h"
16 
17 #include "gmock/gmock.h"
18 #include "gtest/gtest.h"
19 
20 namespace grpc_core {
21 namespace {
22 
23 class FooEntry : public Blackboard::Entry {
24  public:
Type()25   static UniqueTypeName Type() {
26     static UniqueTypeName::Factory kFactory("FooEntry");
27     return kFactory.Create();
28   }
29 };
30 
31 class BarEntry : public Blackboard::Entry {
32  public:
Type()33   static UniqueTypeName Type() {
34     static UniqueTypeName::Factory kFactory("BarEntry");
35     return kFactory.Create();
36   }
37 };
38 
TEST(Blackboard,Basic)39 TEST(Blackboard, Basic) {
40   Blackboard blackboard;
41   // No entry for type FooEntry key "foo".
42   EXPECT_EQ(blackboard.Get<FooEntry>("a"), nullptr);
43   // Set entry for type FooEntry key "foo".
44   auto foo_entry = MakeRefCounted<FooEntry>();
45   blackboard.Set("a", foo_entry);
46   // Get the entry we just added.
47   EXPECT_EQ(blackboard.Get<FooEntry>("a"), foo_entry);
48   // A different key for the same type is still unset.
49   EXPECT_EQ(blackboard.Get<FooEntry>("b"), nullptr);
50   // The same key for a different type is still unset.
51   EXPECT_EQ(blackboard.Get<BarEntry>("a"), nullptr);
52   // Set entry for type BarEntry key "foo".
53   auto bar_entry = MakeRefCounted<BarEntry>();
54   blackboard.Set("a", bar_entry);
55   EXPECT_EQ(blackboard.Get<BarEntry>("a"), bar_entry);
56   // This should not have replaced the same key for FooEntry.
57   EXPECT_EQ(blackboard.Get<FooEntry>("a"), foo_entry);
58 }
59 
60 }  // namespace
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