| 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 |
void ScheduleWakeup(ActivityType*) { |
| 31 |
abort(); |
| 32 |
} |
| 33 |
}; |
| 34 |
|
| 35 |
// A wakeup scheduler that simply runs the callback immediately. |
| 36 |
// Useful for unit testing, probably not so much for real systems due to lock |
| 37 |
// ordering problems. |
| 38 |
struct InlineWakeupScheduler { |
| 39 |
template <typename ActivityType> |
| 40 |
void ScheduleWakeup(ActivityType* activity) { |
| 41 |
activity->RunScheduledWakeup(); |
| 42 |
} |
| 43 |
}; |
| 44 |
|
| 45 |
// Mock for something that can schedule callbacks. |
| 46 |
class MockCallbackScheduler { |
| 47 |
public: |
| 48 |
MOCK_METHOD(void, Schedule, (std::function<void()>)); |
| 49 |
}; |
| 50 |
|
| 51 |
// WakeupScheduler that schedules wakeups against a MockCallbackScheduler. |
| 52 |
// Usage: |
| 53 |
// TEST(..., ...) { |
| 54 |
// MockCallbackScheduler scheduler; |
| 55 |
// auto activity = MakeActivity(..., |
| 56 |
// UseMockCallbackScheduler(&scheduler), |
| 57 |
// ...); |
| 58 |
struct UseMockCallbackScheduler { |
| 59 |
MockCallbackScheduler* scheduler; |
| 60 |
template <typename ActivityType> |
| 61 |
void ScheduleWakeup(ActivityType* activity) { |
| 62 |
scheduler->Schedule([activity] { activity->RunScheduledWakeup(); }); |
| 63 |
} |
| 64 |
}; |
| 65 |
|
| 66 |
} // namespace grpc_core |
| 67 |
|
| 68 |
#endif |