diff options
author | Michael Butler <butlermichael@google.com> | 2020-12-10 15:38:45 -0800 |
---|---|---|
committer | Michael Butler <butlermichael@google.com> | 2021-01-06 12:40:58 -0800 |
commit | afc4d7cfe753669b08562eba8f58cbceefed334f (patch) | |
tree | 85762da5840714f8f23ae781b0dc50d15d276085 /neuralnetworks/utils/common/test/ResilientBufferTest.cpp | |
parent | 31bd11da989a94e020ecbd66a2caf9856ee31ab3 (diff) |
Create unit tests for NN interface utility code
This CL introduces unit tests to validate the V1_X::utils::Device,
*PreparedModel, and *Buffer adapter classes. It does so by mocking the
underlying HIDL interface in order to simulate a driver returning bad
data, HIDL transport failures, and service crashes.
Note that the purpose of these new tests is to validate the adapter
classes themselves, not the HIDL interfaces they use. For example,
because nn::IPreparedModel does not currently define a method for
configuring a burst execution, V1_[23]::utils::PreparedModel similarly
does not use hardware::neuralnetworks::V1_[23]::IPreparedModel's
configureExecutionBurst method.
This CL also introduces unit tests to validate the utils::Resilient*
adapter classes, and mocks DEAD_OBJECT failures to ensure that the
underyling object can be recovered appropriately.
Bug: 163801800
Test: mma
Test: atest neuralnetworks_utils_hal_common_test
Test: atest neuralnetworks_utils_hal_1_[0-3]_test
Change-Id: I2c79865bf666d3f4bf53061ff5090746403583e9
Diffstat (limited to 'neuralnetworks/utils/common/test/ResilientBufferTest.cpp')
-rw-r--r-- | neuralnetworks/utils/common/test/ResilientBufferTest.cpp | 266 |
1 files changed, 266 insertions, 0 deletions
diff --git a/neuralnetworks/utils/common/test/ResilientBufferTest.cpp b/neuralnetworks/utils/common/test/ResilientBufferTest.cpp new file mode 100644 index 0000000000..deb9b7cf21 --- /dev/null +++ b/neuralnetworks/utils/common/test/ResilientBufferTest.cpp @@ -0,0 +1,266 @@ +/* + * Copyright (C) 2020 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. + */ + +#include <gmock/gmock.h> +#include <nnapi/TypeUtils.h> +#include <nnapi/Types.h> +#include <nnapi/hal/ResilientBuffer.h> +#include <tuple> +#include <utility> +#include "MockBuffer.h" + +namespace android::hardware::neuralnetworks::utils { +namespace { + +using ::testing::_; +using ::testing::InvokeWithoutArgs; +using ::testing::Return; + +constexpr auto kToken = nn::Request::MemoryDomainToken{1}; + +using SharedMockBuffer = std::shared_ptr<const nn::MockBuffer>; +using MockBufferFactory = ::testing::MockFunction<nn::GeneralResult<nn::SharedBuffer>()>; + +SharedMockBuffer createConfiguredMockBuffer() { + return std::make_shared<const nn::MockBuffer>(); +} + +std::tuple<std::shared_ptr<const nn::MockBuffer>, std::unique_ptr<MockBufferFactory>, + std::shared_ptr<const ResilientBuffer>> +setup() { + auto mockBuffer = std::make_shared<const nn::MockBuffer>(); + + auto mockBufferFactory = std::make_unique<MockBufferFactory>(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(mockBuffer)); + + auto buffer = ResilientBuffer::create(mockBufferFactory->AsStdFunction()).value(); + return std::make_tuple(std::move(mockBuffer), std::move(mockBufferFactory), std::move(buffer)); +} + +constexpr auto makeError = [](nn::ErrorStatus status) { + return [status](const auto&... /*args*/) { return nn::error(status); }; +}; +const auto kReturnGeneralFailure = makeError(nn::ErrorStatus::GENERAL_FAILURE); +const auto kReturnDeadObject = makeError(nn::ErrorStatus::DEAD_OBJECT); + +const auto kNoError = nn::GeneralResult<void>{}; + +} // namespace + +TEST(ResilientBufferTest, invalidBufferFactory) { + // setup call + const auto invalidBufferFactory = ResilientBuffer::Factory{}; + + // run test + const auto result = ResilientBuffer::create(invalidBufferFactory); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT); +} + +TEST(ResilientBufferTest, bufferFactoryFailure) { + // setup call + const auto invalidBufferFactory = kReturnGeneralFailure; + + // run test + const auto result = ResilientBuffer::create(invalidBufferFactory); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(ResilientBufferTest, getBuffer) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + + // run test + const auto result = buffer->getBuffer(); + + // verify result + EXPECT_TRUE(result == mockBuffer); +} + +TEST(ResilientBufferTest, getToken) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, getToken()).Times(1).WillOnce(Return(kToken)); + + // run test + const auto token = buffer->getToken(); + + // verify result + EXPECT_EQ(token, kToken); +} + +TEST(ResilientBufferTest, copyTo) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(Return(kNoError)); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, copyToError) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(ResilientBufferTest, copyToDeadObjectFailedRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(kReturnDeadObject); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(ResilientBufferTest, copyToDeadObjectSuccessfulRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(kReturnDeadObject); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*recoveredMockBuffer, copyTo(_)).Times(1).WillOnce(Return(kNoError)); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + + // run test + const auto result = buffer->copyTo({}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, copyFrom) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(Return(kNoError)); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, copyFromError) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE); +} + +TEST(ResilientBufferTest, copyFromDeadObjectFailedRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(kReturnDeadObject); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_FALSE(result.has_value()); + EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT); +} + +TEST(ResilientBufferTest, copyFromDeadObjectSuccessfulRecovery) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(kReturnDeadObject); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*recoveredMockBuffer, copyFrom(_, _)).Times(1).WillOnce(Return(kNoError)); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + + // run test + const auto result = buffer->copyFrom({}, {}); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; +} + +TEST(ResilientBufferTest, recover) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + + // run test + const auto result = buffer->recover(mockBuffer.get()); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + EXPECT_TRUE(result.value() == recoveredMockBuffer); +} + +TEST(ResilientBufferTest, recoverFailure) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure); + + // run test + const auto result = buffer->recover(mockBuffer.get()); + + // verify result + EXPECT_FALSE(result.has_value()); +} + +TEST(ResilientBufferTest, someoneElseRecovered) { + // setup call + const auto [mockBuffer, mockBufferFactory, buffer] = setup(); + const auto recoveredMockBuffer = createConfiguredMockBuffer(); + EXPECT_CALL(*mockBufferFactory, Call()).Times(1).WillOnce(Return(recoveredMockBuffer)); + buffer->recover(mockBuffer.get()); + + // run test + const auto result = buffer->recover(mockBuffer.get()); + + // verify result + ASSERT_TRUE(result.has_value()) + << "Failed with " << result.error().code << ": " << result.error().message; + EXPECT_TRUE(result.value() == recoveredMockBuffer); +} + +} // namespace android::hardware::neuralnetworks::utils |