/Users/deen/code/yugabyte-db/src/yb/util/background_task-test.cc
Line | Count | Source |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include <chrono> |
34 | | #include <thread> |
35 | | |
36 | | #include <gtest/gtest.h> |
37 | | |
38 | | #include "yb/util/background_task.h" |
39 | | #include "yb/util/result.h" |
40 | | #include "yb/util/test_macros.h" |
41 | | #include "yb/util/test_util.h" |
42 | | #include "yb/util/tsan_util.h" |
43 | | |
44 | | using namespace std::chrono_literals; |
45 | | |
46 | | namespace yb { |
47 | | |
48 | | class BackgroundTaskTest : public YBTest { |
49 | | public: |
50 | 5 | BackgroundTaskTest() : YBTest(), run_count_(0) {} |
51 | | |
52 | 5 | std::unique_ptr<BackgroundTask> GetTask(std::chrono::milliseconds timeout = 0s) { |
53 | 2.04k | auto task = std::make_unique<BackgroundTask>([this]() { |
54 | 2.04k | run_count_.fetch_add(1); |
55 | 2.04k | }, "test", "test", timeout); |
56 | 5 | EXPECT_OK(task->Init()); |
57 | 5 | return task; |
58 | 5 | } |
59 | | |
60 | | protected: |
61 | | std::atomic<int> run_count_; |
62 | | }; |
63 | | |
64 | 1 | TEST_F(BackgroundTaskTest, RunsTaskOnWake) { |
65 | 1 | constexpr int kNumTaskRuns = 1000; |
66 | 1 | auto bg_task = GetTask(); |
67 | | |
68 | 1.00k | for (int i = 1; i < kNumTaskRuns; ++i) { |
69 | 999 | EXPECT_OK(bg_task->Wake()); |
70 | 999 | EXPECT_OK(WaitFor([this, i]() { |
71 | 999 | return run_count_ == i; |
72 | 999 | }, 1s * kTimeMultiplier, Format("Wait for i-th ($0) task run.", i))); |
73 | 999 | } |
74 | | |
75 | 1 | bg_task->Shutdown(); |
76 | 1 | } |
77 | | |
78 | 1 | TEST_F(BackgroundTaskTest, RunsTaskOnInterval) { |
79 | 1 | constexpr int kNumTaskRuns = 1000; |
80 | | |
81 | 1 | auto interval = 10ms * kTimeMultiplier; |
82 | 1 | auto bg_task = GetTask(interval); |
83 | | |
84 | 1.00k | for (int i = 1; i < kNumTaskRuns; ++i) { |
85 | 999 | std::this_thread::sleep_for(interval); |
86 | 999 | EXPECT_OK(WaitFor([this, i]() { |
87 | 999 | return run_count_ >= i; |
88 | 999 | }, interval / 10, Format("Wait for i-th ($0) task run.", i))); |
89 | 999 | } |
90 | | |
91 | 1 | bg_task->Shutdown(); |
92 | 1 | } |
93 | | |
94 | | class BackgroundTaskShutdownTest : |
95 | | public BackgroundTaskTest, public ::testing::WithParamInterface<std::chrono::milliseconds> {}; |
96 | | |
97 | 3 | TEST_P(BackgroundTaskShutdownTest, InitAndShutdown) { |
98 | 3 | auto interval = GetParam() * kTimeMultiplier; |
99 | 3 | auto bg_task = GetTask(interval); |
100 | 3 | std::this_thread::sleep_for(1s * kTimeMultiplier); |
101 | 3 | bg_task->Shutdown(); |
102 | 3 | } |
103 | | |
104 | | INSTANTIATE_TEST_CASE_P( |
105 | | InitAndShutdown, BackgroundTaskShutdownTest, ::testing::Values(0s, 20ms, 5s)); |
106 | | |
107 | | } // namespace yb |