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 #ifndef WATCHDOG_SERVER_TESTS_LOOPERSTUB_H_
18 #define WATCHDOG_SERVER_TESTS_LOOPERSTUB_H_
19 
20 #include <android-base/chrono_utils.h>
21 #include <android-base/result.h>
22 #include <time.h>
23 #include <utils/Looper.h>
24 #include <utils/Mutex.h>
25 #include <utils/StrongPointer.h>
26 
27 #include <functional>
28 #include <vector>
29 
30 #include "LooperWrapper.h"
31 
32 namespace android {
33 namespace automotive {
34 namespace watchdog {
35 namespace testing {
36 
37 // LooperStub allows polling the underlying looper deterministically.
38 // NOTE: Current implementation only works for one handler.
39 class LooperStub : public LooperWrapper {
40 public:
LooperStub()41     LooperStub() : mHandler(nullptr), mShouldPoll(false), mTimer(0) {}
42 
now()43     nsecs_t now() override { return mTimer.count(); }
44     // No-op when mShouldPoll is false. Otherwise, sends messages (in a non-empty CacheEntry from
45     // the front of |mCache|) to the underlying looper and polls the looper immediately.
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     // Sets |mShouldPoll| so that the subsequent |pollAll| call processes the next non-empty
62     // CacheEntry in |mCache|. Before returning, waits for the pollAll call sent to the underlying
63     // looper to complete. Thus the caller can be certain this message was processed.
64     android::base::Result<void> pollCache();
65 
66     // Number of seconds elapsed since the last pollAll call to the underlying looper.
numSecondsElapsed()67     nsecs_t numSecondsElapsed() {
68         return std::chrono::duration_cast<std::chrono::seconds>(mElapsedTime).count();
69     }
70 
71 private:
72     Mutex mMutex;
73     android::sp<MessageHandler> mHandler;
74     using CacheEntry = std::vector<Message>;  // Messages to process on a given second.
75     std::vector<CacheEntry> mCache;           // Messages pending to be processed.
76     bool mShouldPoll;
77     std::chrono::nanoseconds mTimer;
78     std::chrono::nanoseconds mElapsedTime;
79 };
80 
81 }  // namespace testing
82 }  // namespace watchdog
83 }  // namespace automotive
84 }  // namespace android
85 
86 #endif  // WATCHDOG_SERVER_TESTS_LOOPERSTUB_H_
87