sleep_test.cc 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #include "src/core/lib/promise/sleep.h"
  15. #include <atomic>
  16. #include <gmock/gmock.h>
  17. #include <gtest/gtest.h>
  18. #include "absl/synchronization/notification.h"
  19. #include <grpc/grpc.h>
  20. #include "src/core/lib/promise/race.h"
  21. #include "src/core/lib/promise/seq.h"
  22. #include "test/core/promise/test_wakeup_schedulers.h"
  23. namespace grpc_core {
  24. namespace {
  25. TEST(Sleep, Zzzz) {
  26. ExecCtx exec_ctx;
  27. absl::Notification done;
  28. Timestamp done_time = ExecCtx::Get()->Now() + Duration::Seconds(1);
  29. // Sleep for one second then set done to true.
  30. auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(),
  31. [&done](absl::Status r) {
  32. EXPECT_EQ(r, absl::OkStatus());
  33. done.Notify();
  34. });
  35. done.WaitForNotification();
  36. exec_ctx.InvalidateNow();
  37. EXPECT_GE(ExecCtx::Get()->Now(), done_time);
  38. }
  39. TEST(Sleep, AlreadyDone) {
  40. ExecCtx exec_ctx;
  41. absl::Notification done;
  42. Timestamp done_time = ExecCtx::Get()->Now() - Duration::Seconds(1);
  43. // Sleep for no time at all then set done to true.
  44. auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(),
  45. [&done](absl::Status r) {
  46. EXPECT_EQ(r, absl::OkStatus());
  47. done.Notify();
  48. });
  49. done.WaitForNotification();
  50. }
  51. TEST(Sleep, Cancel) {
  52. ExecCtx exec_ctx;
  53. absl::Notification done;
  54. Timestamp done_time = ExecCtx::Get()->Now() + Duration::Seconds(1);
  55. // Sleep for one second but race it to complete immediately
  56. auto activity = MakeActivity(
  57. Race(Sleep(done_time), [] { return absl::CancelledError(); }),
  58. InlineWakeupScheduler(), [&done](absl::Status r) {
  59. EXPECT_EQ(r, absl::CancelledError());
  60. done.Notify();
  61. });
  62. done.WaitForNotification();
  63. exec_ctx.InvalidateNow();
  64. EXPECT_LT(ExecCtx::Get()->Now(), done_time);
  65. }
  66. } // namespace
  67. } // namespace grpc_core
  68. int main(int argc, char** argv) {
  69. ::testing::InitGoogleTest(&argc, argv);
  70. grpc_init();
  71. int r = RUN_ALL_TESTS();
  72. grpc_shutdown();
  73. return r;
  74. }