sleep_test.cc
Go to the documentation of this file.
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 
16 
17 #include <type_traits>
18 
19 #include "absl/synchronization/notification.h"
20 #include "gtest/gtest.h"
21 
22 #include <grpc/grpc.h>
23 
27 
28 namespace grpc_core {
29 namespace {
30 
31 TEST(Sleep, Zzzz) {
32  ExecCtx exec_ctx;
34  Timestamp done_time = ExecCtx::Get()->Now() + Duration::Seconds(1);
35  // Sleep for one second then set done to true.
36  auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(),
37  [&done](absl::Status r) {
39  done.Notify();
40  });
41  done.WaitForNotification();
43  EXPECT_GE(ExecCtx::Get()->Now(), done_time);
44 }
45 
46 TEST(Sleep, AlreadyDone) {
47  ExecCtx exec_ctx;
49  Timestamp done_time = ExecCtx::Get()->Now() - Duration::Seconds(1);
50  // Sleep for no time at all then set done to true.
51  auto activity = MakeActivity(Sleep(done_time), InlineWakeupScheduler(),
52  [&done](absl::Status r) {
54  done.Notify();
55  });
56  done.WaitForNotification();
57 }
58 
59 TEST(Sleep, Cancel) {
60  ExecCtx exec_ctx;
62  Timestamp done_time = ExecCtx::Get()->Now() + Duration::Seconds(1);
63  // Sleep for one second but race it to complete immediately
64  auto activity = MakeActivity(
65  Race(Sleep(done_time), [] { return absl::CancelledError(); }),
66  InlineWakeupScheduler(), [&done](absl::Status r) {
68  done.Notify();
69  });
70  done.WaitForNotification();
72  EXPECT_LT(ExecCtx::Get()->Now(), done_time);
73 }
74 
75 TEST(Sleep, MoveSemantics) {
76  // ASAN should help determine if there are any memory leaks here
77  ExecCtx exec_ctx;
79  Timestamp done_time = ExecCtx::Get()->Now() + Duration::Milliseconds(111);
80  Sleep donor(done_time);
81  Sleep sleeper = std::move(donor);
82  auto activity = MakeActivity(std::move(sleeper), InlineWakeupScheduler(),
83  [&done](absl::Status r) {
85  done.Notify();
86  });
87  done.WaitForNotification();
89  EXPECT_GE(ExecCtx::Get()->Now(), done_time);
90 }
91 
92 } // namespace
93 } // namespace grpc_core
94 
95 int main(int argc, char** argv) {
96  ::testing::InitGoogleTest(&argc, argv);
97  grpc_init();
98  int r = RUN_ALL_TESTS();
99  grpc_shutdown();
100  return r;
101 }
main
int main(int argc, char **argv)
Definition: sleep_test.cc:95
Timestamp
Definition: bloaty/third_party/protobuf/src/google/protobuf/timestamp.pb.h:69
grpc_core
Definition: call_metric_recorder.h:31
absl::CancelledError
Status CancelledError(absl::string_view message)
Definition: third_party/abseil-cpp/absl/status/status.cc:331
absl::OkStatus
Status OkStatus()
Definition: third_party/abseil-cpp/absl/status/status.h:882
grpc_core::TEST
TEST(AvlTest, NoOp)
Definition: avl_test.cc:21
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
grpc_core::MakeActivity
ActivityPtr MakeActivity(Factory promise_factory, WakeupScheduler wakeup_scheduler, OnDone on_done, Contexts &&... contexts)
Definition: activity.h:522
absl::Notification
Definition: abseil-cpp/absl/synchronization/notification.h:66
grpc_core::Race
promise_detail::Race< Promises... > Race(Promises... promises)
Definition: race.h:77
sleep.h
absl::move
constexpr absl::remove_reference_t< T > && move(T &&t) noexcept
Definition: abseil-cpp/absl/utility/utility.h:221
grpc.h
done
struct tab * done
Definition: bloaty/third_party/zlib/examples/enough.c:176
RUN_ALL_TESTS
int RUN_ALL_TESTS() GTEST_MUST_USE_RESULT_
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2471
test_wakeup_schedulers.h
race.h
grpc_core::Duration::Milliseconds
static constexpr Duration Milliseconds(int64_t millis)
Definition: src/core/lib/gprpp/time.h:155
absl::Now
ABSL_NAMESPACE_BEGIN Time Now()
Definition: abseil-cpp/absl/time/clock.cc:39
testing::InitGoogleTest
GTEST_API_ void InitGoogleTest(int *argc, char **argv)
Definition: bloaty/third_party/googletest/googletest/src/gtest.cc:6106
exec_ctx
grpc_core::ExecCtx exec_ctx
Definition: end2end_binder_transport_test.cc:75
absl::Status
Definition: third_party/abseil-cpp/absl/status/status.h:424
fix_build_deps.r
r
Definition: fix_build_deps.py:491
EXPECT_LT
#define EXPECT_LT(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2032
grpc_core::Duration::Seconds
static constexpr Duration Seconds(int64_t seconds)
Definition: src/core/lib/gprpp/time.h:151
exec_ctx.h
EXPECT_GE
#define EXPECT_GE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2034
grpc_core::ExecCtx::Now
Timestamp Now()
Definition: exec_ctx.cc:90
grpc_init
GRPCAPI void grpc_init(void)
Definition: init.cc:146
grpc_shutdown
GRPCAPI void grpc_shutdown(void)
Definition: init.cc:209
grpc_core::ExecCtx::Get
static ExecCtx * Get()
Definition: exec_ctx.h:205
grpc_core::ExecCtx::InvalidateNow
void InvalidateNow()
Definition: exec_ctx.h:188


grpc
Author(s):
autogenerated on Fri May 16 2025 03:00:13