xref: /aosp_15_r20/external/grpc-grpc/test/core/transport/test_suite/fixture.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
1 // Copyright 2023 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_TRANSPORT_TEST_SUITE_FIXTURE_H
16 #define GRPC_TEST_CORE_TRANSPORT_TEST_SUITE_FIXTURE_H
17 
18 #include "src/core/lib/transport/transport.h"
19 #include "test/core/event_engine/fuzzing_event_engine/fuzzing_event_engine.h"
20 
21 namespace grpc_core {
22 
23 class TransportFixture {
24  public:
25   struct ClientAndServerTransportPair {
26     OrphanablePtr<Transport> client;
27     OrphanablePtr<Transport> server;
28   };
29   virtual ~TransportFixture() = default;
30   virtual ClientAndServerTransportPair CreateTransportPair(
31       std::shared_ptr<grpc_event_engine::experimental::FuzzingEventEngine>
32           event_engine) = 0;
33 };
34 
35 class TransportFixtureRegistry {
36  public:
37   static TransportFixtureRegistry& Get();
38   void RegisterFixture(absl::string_view name,
39                        absl::AnyInvocable<TransportFixture*() const> create);
40 
41   struct Fixture {
42     absl::string_view name;
43     absl::AnyInvocable<TransportFixture*() const> create;
44   };
45 
fixtures()46   const std::vector<Fixture>& fixtures() const { return fixtures_; }
47 
48  private:
49   std::vector<Fixture> fixtures_;
50 };
51 
52 }  // namespace grpc_core
53 
54 #define TRANSPORT_FIXTURE(name)                                                \
55   class TransportFixture_##name : public grpc_core::TransportFixture {         \
56    public:                                                                     \
57     using TransportFixture::TransportFixture;                                  \
58     ClientAndServerTransportPair CreateTransportPair(                          \
59         std::shared_ptr<grpc_event_engine::experimental::FuzzingEventEngine>   \
60             event_engine) override;                                            \
61                                                                                \
62    private:                                                                    \
63     static grpc_core::TransportFixture* Create() {                             \
64       return new TransportFixture_##name();                                    \
65     }                                                                          \
66     static int registered_;                                                    \
67   };                                                                           \
68   int TransportFixture_##name::registered_ =                                   \
69       (grpc_core::TransportFixtureRegistry::Get().RegisterFixture(             \
70            #name, &TransportFixture_##name::Create),                           \
71        0);                                                                     \
72   grpc_core::TransportFixture::ClientAndServerTransportPair                    \
73       TransportFixture_##name::CreateTransportPair(                            \
74           std::shared_ptr<grpc_event_engine::experimental::FuzzingEventEngine> \
75               event_engine GRPC_UNUSED)
76 
77 #endif  // GRPC_TEST_CORE_TRANSPORT_TEST_SUITE_FIXTURE_H
78