test_wakeup_schedulers.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. #ifndef GRPC_TEST_CORE_PROMISE_TEST_WAKEUP_SCHEDULERS_H
  15. #define GRPC_TEST_CORE_PROMISE_TEST_WAKEUP_SCHEDULERS_H
  16. #include <gmock/gmock.h>
  17. namespace grpc_core {
  18. // A wakeup scheduler that simply crashes.
  19. // Useful for very limited tests.
  20. struct NoWakeupScheduler {
  21. template <typename ActivityType>
  22. void ScheduleWakeup(ActivityType*) {
  23. abort();
  24. }
  25. };
  26. // A wakeup scheduler that simply runs the callback immediately.
  27. // Useful for unit testing, probably not so much for real systems due to lock
  28. // ordering problems.
  29. struct InlineWakeupScheduler {
  30. template <typename ActivityType>
  31. void ScheduleWakeup(ActivityType* activity) {
  32. activity->RunScheduledWakeup();
  33. }
  34. };
  35. // Mock for something that can schedule callbacks.
  36. class MockCallbackScheduler {
  37. public:
  38. MOCK_METHOD(void, Schedule, (std::function<void()>));
  39. };
  40. // WakeupScheduler that schedules wakeups against a MockCallbackScheduler.
  41. // Usage:
  42. // TEST(..., ...) {
  43. // MockCallbackScheduler scheduler;
  44. // auto activity = MakeActivity(...,
  45. // UseMockCallbackScheduler(&scheduler),
  46. // ...);
  47. struct UseMockCallbackScheduler {
  48. MockCallbackScheduler* scheduler;
  49. template <typename ActivityType>
  50. void ScheduleWakeup(ActivityType* activity) {
  51. scheduler->Schedule([activity] { activity->RunScheduledWakeup(); });
  52. }
  53. };
  54. } // namespace grpc_core
  55. #endif