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