• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 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_POSIX_POSIX_ENGINE_TEST_UTILS_H
16 #define GRPC_TEST_CORE_EVENT_ENGINE_POSIX_POSIX_ENGINE_TEST_UTILS_H
17 
18 #include <grpc/event_engine/event_engine.h>
19 
20 #include <utility>
21 
22 #include "absl/functional/any_invocable.h"
23 #include "src/core/lib/event_engine/posix_engine/event_poller.h"
24 
25 namespace grpc_event_engine {
26 namespace experimental {
27 
28 class TestScheduler : public Scheduler {
29  public:
TestScheduler(grpc_event_engine::experimental::EventEngine * engine)30   explicit TestScheduler(grpc_event_engine::experimental::EventEngine* engine)
31       : engine_(engine) {}
TestScheduler()32   TestScheduler() : engine_(nullptr) {};
ChangeCurrentEventEngine(grpc_event_engine::experimental::EventEngine * engine)33   void ChangeCurrentEventEngine(
34       grpc_event_engine::experimental::EventEngine* engine) {
35     engine_ = engine;
36   }
Run(experimental::EventEngine::Closure * closure)37   void Run(experimental::EventEngine::Closure* closure) override {
38     if (engine_ != nullptr) {
39       engine_->Run(closure);
40     } else {
41       closure->Run();
42     }
43   }
44 
Run(absl::AnyInvocable<void ()> cb)45   void Run(absl::AnyInvocable<void()> cb) override {
46     if (engine_ != nullptr) {
47       engine_->Run(std::move(cb));
48     } else {
49       cb();
50     }
51   }
52 
53  private:
54   grpc_event_engine::experimental::EventEngine* engine_;
55 };
56 
57 // Creates a client socket and blocks until it connects to the specified
58 // server address. The function abort fails upon encountering errors.
59 int ConnectToServerOrDie(
60     const grpc_event_engine::experimental::EventEngine::ResolvedAddress&
61         server_address);
62 
63 }  // namespace experimental
64 }  // namespace grpc_event_engine
65 
66 #endif  // GRPC_TEST_CORE_EVENT_ENGINE_POSIX_POSIX_ENGINE_TEST_UTILS_H
67