1 /*
2  * Copyright 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 #pragma once
18 
19 #include <android-base/chrono_utils.h>
20 #include <android-base/result.h>
21 #include <time.h>
22 #include <utils/Looper.h>
23 #include <utils/Mutex.h>
24 #include <utils/StrongPointer.h>
25 
26 #include <functional>
27 #include <vector>
28 
29 #include "LooperWrapper.h"
30 
31 namespace android {
32 namespace automotive {
33 namespace watchdog {
34 namespace testing {
35 
36 // LooperStub allows polling the underlying looper deterministically.
37 // NOTE: Current implementation only works for one handler.
38 class LooperStub final : public LooperWrapper {
39 public:
LooperStub()40     LooperStub() : mHandler(nullptr), mShouldPoll(false), mTimer(0) {}
41 
now()42     nsecs_t now() override { return mTimer.count(); }
43     // No-op when mShouldPoll is false. Otherwise, sends messages (in a non-empty CacheEntry from
44     // the front of |mCache|) to the underlying looper and polls the looper immediately. This
45     // method is called internally by the underlying looper.
46     int pollAll(int timeoutMillis) override;
47 
48     // Updates the front of |mCache| with the given message so the next pollAll call to the
49     // underlying looper will poll this message.
50     void sendMessage(const android::sp<MessageHandler>& handler, const Message& message) override;
51 
52     // Updates the seconds(uptimeDelay) position in |mCache| with the given message.
53     // Thus |uptimeDelay| should be convertible to seconds without any fractions. uptimeDelay is
54     // computed from (uptime - now).
55     void sendMessageAtTime(nsecs_t uptime, const android::sp<MessageHandler>& handler,
56                            const Message& message) override;
57 
58     // Removes all the messages from the cache and looper for |mHandler|.
59     void removeMessages(const android::sp<MessageHandler>& handler) override;
60 
61     // Removes the |what| message from the cache and looper for |mHandler|.
62     void removeMessages(const android::sp<MessageHandler>& handler, int what) override;
63 
64     // Sets |mShouldPoll| so that the subsequent |pollAll| call processes the next non-empty
65     // CacheEntry in |mCache|. Before returning, waits for the pollAll call sent to the underlying
66     // looper to complete. Thus the caller can be certain this message was processed.
67     android::base::Result<void> pollCache();
68 
69     // Number of seconds elapsed since the last pollAll call to the underlying looper.
numSecondsElapsed()70     nsecs_t numSecondsElapsed() {
71         return std::chrono::duration_cast<std::chrono::seconds>(mElapsedTime).count();
72     }
73 
74     // Increment time by a specific duration.
incrementTime(std::chrono::nanoseconds duration)75     void incrementTime(std::chrono::nanoseconds duration) { mTimer += duration; }
76 
77 private:
78     Mutex mMutex;
79     android::sp<MessageHandler> mHandler;
80     using CacheEntry = std::vector<Message>;  // Messages to process on a given second.
81     std::vector<CacheEntry> mCache;           // Messages pending to be processed.
82     bool mShouldPoll;
83     std::chrono::nanoseconds mTimer;
84     std::chrono::nanoseconds mElapsedTime;
85 };
86 
87 }  // namespace testing
88 }  // namespace watchdog
89 }  // namespace automotive
90 }  // namespace android
91