Debian Codegraph @

test/core/event_engine/posix/posix_engine_test_utils.h

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