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