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 CPP_WATCHDOG_SERVER_TESTS_LOOPERSTUB_H_ 18 #define CPP_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 // 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 private: 75 Mutex mMutex; 76 android::sp<MessageHandler> mHandler; 77 using CacheEntry = std::vector<Message>; // Messages to process on a given second. 78 std::vector<CacheEntry> mCache; // Messages pending to be processed. 79 bool mShouldPoll; 80 std::chrono::nanoseconds mTimer; 81 std::chrono::nanoseconds mElapsedTime; 82 }; 83 84 } // namespace testing 85 } // namespace watchdog 86 } // namespace automotive 87 } // namespace android 88 89 #endif // CPP_WATCHDOG_SERVER_TESTS_LOOPERSTUB_H_ 90