1 /*
2 * Copyright (C) 2020 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <gmock/gmock.h>
18 #include <gtest/gtest.h>
19
20 #include <vibratorservice/VibratorCallbackScheduler.h>
21
22 #include "test_utils.h"
23
24 using std::chrono::milliseconds;
25 using std::chrono::steady_clock;
26 using std::chrono::time_point;
27
28 using namespace android;
29 using namespace std::chrono_literals;
30 using namespace testing;
31
32 // -------------------------------------------------------------------------------------------------
33
34 // Delay allowed for the scheduler to process callbacks during this test.
35 static const auto TEST_TIMEOUT = 100ms;
36
37 class VibratorCallbackSchedulerTest : public Test {
38 public:
SetUp()39 void SetUp() override { mScheduler = std::make_unique<vibrator::CallbackScheduler>(); }
40
41 protected:
42 std::mutex mMutex;
43 std::unique_ptr<vibrator::CallbackScheduler> mScheduler = nullptr;
44 vibrator::TestCounter mCallbackCounter;
45 std::vector<int32_t> mExpiredCallbacks GUARDED_BY(mMutex);
46
createCallback(int32_t id)47 std::function<void()> createCallback(int32_t id) {
48 return [this, id]() {
49 {
50 std::lock_guard<std::mutex> lock(mMutex);
51 mExpiredCallbacks.push_back(id);
52 }
53 mCallbackCounter.increment();
54 };
55 }
56
getExpiredCallbacks()57 std::vector<int32_t> getExpiredCallbacks() {
58 std::lock_guard<std::mutex> lock(mMutex);
59 return std::vector<int32_t>(mExpiredCallbacks);
60 }
61
waitForCallbacks(int32_t callbackCount,milliseconds timeout)62 int32_t waitForCallbacks(int32_t callbackCount, milliseconds timeout) {
63 mCallbackCounter.tryWaitUntilCountIsAtLeast(callbackCount, timeout);
64 return mCallbackCounter.get();
65 }
66 };
67
68 // -------------------------------------------------------------------------------------------------
69
TEST_F(VibratorCallbackSchedulerTest,TestScheduleRunsOnlyAfterDelay)70 TEST_F(VibratorCallbackSchedulerTest, TestScheduleRunsOnlyAfterDelay) {
71 auto callbackDuration = 50ms;
72 time_point<steady_clock> startTime = steady_clock::now();
73 mScheduler->schedule(createCallback(1), callbackDuration);
74
75 ASSERT_THAT(waitForCallbacks(1, callbackDuration + TEST_TIMEOUT), Eq(1));
76 time_point<steady_clock> callbackTime = steady_clock::now();
77
78 // Callback took at least the required duration to trigger.
79 ASSERT_THAT(callbackTime, Ge(startTime + callbackDuration));
80 }
81
TEST_F(VibratorCallbackSchedulerTest,TestScheduleMultipleCallbacksRunsInDelayOrder)82 TEST_F(VibratorCallbackSchedulerTest, TestScheduleMultipleCallbacksRunsInDelayOrder) {
83 // Schedule first callbacks long enough that all 3 will be scheduled together and run in order.
84 mScheduler->schedule(createCallback(1), 50ms + 2 * TEST_TIMEOUT);
85 mScheduler->schedule(createCallback(2), 50ms + TEST_TIMEOUT);
86 mScheduler->schedule(createCallback(3), 50ms);
87
88 // Callbacks triggered in the expected order based on the requested durations.
89 ASSERT_THAT(waitForCallbacks(3, 50ms + 3 * TEST_TIMEOUT), Eq(3));
90 ASSERT_THAT(getExpiredCallbacks(), ElementsAre(3, 2, 1));
91 }
92
TEST_F(VibratorCallbackSchedulerTest,TestDestructorDropsPendingCallbacksAndKillsThread)93 TEST_F(VibratorCallbackSchedulerTest, TestDestructorDropsPendingCallbacksAndKillsThread) {
94 // Schedule callback long enough that scheduler will be destroyed while it's still scheduled.
95 mScheduler->schedule(createCallback(1), 100ms);
96 mScheduler.reset(nullptr);
97
98 // Should timeout waiting for callback to run.
99 ASSERT_THAT(waitForCallbacks(1, 100ms + TEST_TIMEOUT), Eq(0));
100 ASSERT_THAT(getExpiredCallbacks(), IsEmpty());
101 }
102