xref: /aosp_15_r20/external/grpc-grpc/test/core/promise/test_wakeup_schedulers.h (revision cc02d7e222339f7a4f6ba5f422e6413f4bd931f2)
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_PROMISE_TEST_WAKEUP_SCHEDULERS_H
16 #define GRPC_TEST_CORE_PROMISE_TEST_WAKEUP_SCHEDULERS_H
17 
18 #include <stdlib.h>
19 
20 #include <functional>
21 
22 #include "gmock/gmock.h"
23 
24 namespace grpc_core {
25 
26 // A wakeup scheduler that simply crashes.
27 // Useful for very limited tests.
28 struct NoWakeupScheduler {
29   template <typename ActivityType>
30   class BoundScheduler {
31    public:
BoundSchedulerNoWakeupScheduler32     explicit BoundScheduler(NoWakeupScheduler) {}
ScheduleWakeupNoWakeupScheduler33     void ScheduleWakeup() { abort(); }
34   };
35 };
36 
37 // A wakeup scheduler that simply runs the callback immediately.
38 // Useful for unit testing, probably not so much for real systems due to lock
39 // ordering problems.
40 struct InlineWakeupScheduler {
41   template <typename ActivityType>
42   class BoundScheduler {
43    public:
BoundSchedulerInlineWakeupScheduler44     explicit BoundScheduler(InlineWakeupScheduler) {}
ScheduleWakeupInlineWakeupScheduler45     void ScheduleWakeup() {
46       static_cast<ActivityType*>(this)->RunScheduledWakeup();
47     }
48   };
49 };
50 
51 // Mock for something that can schedule callbacks.
52 class MockCallbackScheduler {
53  public:
54   MOCK_METHOD(void, Schedule, (std::function<void()>));
55 };
56 
57 // WakeupScheduler that schedules wakeups against a MockCallbackScheduler.
58 // Usage:
59 // TEST(..., ...) {
60 //   MockCallbackScheduler scheduler;
61 //   auto activity = MakeActivity(...,
62 //                                UseMockCallbackScheduler(&scheduler),
63 //                                ...);
64 struct UseMockCallbackScheduler {
65   MockCallbackScheduler* scheduler;
66   template <typename ActivityType>
67   class BoundScheduler {
68    public:
BoundSchedulerUseMockCallbackScheduler69     explicit BoundScheduler(UseMockCallbackScheduler use_scheduler)
70         : scheduler(use_scheduler.scheduler) {}
ScheduleWakeupUseMockCallbackScheduler71     void ScheduleWakeup() {
72       scheduler->Schedule(
73           [this] { static_cast<ActivityType*>(this)->RunScheduledWakeup(); });
74     }
75 
76    private:
77     MockCallbackScheduler* scheduler;
78   };
79 };
80 
81 }  // namespace grpc_core
82 
83 #endif  // GRPC_TEST_CORE_PROMISE_TEST_WAKEUP_SCHEDULERS_H
84