Debian Codegraph @

test/core/event_engine/factory_test.cc

raw
1 // Copyright 2022 The 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 <grpc/support/port_platform.h>
16
17 #include <memory>
18
19 #include "gtest/gtest.h"
20
21 #include <grpc/event_engine/event_engine.h>
22 #include <grpc/grpc.h>
23
24 #include "src/core/lib/event_engine/default_event_engine.h"
25 #include "test/core/event_engine/util/aborting_event_engine.h"
26 #include "test/core/util/test_config.h"
27
28 namespace {
29 using ::grpc_event_engine::experimental::AbortingEventEngine;
30 using ::grpc_event_engine::experimental::EventEngine;
31 using ::grpc_event_engine::experimental::EventEngineFactoryReset;
32 using ::grpc_event_engine::experimental::GetDefaultEventEngine;
33 using ::grpc_event_engine::experimental::SetEventEngineFactory;
34
35 class EventEngineFactoryTest : public testing::Test {
36 public:
37 EventEngineFactoryTest() = default;
38 ~EventEngineFactoryTest() override { EventEngineFactoryReset(); }
39 };
40
41 TEST_F(EventEngineFactoryTest, CustomFactoryIsUsed) {
42 int counter{0};
43 SetEventEngineFactory([&counter] {
44 ++counter;
45 return std::make_unique<AbortingEventEngine>();
46 });
47 auto ee1 = GetDefaultEventEngine();
48 ASSERT_EQ(counter, 1);
49 auto ee2 = GetDefaultEventEngine();
50 ASSERT_EQ(counter, 1);
51 ASSERT_EQ(ee1, ee2);
52 }
53
54 TEST_F(EventEngineFactoryTest, FactoryResetWorks) {
55 int counter{0};
56 SetEventEngineFactory([&counter]() -> std::unique_ptr<EventEngine> {
57 // this factory should only be used twice;
58 EXPECT_LE(++counter, 2);
59 return std::make_unique<AbortingEventEngine>();
60 });
61 auto custom_ee = GetDefaultEventEngine();
62 ASSERT_EQ(counter, 1);
63 auto same_ee = GetDefaultEventEngine();
64 ASSERT_EQ(custom_ee, same_ee);
65 ASSERT_EQ(counter, 1);
66 EventEngineFactoryReset();
67 auto default_ee = GetDefaultEventEngine();
68 ASSERT_NE(custom_ee, default_ee);
69 }
70 } // namespace
71
72 int main(int argc, char** argv) {
73 testing::InitGoogleTest(&argc, argv);
74 grpc::testing::TestEnvironment env(&argc, argv);
75 grpc_init();
76 auto result = RUN_ALL_TESTS();
77 grpc_shutdown();
78 return result;
79 }