1 // Copyright 2021 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 #ifndef GRPC_TEST_CORE_EVENT_ENGINE_TEST_SUITE_EVENT_ENGINE_TEST_FRAMEWORK_H 16 #define GRPC_TEST_CORE_EVENT_ENGINE_TEST_SUITE_EVENT_ENGINE_TEST_FRAMEWORK_H 17 #include <memory> 18 #include <utility> 19 20 #include <gtest/gtest.h> 21 22 #include "absl/functional/any_invocable.h" 23 24 #include <grpc/event_engine/event_engine.h> 25 #include <grpc/support/log.h> 26 27 extern absl::AnyInvocable< 28 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()>* 29 g_ee_factory; 30 31 extern absl::AnyInvocable< 32 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()>* 33 g_oracle_ee_factory; 34 35 // Manages the lifetime of the global EventEngine factory. 36 class EventEngineTestEnvironment : public testing::Environment { 37 public: EventEngineTestEnvironment(absl::AnyInvocable<std::shared_ptr<grpc_event_engine::experimental::EventEngine> ()> factory,absl::AnyInvocable<std::shared_ptr<grpc_event_engine::experimental::EventEngine> ()> oracle_factory)38 EventEngineTestEnvironment( 39 absl::AnyInvocable< 40 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()> 41 factory, 42 absl::AnyInvocable< 43 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()> 44 oracle_factory) 45 : factory_(std::move(factory)), 46 oracle_factory_(std::move(oracle_factory)) {} 47 SetUp()48 void SetUp() override { 49 g_ee_factory = &factory_; 50 g_oracle_ee_factory = &oracle_factory_; 51 } 52 TearDown()53 void TearDown() override { 54 g_ee_factory = nullptr; 55 g_oracle_ee_factory = nullptr; 56 } 57 58 private: 59 absl::AnyInvocable< 60 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()> 61 factory_; 62 absl::AnyInvocable< 63 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()> 64 oracle_factory_; 65 }; 66 67 class EventEngineTest : public testing::Test { 68 protected: 69 std::shared_ptr<grpc_event_engine::experimental::EventEngine> NewEventEngine()70 NewEventEngine() { 71 GPR_ASSERT(g_ee_factory != nullptr); 72 return (*g_ee_factory)(); 73 } 74 75 std::shared_ptr<grpc_event_engine::experimental::EventEngine> NewOracleEventEngine()76 NewOracleEventEngine() { 77 GPR_ASSERT(g_oracle_ee_factory != nullptr); 78 return (*g_oracle_ee_factory)(); 79 } 80 }; 81 82 // Set a custom factory for the EventEngine test suite. An optional oracle 83 // EventEngine can additionally be specified here. 84 void SetEventEngineFactories( 85 absl::AnyInvocable< 86 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()> 87 ee_factory, 88 absl::AnyInvocable< 89 std::shared_ptr<grpc_event_engine::experimental::EventEngine>()> 90 oracle_ee_factory); 91 92 #endif // GRPC_TEST_CORE_EVENT_ENGINE_TEST_SUITE_EVENT_ENGINE_TEST_FRAMEWORK_H 93