diff --git a/health/storage/1.0/vts/functional/Android.bp b/health/storage/1.0/vts/functional/Android.bp index 22010319a2..731ad62434 100644 --- a/health/storage/1.0/vts/functional/Android.bp +++ b/health/storage/1.0/vts/functional/Android.bp @@ -19,6 +19,9 @@ cc_test { defaults: ["VtsHalTargetTestDefaults"], srcs: ["VtsHalHealthStorageV1_0TargetTest.cpp"], static_libs: ["android.hardware.health.storage@1.0"], + header_libs: [ + "libhealth_storage_test_common_headers", + ], shared_libs: [ "libhidlbase", ], diff --git a/health/storage/1.0/vts/functional/VtsHalHealthStorageV1_0TargetTest.cpp b/health/storage/1.0/vts/functional/VtsHalHealthStorageV1_0TargetTest.cpp index 24ddc5d814..ddb6b5a1fd 100644 --- a/health/storage/1.0/vts/functional/VtsHalHealthStorageV1_0TargetTest.cpp +++ b/health/storage/1.0/vts/functional/VtsHalHealthStorageV1_0TargetTest.cpp @@ -14,14 +14,17 @@ * limitations under the License. */ +#include + +#include + #include #include #include +#include #include #include #include -#include -#include namespace android { namespace hardware { @@ -29,61 +32,17 @@ namespace health { namespace storage { namespace V1_0 { +using namespace ::android::hardware::health::storage::test; using ::std::literals::chrono_literals::operator""ms; #define ASSERT_OK(ret) ASSERT_TRUE(ret.isOk()) << ret.description() -// Dev GC timeout. This is the timeout used by vold. -const uint64_t kDevGcTimeoutSec = 120; -const std::chrono::seconds kDevGcTimeout{kDevGcTimeoutSec}; -// Dev GC timeout tolerance. The HAL may not immediately return after the -// timeout, so include an acceptable tolerance. -const std::chrono::seconds kDevGcTolerance{3}; -// Time accounted for RPC calls. -const std::chrono::milliseconds kRpcTime{1000}; - -template -std::string toString(std::chrono::duration time) { - return std::to_string(time.count()) + "ms"; -} - -/** An atomic boolean flag that indicates whether a task has finished. */ -class Flag { - public: - void onFinish() { - std::unique_lock lock(mMutex); - onFinishLocked(&lock); - } - template - bool wait(std::chrono::duration duration) { - std::unique_lock lock(mMutex); - return waitLocked(&lock, duration); - } - - protected: - /** Will unlock. */ - void onFinishLocked(std::unique_lock* lock) { - mFinished = true; - lock->unlock(); - mCv.notify_all(); - } - template - bool waitLocked(std::unique_lock* lock, std::chrono::duration duration) { - mCv.wait_for(*lock, duration, [this] { return mFinished; }); - return mFinished; - } - - bool mFinished{false}; - std::mutex mMutex; - std::condition_variable mCv; -}; - class GcCallback : public IGarbageCollectCallback, public Flag { - public: + public: Return onFinish(Result result) override { - std::unique_lock lock(mMutex); - mResult = result; - Flag::onFinishLocked(&lock); + std::unique_lock lock(mutex_); + result_ = result; + Flag::OnFinishLocked(&lock); return Void(); } @@ -93,13 +52,13 @@ class GcCallback : public IGarbageCollectCallback, public Flag { */ template void waitForResult(std::chrono::duration timeout, Result expected) { - std::unique_lock lock(mMutex); - ASSERT_TRUE(waitLocked(&lock, timeout)) << "timeout after " << toString(timeout); - EXPECT_EQ(expected, mResult); + std::unique_lock lock(mutex_); + ASSERT_TRUE(WaitLocked(&lock, timeout)) << "timeout after " << to_string(timeout); + EXPECT_EQ(expected, result_); } - private: - Result mResult{Result::UNKNOWN_ERROR}; + private: + Result result_{Result::UNKNOWN_ERROR}; }; class HealthStorageHidlTest : public ::testing::TestWithParam { @@ -127,10 +86,10 @@ class HealthStorageHidlTest : public ::testing::TestWithParam { auto pingFlag = std::make_shared(); std::thread([service, pingFlag] { service->ping(); - pingFlag->onFinish(); + pingFlag->OnFinish(); }) .detach(); - return pingFlag->wait(timeout); + return pingFlag->Wait(timeout); } sp fs; @@ -147,7 +106,7 @@ TEST_P(HealthStorageHidlTest, GcNullCallback) { // Hold test process because HAL can be single-threaded and doing GC. ASSERT_TRUE(ping(kDevGcTimeout + kDevGcTolerance + kRpcTime)) << "Service must be available after " - << toString(kDevGcTimeout + kDevGcTolerance + kRpcTime); + << to_string(kDevGcTimeout + kDevGcTolerance + kRpcTime); } /** diff --git a/health/storage/test_common/Android.bp b/health/storage/test_common/Android.bp new file mode 100644 index 0000000000..7c6bef414c --- /dev/null +++ b/health/storage/test_common/Android.bp @@ -0,0 +1,20 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +cc_library_headers { + name: "libhealth_storage_test_common_headers", + export_include_dirs: ["include"], +} diff --git a/health/storage/test_common/include/health-storage-test/common.h b/health/storage/test_common/include/health-storage-test/common.h new file mode 100644 index 0000000000..dfda83051d --- /dev/null +++ b/health/storage/test_common/include/health-storage-test/common.h @@ -0,0 +1,69 @@ +/* + * Copyright (C) 2021 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include +#include + +namespace android::hardware::health::storage::test { + +// Dev GC timeout. This is the timeout used by vold. +const uint64_t kDevGcTimeoutSec = 120; +const std::chrono::seconds kDevGcTimeout{kDevGcTimeoutSec}; +// Dev GC timeout tolerance. The HAL may not immediately return after the +// timeout, so include an acceptable tolerance. +const std::chrono::seconds kDevGcTolerance{3}; +// Time accounted for RPC calls. +const std::chrono::milliseconds kRpcTime{1000}; + +template +std::string to_string(std::chrono::duration time) { + return std::to_string(time.count()) + "ms"; +} + +/** An atomic boolean flag that indicates whether a task has finished. */ +class Flag { + public: + void OnFinish() { + std::unique_lock lock(mutex_); + OnFinishLocked(&lock); + } + template + bool Wait(std::chrono::duration duration) { + std::unique_lock lock(mutex_); + return WaitLocked(&lock, duration); + } + + protected: + /** Will unlock. */ + void OnFinishLocked(std::unique_lock* lock) { + finished_ = true; + lock->unlock(); + cv_.notify_all(); + } + template + bool WaitLocked(std::unique_lock* lock, std::chrono::duration duration) { + cv_.wait_for(*lock, duration, [this] { return finished_; }); + return finished_; + } + + bool finished_{false}; + std::mutex mutex_; + std::condition_variable cv_; +}; + +} // namespace android::hardware::health::storage::test