Merge changes I2c79865b,Idfe8093c am: 8be4b590cf
Original change: https://android-review.googlesource.com/c/platform/hardware/interfaces/+/1544546 MUST ONLY BE SUBMITTED BY AUTOMERGER Change-Id: Iff929729f915c29e6eaa72c254766bc1b71faac4
This commit is contained in:
commit
53da7727b4
30 changed files with 6600 additions and 0 deletions
|
@ -32,3 +32,29 @@ cc_library_static {
|
||||||
"neuralnetworks_utils_hal_common",
|
"neuralnetworks_utils_hal_common",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_test {
|
||||||
|
name: "neuralnetworks_utils_hal_1_0_test",
|
||||||
|
srcs: ["test/*.cpp"],
|
||||||
|
static_libs: [
|
||||||
|
"android.hardware.neuralnetworks@1.0",
|
||||||
|
"libgmock",
|
||||||
|
"libneuralnetworks_common",
|
||||||
|
"neuralnetworks_types",
|
||||||
|
"neuralnetworks_utils_hal_common",
|
||||||
|
"neuralnetworks_utils_hal_1_0",
|
||||||
|
],
|
||||||
|
shared_libs: [
|
||||||
|
"android.hidl.allocator@1.0",
|
||||||
|
"android.hidl.memory@1.0",
|
||||||
|
"libbase",
|
||||||
|
"libcutils",
|
||||||
|
"libfmq",
|
||||||
|
"libhidlbase",
|
||||||
|
"libhidlmemory",
|
||||||
|
"liblog",
|
||||||
|
"libnativewindow",
|
||||||
|
"libutils",
|
||||||
|
],
|
||||||
|
test_suites: ["general-tests"],
|
||||||
|
}
|
||||||
|
|
524
neuralnetworks/1.0/utils/test/DeviceTest.cpp
Normal file
524
neuralnetworks/1.0/utils/test/DeviceTest.cpp
Normal file
|
@ -0,0 +1,524 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockDevice.h"
|
||||||
|
#include "MockPreparedModel.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.0/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IDevice.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.0/Device.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_0::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
|
||||||
|
const nn::Model kSimpleModel = {
|
||||||
|
.main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT},
|
||||||
|
{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}},
|
||||||
|
.operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}},
|
||||||
|
.inputIndexes = {0},
|
||||||
|
.outputIndexes = {1}}};
|
||||||
|
|
||||||
|
const std::string kName = "Google-MockV1";
|
||||||
|
const std::string kInvalidName = "";
|
||||||
|
const sp<V1_0::IDevice> kInvalidDevice;
|
||||||
|
constexpr V1_0::PerformanceInfo kNoPerformanceInfo = {
|
||||||
|
.execTime = std::numeric_limits<float>::max(),
|
||||||
|
.powerUsage = std::numeric_limits<float>::max()};
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
auto makeCallbackReturn(Args&&... args) {
|
||||||
|
return [argPack = std::make_tuple(std::forward<Args>(args)...)](const auto& cb) {
|
||||||
|
std::apply(cb, argPack);
|
||||||
|
return Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
sp<MockDevice> createMockDevice() {
|
||||||
|
const auto mockDevice = MockDevice::create();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto getCapabilities_ret = makeCallbackReturn(
|
||||||
|
V1_0::ErrorStatus::NONE, V1_0::Capabilities{
|
||||||
|
.float32Performance = kNoPerformanceInfo,
|
||||||
|
.quantized8Performance = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, getCapabilities(_)).WillByDefault(Invoke(getCapabilities_ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makePreparedModelReturn(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
|
||||||
|
const sp<V1_0::utils::MockPreparedModel>& preparedModel) {
|
||||||
|
return [launchStatus, returnStatus, preparedModel](const V1_0::Model& /*model*/,
|
||||||
|
const sp<V1_0::IPreparedModelCallback>& cb)
|
||||||
|
-> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
cb->notify(returnStatus, preparedModel).isOk();
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidName) {
|
||||||
|
// run test
|
||||||
|
const auto device = MockDevice::create();
|
||||||
|
const auto result = Device::create(kInvalidName, device);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidDevice) {
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, kInvalidDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_0::Capabilities{
|
||||||
|
.float32Performance = kNoPerformanceInfo,
|
||||||
|
.quantized8Performance = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<bool> { return false; };
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getName) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto& name = device->getName();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(name, kName);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getFeatureLevel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto featureLevel = device->getFeatureLevel();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(featureLevel, nn::Version::ANDROID_OC_MR1);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCachedData) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& device = result.value();
|
||||||
|
|
||||||
|
// run test and verify results
|
||||||
|
EXPECT_EQ(device->getVersionString(), device->getVersionString());
|
||||||
|
EXPECT_EQ(device->getType(), device->getType());
|
||||||
|
EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions());
|
||||||
|
EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded());
|
||||||
|
EXPECT_EQ(device->getCapabilities(), device->getCapabilities());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, wait) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<void> { return {}; };
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperations) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& model, const auto& cb) {
|
||||||
|
cb(V1_0::ErrorStatus::NONE, std::vector<bool>(model.operations.size(), true));
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& supportedOperations = result.value();
|
||||||
|
EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size());
|
||||||
|
EXPECT_THAT(supportedOperations, Each(testing::IsTrue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& /*model*/, const auto& cb) {
|
||||||
|
cb(V1_0::ErrorStatus::GENERAL_FAILURE, {});
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto mockPreparedModel = V1_0::utils::MockPreparedModel::create();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, mockPreparedModel)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_NE(result.value(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelLaunchError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelReturnError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelNullptrError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [&mockDevice]() -> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
mockDevice->simulateCrash();
|
||||||
|
return V1_0::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _)).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheNotSupported) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, allocateNotSupported) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_0::utils
|
86
neuralnetworks/1.0/utils/test/MockDevice.h
Normal file
86
neuralnetworks/1.0/utils/test/MockDevice.h
Normal file
|
@ -0,0 +1,86 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_DEVICE
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_DEVICE
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.0/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_0::utils {
|
||||||
|
|
||||||
|
class MockDevice final : public IDevice {
|
||||||
|
public:
|
||||||
|
static sp<MockDevice> create();
|
||||||
|
|
||||||
|
// IBase methods below.
|
||||||
|
MOCK_METHOD(Return<void>, ping, (), (override));
|
||||||
|
MOCK_METHOD(Return<bool>, linkToDeathRet, ());
|
||||||
|
Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t /*cookie*/);
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities, (getCapabilities_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations,
|
||||||
|
(const V1_0::Model& model, getSupportedOperations_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel,
|
||||||
|
(const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::DeviceStatus>, getStatus, (), (override));
|
||||||
|
|
||||||
|
// Helper methods.
|
||||||
|
void simulateCrash();
|
||||||
|
|
||||||
|
private:
|
||||||
|
sp<hidl_death_recipient> mDeathRecipient;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockDevice> MockDevice::create() {
|
||||||
|
auto mockDevice = sp<MockDevice>::make();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto ret = []() -> Return<bool> { return true; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) {
|
||||||
|
mDeathRecipient = recipient;
|
||||||
|
return linkToDeathRet();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void MockDevice::simulateCrash() {
|
||||||
|
ASSERT_NE(nullptr, mDeathRecipient.get());
|
||||||
|
|
||||||
|
// Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
|
||||||
|
// and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
|
||||||
|
// to determine which object is dead. However, the utils::Device code only pairs a single death
|
||||||
|
// recipient with a single HIDL interface object, so these arguments are redundant.
|
||||||
|
mDeathRecipient->serviceDied(0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_0::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_DEVICE
|
85
neuralnetworks/1.0/utils/test/MockPreparedModel.h
Normal file
85
neuralnetworks/1.0/utils/test/MockPreparedModel.h
Normal file
|
@ -0,0 +1,85 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/HidlSupport.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_0::utils {
|
||||||
|
|
||||||
|
class MockPreparedModel final : public IPreparedModel {
|
||||||
|
public:
|
||||||
|
static sp<MockPreparedModel> create();
|
||||||
|
|
||||||
|
// IBase methods below.
|
||||||
|
MOCK_METHOD(Return<void>, ping, (), (override));
|
||||||
|
MOCK_METHOD(Return<bool>, linkToDeathRet, ());
|
||||||
|
Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) override;
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, execute,
|
||||||
|
(const V1_0::Request& request, const sp<V1_0::IExecutionCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// Helper methods.
|
||||||
|
void simulateCrash();
|
||||||
|
|
||||||
|
private:
|
||||||
|
sp<hidl_death_recipient> mDeathRecipient;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockPreparedModel> MockPreparedModel::create() {
|
||||||
|
auto mockPreparedModel = sp<MockPreparedModel>::make();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto ret = []() -> Return<bool> { return true; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockPreparedModel, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockPreparedModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Return<bool> MockPreparedModel::linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) {
|
||||||
|
mDeathRecipient = recipient;
|
||||||
|
return linkToDeathRet();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void MockPreparedModel::simulateCrash() {
|
||||||
|
ASSERT_NE(nullptr, mDeathRecipient.get());
|
||||||
|
|
||||||
|
// Currently, the utils::PreparedModel will not use the `cookie` or `who` arguments, so we pass
|
||||||
|
// in 0 and nullptr for these arguments instead. Normally, they are used by the
|
||||||
|
// hidl_death_recipient to determine which object is dead. However, the utils::PreparedModel
|
||||||
|
// code only pairs a single death recipient with a single HIDL interface object, so these
|
||||||
|
// arguments are redundant.
|
||||||
|
mDeathRecipient->serviceDied(0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_0::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_0_UTILS_TEST_MOCK_PREPARED_MODEL
|
243
neuralnetworks/1.0/utils/test/PreparedModelTest.cpp
Normal file
243
neuralnetworks/1.0/utils/test/PreparedModelTest.cpp
Normal file
|
@ -0,0 +1,243 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockPreparedModel.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.0/IExecutionCallback.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IPreparedModel.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.0/PreparedModel.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_0::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
|
||||||
|
const sp<V1_0::IPreparedModel> kInvalidPreparedModel;
|
||||||
|
|
||||||
|
sp<MockPreparedModel> createMockPreparedModel() {
|
||||||
|
return MockPreparedModel::create();
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makeExecute(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus) {
|
||||||
|
return [launchStatus, returnStatus](
|
||||||
|
const V1_0::Request& /*request*/,
|
||||||
|
const sp<V1_0::IExecutionCallback>& cb) -> Return<V1_0::ErrorStatus> {
|
||||||
|
cb->notify(returnStatus);
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, invalidPreparedModel) {
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(kInvalidPreparedModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto ret = []() -> Return<bool> { return false; };
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, execute) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecute(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeLaunchError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecute(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeReturnError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(
|
||||||
|
makeExecute(V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::GENERAL_FAILURE)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
const auto ret = [&mockPreparedModel]() -> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
mockPreparedModel->simulateCrash();
|
||||||
|
return V1_0::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _)).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeFencedNotSupported) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: test burst execution if/when it is added to nn::IPreparedModel.
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, getUnderlyingResource) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel = PreparedModel::create(mockPreparedModel).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto resource = preparedModel->getUnderlyingResource();
|
||||||
|
|
||||||
|
// verify resource
|
||||||
|
const sp<V1_0::IPreparedModel>* maybeMock = std::any_cast<sp<V1_0::IPreparedModel>>(&resource);
|
||||||
|
ASSERT_NE(maybeMock, nullptr);
|
||||||
|
EXPECT_EQ(maybeMock->get(), mockPreparedModel.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_0::utils
|
|
@ -34,3 +34,31 @@ cc_library_static {
|
||||||
"neuralnetworks_utils_hal_common",
|
"neuralnetworks_utils_hal_common",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_test {
|
||||||
|
name: "neuralnetworks_utils_hal_1_1_test",
|
||||||
|
srcs: ["test/*.cpp"],
|
||||||
|
static_libs: [
|
||||||
|
"android.hardware.neuralnetworks@1.0",
|
||||||
|
"android.hardware.neuralnetworks@1.1",
|
||||||
|
"libgmock",
|
||||||
|
"libneuralnetworks_common",
|
||||||
|
"neuralnetworks_types",
|
||||||
|
"neuralnetworks_utils_hal_common",
|
||||||
|
"neuralnetworks_utils_hal_1_0",
|
||||||
|
"neuralnetworks_utils_hal_1_1",
|
||||||
|
],
|
||||||
|
shared_libs: [
|
||||||
|
"android.hidl.allocator@1.0",
|
||||||
|
"android.hidl.memory@1.0",
|
||||||
|
"libbase",
|
||||||
|
"libcutils",
|
||||||
|
"libfmq",
|
||||||
|
"libhidlbase",
|
||||||
|
"libhidlmemory",
|
||||||
|
"liblog",
|
||||||
|
"libnativewindow",
|
||||||
|
"libutils",
|
||||||
|
],
|
||||||
|
test_suites: ["general-tests"],
|
||||||
|
}
|
||||||
|
|
534
neuralnetworks/1.1/utils/test/DeviceTest.cpp
Normal file
534
neuralnetworks/1.1/utils/test/DeviceTest.cpp
Normal file
|
@ -0,0 +1,534 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockDevice.h"
|
||||||
|
#include "MockPreparedModel.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.1/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IDevice.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.1/Device.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_1::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
|
||||||
|
const nn::Model kSimpleModel = {
|
||||||
|
.main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT},
|
||||||
|
{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}},
|
||||||
|
.operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}},
|
||||||
|
.inputIndexes = {0},
|
||||||
|
.outputIndexes = {1}}};
|
||||||
|
|
||||||
|
const std::string kName = "Google-MockV1";
|
||||||
|
const std::string kInvalidName = "";
|
||||||
|
const sp<V1_1::IDevice> kInvalidDevice;
|
||||||
|
constexpr V1_0::PerformanceInfo kNoPerformanceInfo = {
|
||||||
|
.execTime = std::numeric_limits<float>::max(),
|
||||||
|
.powerUsage = std::numeric_limits<float>::max()};
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
auto makeCallbackReturn(Args&&... args) {
|
||||||
|
return [argPack = std::make_tuple(std::forward<Args>(args)...)](const auto& cb) {
|
||||||
|
std::apply(cb, argPack);
|
||||||
|
return Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
sp<MockDevice> createMockDevice() {
|
||||||
|
const auto mockDevice = MockDevice::create();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto getCapabilities_ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_1::Capabilities{
|
||||||
|
.float32Performance = kNoPerformanceInfo,
|
||||||
|
.quantized8Performance = kNoPerformanceInfo,
|
||||||
|
.relaxedFloat32toFloat16Performance = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, getCapabilities_1_1(_)).WillByDefault(Invoke(getCapabilities_ret));
|
||||||
|
|
||||||
|
// Ensure that older calls are not used.
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _)).Times(0);
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makePreparedModelReturn(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
|
||||||
|
const sp<V1_0::utils::MockPreparedModel>& preparedModel) {
|
||||||
|
return [launchStatus, returnStatus, preparedModel](const V1_1::Model& /*model*/,
|
||||||
|
V1_1::ExecutionPreference /*preference*/,
|
||||||
|
const sp<V1_0::IPreparedModelCallback>& cb)
|
||||||
|
-> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
cb->notify(returnStatus, preparedModel).isOk();
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidName) {
|
||||||
|
// run test
|
||||||
|
const auto device = MockDevice::create();
|
||||||
|
const auto result = Device::create(kInvalidName, device);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidDevice) {
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, kInvalidDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_1::Capabilities{
|
||||||
|
.float32Performance = kNoPerformanceInfo,
|
||||||
|
.quantized8Performance = kNoPerformanceInfo,
|
||||||
|
.relaxedFloat32toFloat16Performance = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_1(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_1(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<bool> { return false; };
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getName) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto& name = device->getName();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(name, kName);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getFeatureLevel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto featureLevel = device->getFeatureLevel();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(featureLevel, nn::Version::ANDROID_P);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCachedData) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& device = result.value();
|
||||||
|
|
||||||
|
// run test and verify results
|
||||||
|
EXPECT_EQ(device->getVersionString(), device->getVersionString());
|
||||||
|
EXPECT_EQ(device->getType(), device->getType());
|
||||||
|
EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions());
|
||||||
|
EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded());
|
||||||
|
EXPECT_EQ(device->getCapabilities(), device->getCapabilities());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, wait) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<void> { return {}; };
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperations) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& model, const auto& cb) {
|
||||||
|
cb(V1_0::ErrorStatus::NONE, std::vector<bool>(model.operations.size(), true));
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_1(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& supportedOperations = result.value();
|
||||||
|
EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size());
|
||||||
|
EXPECT_THAT(supportedOperations, Each(testing::IsTrue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& /*model*/, const auto& cb) {
|
||||||
|
cb(V1_0::ErrorStatus::GENERAL_FAILURE, {});
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_1(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_1(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_1(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto mockPreparedModel = V1_0::utils::MockPreparedModel::create();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, mockPreparedModel)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_NE(result.value(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelLaunchError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelReturnError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelNullptrError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [&mockDevice]() -> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
mockDevice->simulateCrash();
|
||||||
|
return V1_0::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _)).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheNotSupported) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, allocateNotSupported) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_1::utils
|
95
neuralnetworks/1.1/utils/test/MockDevice.h
Normal file
95
neuralnetworks/1.1/utils/test/MockDevice.h
Normal file
|
@ -0,0 +1,95 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_DEVICE
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_DEVICE
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.1/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_1::utils {
|
||||||
|
|
||||||
|
class MockDevice final : public IDevice {
|
||||||
|
public:
|
||||||
|
static sp<MockDevice> create();
|
||||||
|
|
||||||
|
// IBase methods below.
|
||||||
|
MOCK_METHOD(Return<void>, ping, (), (override));
|
||||||
|
MOCK_METHOD(Return<bool>, linkToDeathRet, ());
|
||||||
|
Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t /*cookie*/);
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities, (getCapabilities_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations,
|
||||||
|
(const V1_0::Model& model, getSupportedOperations_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel,
|
||||||
|
(const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::DeviceStatus>, getStatus, (), (override));
|
||||||
|
|
||||||
|
// V1_1 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities_1_1, (getCapabilities_1_1_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations_1_1,
|
||||||
|
(const V1_1::Model& model, getSupportedOperations_1_1_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_1,
|
||||||
|
(const V1_1::Model& model, V1_1::ExecutionPreference preference,
|
||||||
|
const sp<V1_0::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// Helper methods.
|
||||||
|
void simulateCrash();
|
||||||
|
|
||||||
|
private:
|
||||||
|
sp<hidl_death_recipient> mDeathRecipient;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockDevice> MockDevice::create() {
|
||||||
|
auto mockDevice = sp<MockDevice>::make();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto ret = []() -> Return<bool> { return true; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) {
|
||||||
|
mDeathRecipient = recipient;
|
||||||
|
return linkToDeathRet();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void MockDevice::simulateCrash() {
|
||||||
|
ASSERT_NE(nullptr, mDeathRecipient.get());
|
||||||
|
|
||||||
|
// Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
|
||||||
|
// and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
|
||||||
|
// to determine which object is dead. However, the utils::Device code only pairs a single death
|
||||||
|
// recipient with a single HIDL interface object, so these arguments are redundant.
|
||||||
|
mDeathRecipient->serviceDied(0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_1::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_DEVICE
|
44
neuralnetworks/1.1/utils/test/MockPreparedModel.h
Normal file
44
neuralnetworks/1.1/utils/test/MockPreparedModel.h
Normal file
|
@ -0,0 +1,44 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.0/IPreparedModel.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/HidlSupport.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_0::utils {
|
||||||
|
|
||||||
|
class MockPreparedModel final : public IPreparedModel {
|
||||||
|
public:
|
||||||
|
static sp<MockPreparedModel> create();
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, execute,
|
||||||
|
(const V1_0::Request& request, const sp<V1_0::IExecutionCallback>& callback),
|
||||||
|
(override));
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockPreparedModel> MockPreparedModel::create() {
|
||||||
|
return sp<MockPreparedModel>::make();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_0::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_1_UTILS_TEST_MOCK_PREPARED_MODEL
|
|
@ -36,3 +36,33 @@ cc_library_static {
|
||||||
"neuralnetworks_utils_hal_common",
|
"neuralnetworks_utils_hal_common",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_test {
|
||||||
|
name: "neuralnetworks_utils_hal_1_2_test",
|
||||||
|
srcs: ["test/*.cpp"],
|
||||||
|
static_libs: [
|
||||||
|
"android.hardware.neuralnetworks@1.0",
|
||||||
|
"android.hardware.neuralnetworks@1.1",
|
||||||
|
"android.hardware.neuralnetworks@1.2",
|
||||||
|
"libgmock",
|
||||||
|
"libneuralnetworks_common",
|
||||||
|
"neuralnetworks_types",
|
||||||
|
"neuralnetworks_utils_hal_common",
|
||||||
|
"neuralnetworks_utils_hal_1_0",
|
||||||
|
"neuralnetworks_utils_hal_1_1",
|
||||||
|
"neuralnetworks_utils_hal_1_2",
|
||||||
|
],
|
||||||
|
shared_libs: [
|
||||||
|
"android.hidl.allocator@1.0",
|
||||||
|
"android.hidl.memory@1.0",
|
||||||
|
"libbase",
|
||||||
|
"libcutils",
|
||||||
|
"libfmq",
|
||||||
|
"libhidlbase",
|
||||||
|
"libhidlmemory",
|
||||||
|
"liblog",
|
||||||
|
"libnativewindow",
|
||||||
|
"libutils",
|
||||||
|
],
|
||||||
|
test_suites: ["general-tests"],
|
||||||
|
}
|
||||||
|
|
875
neuralnetworks/1.2/utils/test/DeviceTest.cpp
Normal file
875
neuralnetworks/1.2/utils/test/DeviceTest.cpp
Normal file
|
@ -0,0 +1,875 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockDevice.h"
|
||||||
|
#include "MockPreparedModel.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.2/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IDevice.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.2/Device.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_2::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
|
||||||
|
const nn::Model kSimpleModel = {
|
||||||
|
.main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT},
|
||||||
|
{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}},
|
||||||
|
.operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}},
|
||||||
|
.inputIndexes = {0},
|
||||||
|
.outputIndexes = {1}}};
|
||||||
|
|
||||||
|
const std::string kName = "Google-MockV1";
|
||||||
|
const std::string kInvalidName = "";
|
||||||
|
const sp<V1_2::IDevice> kInvalidDevice;
|
||||||
|
constexpr V1_0::PerformanceInfo kNoPerformanceInfo = {
|
||||||
|
.execTime = std::numeric_limits<float>::max(),
|
||||||
|
.powerUsage = std::numeric_limits<float>::max()};
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
auto makeCallbackReturn(Args&&... args) {
|
||||||
|
return [argPack = std::make_tuple(std::forward<Args>(args)...)](const auto& cb) {
|
||||||
|
std::apply(cb, argPack);
|
||||||
|
return Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
sp<MockDevice> createMockDevice() {
|
||||||
|
const auto mockDevice = MockDevice::create();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto getVersionString_ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, kName);
|
||||||
|
const auto getType_ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, V1_2::DeviceType::OTHER);
|
||||||
|
const auto getSupportedExtensions_ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::NONE, hidl_vec<V1_2::Extension>{});
|
||||||
|
const auto getNumberOfCacheFilesNeeded_ret = makeCallbackReturn(
|
||||||
|
V1_0::ErrorStatus::NONE, nn::kMaxNumberOfCacheFiles, nn::kMaxNumberOfCacheFiles);
|
||||||
|
const auto getCapabilities_ret = makeCallbackReturn(
|
||||||
|
V1_0::ErrorStatus::NONE,
|
||||||
|
V1_2::Capabilities{
|
||||||
|
.relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo,
|
||||||
|
.relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, getVersionString(_)).WillByDefault(Invoke(getVersionString_ret));
|
||||||
|
ON_CALL(*mockDevice, getType(_)).WillByDefault(Invoke(getType_ret));
|
||||||
|
ON_CALL(*mockDevice, getSupportedExtensions(_))
|
||||||
|
.WillByDefault(Invoke(getSupportedExtensions_ret));
|
||||||
|
ON_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
|
||||||
|
.WillByDefault(Invoke(getNumberOfCacheFilesNeeded_ret));
|
||||||
|
ON_CALL(*mockDevice, getCapabilities_1_2(_)).WillByDefault(Invoke(getCapabilities_ret));
|
||||||
|
|
||||||
|
// Ensure that older calls are not used.
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_1(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _)).Times(0);
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makePreparedModelReturn(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
|
||||||
|
const sp<MockPreparedModel>& preparedModel) {
|
||||||
|
return [launchStatus, returnStatus, preparedModel](
|
||||||
|
const V1_2::Model& /*model*/, V1_1::ExecutionPreference /*preference*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*modelCache*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*dataCache*/,
|
||||||
|
const CacheToken& /*token*/, const sp<V1_2::IPreparedModelCallback>& cb)
|
||||||
|
-> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
cb->notify_1_2(returnStatus, preparedModel).isOk();
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto makePreparedModelFromCacheReturn(V1_0::ErrorStatus launchStatus,
|
||||||
|
V1_0::ErrorStatus returnStatus,
|
||||||
|
const sp<MockPreparedModel>& preparedModel) {
|
||||||
|
return [launchStatus, returnStatus, preparedModel](
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*modelCache*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*dataCache*/,
|
||||||
|
const CacheToken& /*token*/, const sp<V1_2::IPreparedModelCallback>& cb)
|
||||||
|
-> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
cb->notify_1_2(returnStatus, preparedModel).isOk();
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidName) {
|
||||||
|
// run test
|
||||||
|
const auto device = MockDevice::create();
|
||||||
|
const auto result = Device::create(kInvalidName, device);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidDevice) {
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, kInvalidDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getVersionStringError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, "");
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getVersionStringTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getVersionStringDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getTypeError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, V1_2::DeviceType::OTHER);
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getTypeTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getTypeDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedExtensionsError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, hidl_vec<V1_2::Extension>{});
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedExtensionsTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedExtensionsDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getNumberOfCacheFilesNeededError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
nn::kMaxNumberOfCacheFiles, nn::kMaxNumberOfCacheFiles);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, dataCacheFilesExceedsSpecifiedMax) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, nn::kMaxNumberOfCacheFiles + 1,
|
||||||
|
nn::kMaxNumberOfCacheFiles);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, modelCacheFilesExceedsSpecifiedMax) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, nn::kMaxNumberOfCacheFiles,
|
||||||
|
nn::kMaxNumberOfCacheFiles + 1);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getNumberOfCacheFilesNeededDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_2::Capabilities{
|
||||||
|
.relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo,
|
||||||
|
.relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_2(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_2(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<bool> { return false; };
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getName) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto& name = device->getName();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(name, kName);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getFeatureLevel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto featureLevel = device->getFeatureLevel();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(featureLevel, nn::Version::ANDROID_Q);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCachedData) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(1);
|
||||||
|
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& device = result.value();
|
||||||
|
|
||||||
|
// run test and verify results
|
||||||
|
EXPECT_EQ(device->getVersionString(), device->getVersionString());
|
||||||
|
EXPECT_EQ(device->getType(), device->getType());
|
||||||
|
EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions());
|
||||||
|
EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded());
|
||||||
|
EXPECT_EQ(device->getCapabilities(), device->getCapabilities());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, wait) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<void> { return {}; };
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperations) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& model, const auto& cb) {
|
||||||
|
cb(V1_0::ErrorStatus::NONE, std::vector<bool>(model.operations.size(), true));
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_2(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& supportedOperations = result.value();
|
||||||
|
EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size());
|
||||||
|
EXPECT_THAT(supportedOperations, Each(testing::IsTrue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& /*model*/, const auto& cb) {
|
||||||
|
cb(V1_0::ErrorStatus::GENERAL_FAILURE, {});
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_2(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_2(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_2(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto mockPreparedModel = MockPreparedModel::create();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, mockPreparedModel)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_NE(result.value(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelLaunchError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelReturnError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelNullptrError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [&mockDevice]() -> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
mockDevice->simulateCrash();
|
||||||
|
return V1_0::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCache) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto mockPreparedModel = MockPreparedModel::create();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelFromCacheReturn(
|
||||||
|
V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::NONE, mockPreparedModel)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_NE(result.value(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelFromCacheReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheNullptrError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelFromCacheReturn(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [&mockDevice]() -> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
mockDevice->simulateCrash();
|
||||||
|
return V1_0::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, allocateNotSupported) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_2::utils
|
117
neuralnetworks/1.2/utils/test/MockDevice.h
Normal file
117
neuralnetworks/1.2/utils/test/MockDevice.h
Normal file
|
@ -0,0 +1,117 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_DEVICE
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_DEVICE
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.2/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_2::utils {
|
||||||
|
|
||||||
|
using CacheToken =
|
||||||
|
hidl_array<uint8_t, static_cast<uint32_t>(V1_2::Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
|
||||||
|
|
||||||
|
class MockDevice final : public IDevice {
|
||||||
|
public:
|
||||||
|
static sp<MockDevice> create();
|
||||||
|
|
||||||
|
// IBase methods below.
|
||||||
|
MOCK_METHOD(Return<void>, ping, (), (override));
|
||||||
|
MOCK_METHOD(Return<bool>, linkToDeathRet, ());
|
||||||
|
Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t /*cookie*/);
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities, (getCapabilities_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations,
|
||||||
|
(const V1_0::Model& model, getSupportedOperations_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel,
|
||||||
|
(const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::DeviceStatus>, getStatus, (), (override));
|
||||||
|
|
||||||
|
// V1_1 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities_1_1, (getCapabilities_1_1_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations_1_1,
|
||||||
|
(const V1_1::Model& model, getSupportedOperations_1_1_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_1,
|
||||||
|
(const V1_1::Model& model, V1_1::ExecutionPreference preference,
|
||||||
|
const sp<V1_0::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// V1_2 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getVersionString, (getVersionString_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getType, (getType_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities_1_2, (getCapabilities_1_2_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedExtensions, (getSupportedExtensions_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations_1_2,
|
||||||
|
(const V1_2::Model& model, getSupportedOperations_1_2_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getNumberOfCacheFilesNeeded, (getNumberOfCacheFilesNeeded_cb cb),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_2,
|
||||||
|
(const V1_2::Model& model, V1_1::ExecutionPreference preference,
|
||||||
|
const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
|
||||||
|
const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModelFromCache,
|
||||||
|
(const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
|
||||||
|
const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// Helper methods.
|
||||||
|
void simulateCrash();
|
||||||
|
|
||||||
|
private:
|
||||||
|
sp<hidl_death_recipient> mDeathRecipient;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockDevice> MockDevice::create() {
|
||||||
|
auto mockDevice = sp<MockDevice>::make();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto ret = []() -> Return<bool> { return true; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) {
|
||||||
|
mDeathRecipient = recipient;
|
||||||
|
return linkToDeathRet();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void MockDevice::simulateCrash() {
|
||||||
|
ASSERT_NE(nullptr, mDeathRecipient.get());
|
||||||
|
|
||||||
|
// Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
|
||||||
|
// and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
|
||||||
|
// to determine which object is dead. However, the utils::Device code only pairs a single death
|
||||||
|
// recipient with a single HIDL interface object, so these arguments are redundant.
|
||||||
|
mDeathRecipient->serviceDied(0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_2::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_DEVICE
|
101
neuralnetworks/1.2/utils/test/MockPreparedModel.h
Normal file
101
neuralnetworks/1.2/utils/test/MockPreparedModel.h
Normal file
|
@ -0,0 +1,101 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.2/IPreparedModel.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/HidlSupport.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_2::utils {
|
||||||
|
|
||||||
|
class MockPreparedModel final : public IPreparedModel {
|
||||||
|
public:
|
||||||
|
static sp<MockPreparedModel> create();
|
||||||
|
|
||||||
|
// IBase methods below.
|
||||||
|
MOCK_METHOD(Return<void>, ping, (), (override));
|
||||||
|
MOCK_METHOD(Return<bool>, linkToDeathRet, ());
|
||||||
|
Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) override;
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, execute,
|
||||||
|
(const V1_0::Request& request, const sp<V1_0::IExecutionCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// V1_2 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, execute_1_2,
|
||||||
|
(const V1_0::Request& request, V1_2::MeasureTiming measure,
|
||||||
|
const sp<V1_2::IExecutionCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<void>, executeSynchronously,
|
||||||
|
(const V1_0::Request& request, V1_2::MeasureTiming measure,
|
||||||
|
executeSynchronously_cb cb),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<void>, configureExecutionBurst,
|
||||||
|
(const sp<V1_2::IBurstCallback>& callback,
|
||||||
|
const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel,
|
||||||
|
const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel,
|
||||||
|
configureExecutionBurst_cb cb),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// Helper methods.
|
||||||
|
void simulateCrash();
|
||||||
|
|
||||||
|
private:
|
||||||
|
sp<hidl_death_recipient> mDeathRecipient;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockPreparedModel> MockPreparedModel::create() {
|
||||||
|
auto mockPreparedModel = sp<MockPreparedModel>::make();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto ret = []() -> Return<bool> { return true; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockPreparedModel, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockPreparedModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Return<bool> MockPreparedModel::linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) {
|
||||||
|
mDeathRecipient = recipient;
|
||||||
|
return linkToDeathRet();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void MockPreparedModel::simulateCrash() {
|
||||||
|
ASSERT_NE(nullptr, mDeathRecipient.get());
|
||||||
|
|
||||||
|
// Currently, the utils::PreparedModel will not use the `cookie` or `who` arguments, so we pass
|
||||||
|
// in 0 and nullptr for these arguments instead. Normally, they are used by the
|
||||||
|
// hidl_death_recipient to determine which object is dead. However, the utils::PreparedModel
|
||||||
|
// code only pairs a single death recipient with a single HIDL interface object, so these
|
||||||
|
// arguments are redundant.
|
||||||
|
mDeathRecipient->serviceDied(0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_2::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_2_UTILS_TEST_MOCK_PREPARED_MODEL
|
341
neuralnetworks/1.2/utils/test/PreparedModelTest.cpp
Normal file
341
neuralnetworks/1.2/utils/test/PreparedModelTest.cpp
Normal file
|
@ -0,0 +1,341 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockPreparedModel.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.2/IExecutionCallback.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IPreparedModel.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.2/PreparedModel.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_2::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
|
||||||
|
const sp<V1_2::IPreparedModel> kInvalidPreparedModel;
|
||||||
|
constexpr auto kNoTiming = V1_2::Timing{.timeOnDevice = std::numeric_limits<uint64_t>::max(),
|
||||||
|
.timeInDriver = std::numeric_limits<uint64_t>::max()};
|
||||||
|
|
||||||
|
sp<MockPreparedModel> createMockPreparedModel() {
|
||||||
|
const auto mockPreparedModel = MockPreparedModel::create();
|
||||||
|
|
||||||
|
// Ensure that older calls are not used.
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _)).Times(0);
|
||||||
|
|
||||||
|
return mockPreparedModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makeExecuteSynchronously(V1_0::ErrorStatus status,
|
||||||
|
const std::vector<V1_2::OutputShape>& outputShapes,
|
||||||
|
const V1_2::Timing& timing) {
|
||||||
|
return [status, outputShapes, timing](const V1_0::Request& /*request*/,
|
||||||
|
V1_2::MeasureTiming /*measureTiming*/,
|
||||||
|
const V1_2::IPreparedModel::executeSynchronously_cb& cb) {
|
||||||
|
cb(status, outputShapes, timing);
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto makeExecuteAsynchronously(V1_0::ErrorStatus launchStatus, V1_0::ErrorStatus returnStatus,
|
||||||
|
const std::vector<V1_2::OutputShape>& outputShapes,
|
||||||
|
const V1_2::Timing& timing) {
|
||||||
|
return [launchStatus, returnStatus, outputShapes, timing](
|
||||||
|
const V1_0::Request& /*request*/, V1_2::MeasureTiming /*measureTiming*/,
|
||||||
|
const sp<V1_2::IExecutionCallback>& cb) -> Return<V1_0::ErrorStatus> {
|
||||||
|
cb->notify_1_2(returnStatus, outputShapes, timing);
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, invalidPreparedModel) {
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(kInvalidPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto ret = []() -> Return<bool> { return false; };
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSync) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteSynchronously(V1_0::ErrorStatus::NONE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSyncError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(
|
||||||
|
makeExecuteSynchronously(V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSyncTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSyncDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsync) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_2(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteAsynchronously(V1_0::ErrorStatus::NONE,
|
||||||
|
V1_0::ErrorStatus::NONE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncLaunchError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_2(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteAsynchronously(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_0::ErrorStatus::GENERAL_FAILURE, {},
|
||||||
|
kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncReturnError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_2(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteAsynchronously(
|
||||||
|
V1_0::ErrorStatus::NONE, V1_0::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_2(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_2(_, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
const auto ret = [&mockPreparedModel]() -> hardware::Return<V1_0::ErrorStatus> {
|
||||||
|
mockPreparedModel->simulateCrash();
|
||||||
|
return V1_0::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_2(_, _, _)).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeFencedNotSupported) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: test burst execution if/when it is added to nn::IPreparedModel.
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, getUnderlyingResource) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto resource = preparedModel->getUnderlyingResource();
|
||||||
|
|
||||||
|
// verify resource
|
||||||
|
const sp<V1_2::IPreparedModel>* maybeMock = std::any_cast<sp<V1_2::IPreparedModel>>(&resource);
|
||||||
|
ASSERT_NE(maybeMock, nullptr);
|
||||||
|
EXPECT_EQ(maybeMock->get(), mockPreparedModel.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_2::utils
|
|
@ -38,3 +38,35 @@ cc_library_static {
|
||||||
"neuralnetworks_utils_hal_common",
|
"neuralnetworks_utils_hal_common",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_test {
|
||||||
|
name: "neuralnetworks_utils_hal_1_3_test",
|
||||||
|
srcs: ["test/*.cpp"],
|
||||||
|
static_libs: [
|
||||||
|
"android.hardware.neuralnetworks@1.0",
|
||||||
|
"android.hardware.neuralnetworks@1.1",
|
||||||
|
"android.hardware.neuralnetworks@1.2",
|
||||||
|
"android.hardware.neuralnetworks@1.3",
|
||||||
|
"libgmock",
|
||||||
|
"libneuralnetworks_common",
|
||||||
|
"neuralnetworks_types",
|
||||||
|
"neuralnetworks_utils_hal_common",
|
||||||
|
"neuralnetworks_utils_hal_1_0",
|
||||||
|
"neuralnetworks_utils_hal_1_1",
|
||||||
|
"neuralnetworks_utils_hal_1_2",
|
||||||
|
"neuralnetworks_utils_hal_1_3",
|
||||||
|
],
|
||||||
|
shared_libs: [
|
||||||
|
"android.hidl.allocator@1.0",
|
||||||
|
"android.hidl.memory@1.0",
|
||||||
|
"libbase",
|
||||||
|
"libcutils",
|
||||||
|
"libfmq",
|
||||||
|
"libhidlbase",
|
||||||
|
"libhidlmemory",
|
||||||
|
"liblog",
|
||||||
|
"libnativewindow",
|
||||||
|
"libutils",
|
||||||
|
],
|
||||||
|
test_suites: ["general-tests"],
|
||||||
|
}
|
||||||
|
|
208
neuralnetworks/1.3/utils/test/BufferTest.cpp
Normal file
208
neuralnetworks/1.3/utils/test/BufferTest.cpp
Normal file
|
@ -0,0 +1,208 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockBuffer.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IBuffer.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IBuffer.h>
|
||||||
|
#include <nnapi/SharedMemory.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.3/Buffer.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_3::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
using ::testing::Return;
|
||||||
|
|
||||||
|
const auto kMemory = nn::createSharedMemory(4).value();
|
||||||
|
const sp<V1_3::IBuffer> kInvalidBuffer;
|
||||||
|
constexpr auto kInvalidToken = nn::Request::MemoryDomainToken{0};
|
||||||
|
constexpr auto kToken = nn::Request::MemoryDomainToken{1};
|
||||||
|
|
||||||
|
std::function<hardware::Return<V1_3::ErrorStatus>()> makeFunctionReturn(V1_3::ErrorStatus status) {
|
||||||
|
return [status]() -> hardware::Return<V1_3::ErrorStatus> { return status; };
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeSuccessful = makeFunctionReturn(V1_3::ErrorStatus::NONE);
|
||||||
|
const auto makeGeneralError = makeFunctionReturn(V1_3::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(BufferTest, invalidBuffer) {
|
||||||
|
// run test
|
||||||
|
const auto result = Buffer::create(kInvalidBuffer, kToken);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, invalidToken) {
|
||||||
|
// setup call
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Buffer::create(mockBuffer, kInvalidToken);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, create) {
|
||||||
|
// setup call
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto token = buffer->getToken();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(token, kToken);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyTo) {
|
||||||
|
// setup call
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeSuccessful));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyTo(kMemory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result.has_value()) << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyToError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralError));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyTo(kMemory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyToTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyTo(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyTo(kMemory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyToDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyTo(_)).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyTo(kMemory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyFrom) {
|
||||||
|
// setup call
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(InvokeWithoutArgs(makeSuccessful));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyFrom(kMemory, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result.has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyFromError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyFrom(_, _)).Times(1).WillOnce(InvokeWithoutArgs(makeGeneralError));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyFrom(kMemory, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyFromTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyFrom(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyFrom(kMemory, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(BufferTest, copyFromDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
const auto buffer = Buffer::create(mockBuffer, kToken).value();
|
||||||
|
EXPECT_CALL(*mockBuffer, copyFrom(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = buffer->copyFrom(kMemory, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_3::utils
|
951
neuralnetworks/1.3/utils/test/DeviceTest.cpp
Normal file
951
neuralnetworks/1.3/utils/test/DeviceTest.cpp
Normal file
|
@ -0,0 +1,951 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockBuffer.h"
|
||||||
|
#include "MockDevice.h"
|
||||||
|
#include "MockPreparedModel.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IDevice.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.3/Device.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_3::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
|
||||||
|
const nn::Model kSimpleModel = {
|
||||||
|
.main = {.operands = {{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_INPUT},
|
||||||
|
{.type = nn::OperandType::TENSOR_FLOAT32,
|
||||||
|
.dimensions = {1},
|
||||||
|
.lifetime = nn::Operand::LifeTime::SUBGRAPH_OUTPUT}},
|
||||||
|
.operations = {{.type = nn::OperationType::RELU, .inputs = {0}, .outputs = {1}}},
|
||||||
|
.inputIndexes = {0},
|
||||||
|
.outputIndexes = {1}}};
|
||||||
|
|
||||||
|
const std::string kName = "Google-MockV1";
|
||||||
|
const std::string kInvalidName = "";
|
||||||
|
const sp<V1_3::IDevice> kInvalidDevice;
|
||||||
|
constexpr V1_0::PerformanceInfo kNoPerformanceInfo = {
|
||||||
|
.execTime = std::numeric_limits<float>::max(),
|
||||||
|
.powerUsage = std::numeric_limits<float>::max()};
|
||||||
|
|
||||||
|
template <typename... Args>
|
||||||
|
auto makeCallbackReturn(Args&&... args) {
|
||||||
|
return [argPack = std::make_tuple(std::forward<Args>(args)...)](const auto& cb) {
|
||||||
|
std::apply(cb, argPack);
|
||||||
|
return Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
sp<MockDevice> createMockDevice() {
|
||||||
|
const auto mockDevice = MockDevice::create();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto getVersionString_ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, kName);
|
||||||
|
const auto getType_ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, V1_2::DeviceType::OTHER);
|
||||||
|
const auto getSupportedExtensions_ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::NONE, hidl_vec<V1_2::Extension>{});
|
||||||
|
const auto getNumberOfCacheFilesNeeded_ret = makeCallbackReturn(
|
||||||
|
V1_0::ErrorStatus::NONE, nn::kMaxNumberOfCacheFiles, nn::kMaxNumberOfCacheFiles);
|
||||||
|
const auto getCapabilities_ret = makeCallbackReturn(
|
||||||
|
V1_3::ErrorStatus::NONE,
|
||||||
|
V1_3::Capabilities{
|
||||||
|
.relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo,
|
||||||
|
.relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo,
|
||||||
|
.ifPerformance = kNoPerformanceInfo,
|
||||||
|
.whilePerformance = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, getVersionString(_)).WillByDefault(Invoke(getVersionString_ret));
|
||||||
|
ON_CALL(*mockDevice, getType(_)).WillByDefault(Invoke(getType_ret));
|
||||||
|
ON_CALL(*mockDevice, getSupportedExtensions(_))
|
||||||
|
.WillByDefault(Invoke(getSupportedExtensions_ret));
|
||||||
|
ON_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
|
||||||
|
.WillByDefault(Invoke(getNumberOfCacheFilesNeeded_ret));
|
||||||
|
ON_CALL(*mockDevice, getCapabilities_1_3(_)).WillByDefault(Invoke(getCapabilities_ret));
|
||||||
|
|
||||||
|
// Ensure that older calls are not used.
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities(_)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_1(_)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_2(_)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_1(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_1(_, _, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_2(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_2(_, _, _, _, _, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _)).Times(0);
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makePreparedModelReturn(V1_3::ErrorStatus launchStatus, V1_3::ErrorStatus returnStatus,
|
||||||
|
const sp<MockPreparedModel>& preparedModel) {
|
||||||
|
return [launchStatus, returnStatus, preparedModel](
|
||||||
|
const V1_3::Model& /*model*/, V1_1::ExecutionPreference /*preference*/,
|
||||||
|
V1_3::Priority /*priority*/, const V1_3::OptionalTimePoint& /*deadline*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*modelCache*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*dataCache*/,
|
||||||
|
const CacheToken& /*token*/, const sp<V1_3::IPreparedModelCallback>& cb)
|
||||||
|
-> hardware::Return<V1_3::ErrorStatus> {
|
||||||
|
cb->notify_1_3(returnStatus, preparedModel).isOk();
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto makePreparedModelFromCacheReturn(V1_3::ErrorStatus launchStatus,
|
||||||
|
V1_3::ErrorStatus returnStatus,
|
||||||
|
const sp<MockPreparedModel>& preparedModel) {
|
||||||
|
return [launchStatus, returnStatus, preparedModel](
|
||||||
|
const V1_3::OptionalTimePoint& /*deadline*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*modelCache*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*dataCache*/,
|
||||||
|
const CacheToken& /*token*/, const sp<V1_3::IPreparedModelCallback>& cb)
|
||||||
|
-> hardware::Return<V1_3::ErrorStatus> {
|
||||||
|
cb->notify_1_3(returnStatus, preparedModel).isOk();
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto makeAllocateReturn(ErrorStatus status, const sp<MockBuffer>& buffer, uint32_t token) {
|
||||||
|
return [status, buffer, token](
|
||||||
|
const V1_3::BufferDesc& /*desc*/,
|
||||||
|
const hardware::hidl_vec<sp<V1_3::IPreparedModel>>& /*preparedModels*/,
|
||||||
|
const hardware::hidl_vec<V1_3::BufferRole>& /*inputRoles*/,
|
||||||
|
const hardware::hidl_vec<V1_3::BufferRole>& /*outputRoles*/,
|
||||||
|
const V1_3::IDevice::allocate_cb& cb) -> hardware::Return<void> {
|
||||||
|
cb(status, buffer, token);
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidName) {
|
||||||
|
// run test
|
||||||
|
const auto device = MockDevice::create();
|
||||||
|
const auto result = Device::create(kInvalidName, device);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, invalidDevice) {
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, kInvalidDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getVersionStringError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, "");
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getVersionStringTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getVersionStringDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getTypeError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, V1_2::DeviceType::OTHER);
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getTypeTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getTypeDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedExtensionsError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret =
|
||||||
|
makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE, hidl_vec<V1_2::Extension>{});
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedExtensionsTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedExtensionsDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getNumberOfCacheFilesNeededError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
nn::kMaxNumberOfCacheFiles, nn::kMaxNumberOfCacheFiles);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, dataCacheFilesExceedsSpecifiedMax) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, nn::kMaxNumberOfCacheFiles + 1,
|
||||||
|
nn::kMaxNumberOfCacheFiles);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, modelCacheFilesExceedsSpecifiedMax) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(V1_0::ErrorStatus::NONE, nn::kMaxNumberOfCacheFiles,
|
||||||
|
nn::kMaxNumberOfCacheFiles + 1);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getNumberOfCacheFilesNeededTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getNumberOfCacheFilesNeededDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = makeCallbackReturn(
|
||||||
|
V1_3::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_3::Capabilities{
|
||||||
|
.relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo,
|
||||||
|
.relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo,
|
||||||
|
.ifPerformance = kNoPerformanceInfo,
|
||||||
|
.whilePerformance = kNoPerformanceInfo,
|
||||||
|
});
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_3(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCapabilitiesDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_3(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<bool> { return false; };
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, linkToDeathDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getName) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto& name = device->getName();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(name, kName);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getFeatureLevel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto featureLevel = device->getFeatureLevel();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_EQ(featureLevel, nn::Version::ANDROID_R);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getCachedData) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getType(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded(_)).Times(1);
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities_1_3(_)).Times(1);
|
||||||
|
|
||||||
|
const auto result = Device::create(kName, mockDevice);
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& device = result.value();
|
||||||
|
|
||||||
|
// run test and verify results
|
||||||
|
EXPECT_EQ(device->getVersionString(), device->getVersionString());
|
||||||
|
EXPECT_EQ(device->getType(), device->getType());
|
||||||
|
EXPECT_EQ(device->getSupportedExtensions(), device->getSupportedExtensions());
|
||||||
|
EXPECT_EQ(device->getNumberOfCacheFilesNeeded(), device->getNumberOfCacheFilesNeeded());
|
||||||
|
EXPECT_EQ(device->getCapabilities(), device->getCapabilities());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, wait) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto ret = []() -> Return<void> { return {}; };
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, waitDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
EXPECT_CALL(*mockDevice, ping()).Times(1).WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperations) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& model, const auto& cb) {
|
||||||
|
cb(V1_3::ErrorStatus::NONE, std::vector<bool>(model.main.operations.size(), true));
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_3(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& supportedOperations = result.value();
|
||||||
|
EXPECT_EQ(supportedOperations.size(), kSimpleModel.main.operations.size());
|
||||||
|
EXPECT_THAT(supportedOperations, Each(testing::IsTrue()));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [](const auto& /*model*/, const auto& cb) {
|
||||||
|
cb(V1_3::ErrorStatus::GENERAL_FAILURE, {});
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_3(_, _)).Times(1).WillOnce(Invoke(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_3(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, getSupportedOperationsDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations_1_3(_, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations(kSimpleModel);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModel) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto mockPreparedModel = MockPreparedModel::create();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_3::ErrorStatus::NONE,
|
||||||
|
V1_3::ErrorStatus::NONE, mockPreparedModel)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_NE(result.value(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelLaunchError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_3::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_3::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelReturnError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_3::ErrorStatus::NONE,
|
||||||
|
V1_3::ErrorStatus::GENERAL_FAILURE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelNullptrError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelReturn(V1_3::ErrorStatus::NONE,
|
||||||
|
V1_3::ErrorStatus::NONE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [&mockDevice]() -> hardware::Return<V1_3::ErrorStatus> {
|
||||||
|
mockDevice->simulateCrash();
|
||||||
|
return V1_3::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel_1_3(_, _, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel(kSimpleModel, nn::ExecutionPreference::DEFAULT,
|
||||||
|
nn::Priority::DEFAULT, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCache) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto mockPreparedModel = MockPreparedModel::create();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelFromCacheReturn(
|
||||||
|
V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::NONE, mockPreparedModel)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_NE(result.value(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelFromCacheReturn(V1_3::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_3::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheNullptrError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makePreparedModelFromCacheReturn(V1_3::ErrorStatus::NONE,
|
||||||
|
V1_3::ErrorStatus::NONE, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, prepareModelFromCacheAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto ret = [&mockDevice]() -> hardware::Return<V1_3::ErrorStatus> {
|
||||||
|
mockDevice->simulateCrash();
|
||||||
|
return V1_3::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, allocate) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
const auto mockBuffer = MockBuffer::create();
|
||||||
|
constexpr uint32_t token = 1;
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeAllocateReturn(ErrorStatus::NONE, mockBuffer, token)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_NE(result.value(), nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, allocateError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeAllocateReturn(ErrorStatus::GENERAL_FAILURE, nullptr, 0)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, allocateTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(DeviceTest, allocateDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockDevice = createMockDevice();
|
||||||
|
const auto device = Device::create(kName, mockDevice).value();
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_3::utils
|
43
neuralnetworks/1.3/utils/test/MockBuffer.h
Normal file
43
neuralnetworks/1.3/utils/test/MockBuffer.h
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_BUFFER
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_BUFFER
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IBuffer.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_3::utils {
|
||||||
|
|
||||||
|
class MockBuffer final : public IBuffer {
|
||||||
|
public:
|
||||||
|
static sp<MockBuffer> create();
|
||||||
|
|
||||||
|
// V1_3 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_3::ErrorStatus>, copyTo, (const hidl_memory& dst), (override));
|
||||||
|
MOCK_METHOD(Return<V1_3::ErrorStatus>, copyFrom,
|
||||||
|
(const hidl_memory& src, const hidl_vec<uint32_t>& dimensions), (override));
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockBuffer> MockBuffer::create() {
|
||||||
|
return sp<MockBuffer>::make();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_3::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_BUFFER
|
139
neuralnetworks/1.3/utils/test/MockDevice.h
Normal file
139
neuralnetworks/1.3/utils/test/MockDevice.h
Normal file
|
@ -0,0 +1,139 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_DEVICE
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_DEVICE
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IDevice.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_3::utils {
|
||||||
|
|
||||||
|
using CacheToken =
|
||||||
|
hidl_array<uint8_t, static_cast<uint32_t>(V1_2::Constant::BYTE_SIZE_OF_CACHE_TOKEN)>;
|
||||||
|
|
||||||
|
class MockDevice final : public IDevice {
|
||||||
|
public:
|
||||||
|
static sp<MockDevice> create();
|
||||||
|
|
||||||
|
// IBase methods below.
|
||||||
|
MOCK_METHOD(Return<void>, ping, (), (override));
|
||||||
|
MOCK_METHOD(Return<bool>, linkToDeathRet, ());
|
||||||
|
Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient, uint64_t /*cookie*/);
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities, (getCapabilities_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations,
|
||||||
|
(const V1_0::Model& model, getSupportedOperations_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel,
|
||||||
|
(const V1_0::Model& model, const sp<V1_0::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::DeviceStatus>, getStatus, (), (override));
|
||||||
|
|
||||||
|
// V1_1 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities_1_1, (getCapabilities_1_1_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations_1_1,
|
||||||
|
(const V1_1::Model& model, getSupportedOperations_1_1_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_1,
|
||||||
|
(const V1_1::Model& model, V1_1::ExecutionPreference preference,
|
||||||
|
const sp<V1_0::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// V1_2 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getVersionString, (getVersionString_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getType, (getType_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities_1_2, (getCapabilities_1_2_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedExtensions, (getSupportedExtensions_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations_1_2,
|
||||||
|
(const V1_2::Model& model, getSupportedOperations_1_2_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getNumberOfCacheFilesNeeded, (getNumberOfCacheFilesNeeded_cb cb),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModel_1_2,
|
||||||
|
(const V1_2::Model& model, V1_1::ExecutionPreference preference,
|
||||||
|
const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
|
||||||
|
const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, prepareModelFromCache,
|
||||||
|
(const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
|
||||||
|
const CacheToken& token, const sp<V1_2::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// V1_3 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getCapabilities_1_3, (getCapabilities_1_3_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<void>, getSupportedOperations_1_3,
|
||||||
|
(const V1_3::Model& model, getSupportedOperations_1_3_cb cb), (override));
|
||||||
|
MOCK_METHOD(Return<V1_3::ErrorStatus>, prepareModel_1_3,
|
||||||
|
(const V1_3::Model& model, V1_1::ExecutionPreference preference,
|
||||||
|
V1_3::Priority priority, const V1_3::OptionalTimePoint& deadline,
|
||||||
|
const hidl_vec<hidl_handle>& modelCache, const hidl_vec<hidl_handle>& dataCache,
|
||||||
|
const CacheToken& token, const sp<V1_3::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<V1_3::ErrorStatus>, prepareModelFromCache_1_3,
|
||||||
|
(const V1_3::OptionalTimePoint& deadline, const hidl_vec<hidl_handle>& modelCache,
|
||||||
|
const hidl_vec<hidl_handle>& dataCache, const CacheToken& token,
|
||||||
|
const sp<V1_3::IPreparedModelCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<void>, allocate,
|
||||||
|
(const V1_3::BufferDesc& desc,
|
||||||
|
const hidl_vec<sp<V1_3::IPreparedModel>>& preparedModels,
|
||||||
|
const hidl_vec<V1_3::BufferRole>& inputRoles,
|
||||||
|
const hidl_vec<V1_3::BufferRole>& outputRoles, allocate_cb cb),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// Helper methods.
|
||||||
|
void simulateCrash();
|
||||||
|
|
||||||
|
private:
|
||||||
|
sp<hidl_death_recipient> mDeathRecipient;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockDevice> MockDevice::create() {
|
||||||
|
auto mockDevice = sp<MockDevice>::make();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto ret = []() -> Return<bool> { return true; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, linkToDeathRet()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Return<bool> MockDevice::linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) {
|
||||||
|
mDeathRecipient = recipient;
|
||||||
|
return linkToDeathRet();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void MockDevice::simulateCrash() {
|
||||||
|
ASSERT_NE(nullptr, mDeathRecipient.get());
|
||||||
|
|
||||||
|
// Currently, the utils::Device will not use the `cookie` or `who` arguments, so we pass in 0
|
||||||
|
// and nullptr for these arguments instead. Normally, they are used by the hidl_death_recipient
|
||||||
|
// to determine which object is dead. However, the utils::Device code only pairs a single death
|
||||||
|
// recipient with a single HIDL interface object, so these arguments are redundant.
|
||||||
|
mDeathRecipient->serviceDied(0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_3::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_DEVICE
|
42
neuralnetworks/1.3/utils/test/MockFencedExecutionCallback.h
Normal file
42
neuralnetworks/1.3/utils/test/MockFencedExecutionCallback.h
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_3::utils {
|
||||||
|
|
||||||
|
class MockFencedExecutionCallback final : public IFencedExecutionCallback {
|
||||||
|
public:
|
||||||
|
static sp<MockFencedExecutionCallback> create();
|
||||||
|
|
||||||
|
// V1_3 methods below.
|
||||||
|
MOCK_METHOD(Return<void>, getExecutionInfo, (IFencedExecutionCallback::getExecutionInfo_cb cb),
|
||||||
|
(override));
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockFencedExecutionCallback> MockFencedExecutionCallback::create() {
|
||||||
|
return sp<MockFencedExecutionCallback>::make();
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_3::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_FENCED_EXECUTION_CALLBACK
|
121
neuralnetworks/1.3/utils/test/MockPreparedModel.h
Normal file
121
neuralnetworks/1.3/utils/test/MockPreparedModel.h
Normal file
|
@ -0,0 +1,121 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_PREPARED_MODEL
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IPreparedModel.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <hidl/HidlSupport.h>
|
||||||
|
#include <hidl/Status.h>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_3::utils {
|
||||||
|
|
||||||
|
class MockPreparedModel final : public IPreparedModel {
|
||||||
|
public:
|
||||||
|
static sp<MockPreparedModel> create();
|
||||||
|
|
||||||
|
// IBase methods below.
|
||||||
|
MOCK_METHOD(Return<void>, ping, (), (override));
|
||||||
|
MOCK_METHOD(Return<bool>, linkToDeathRet, ());
|
||||||
|
Return<bool> linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) override;
|
||||||
|
|
||||||
|
// V1_0 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, execute,
|
||||||
|
(const V1_0::Request& request, const sp<V1_0::IExecutionCallback>& callback),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// V1_2 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_0::ErrorStatus>, execute_1_2,
|
||||||
|
(const V1_0::Request& request, V1_2::MeasureTiming measure,
|
||||||
|
const sp<V1_2::IExecutionCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<void>, executeSynchronously,
|
||||||
|
(const V1_0::Request& request, V1_2::MeasureTiming measure,
|
||||||
|
executeSynchronously_cb cb),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<void>, configureExecutionBurst,
|
||||||
|
(const sp<V1_2::IBurstCallback>& callback,
|
||||||
|
const MQDescriptorSync<V1_2::FmqRequestDatum>& requestChannel,
|
||||||
|
const MQDescriptorSync<V1_2::FmqResultDatum>& resultChannel,
|
||||||
|
configureExecutionBurst_cb cb),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// V1_3 methods below.
|
||||||
|
MOCK_METHOD(Return<V1_3::ErrorStatus>, execute_1_3,
|
||||||
|
(const V1_3::Request& request, V1_2::MeasureTiming measure,
|
||||||
|
const V1_3::OptionalTimePoint& deadline,
|
||||||
|
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
|
||||||
|
const sp<V1_3::IExecutionCallback>& callback),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<void>, executeSynchronously_1_3,
|
||||||
|
(const V1_3::Request& request, V1_2::MeasureTiming measure,
|
||||||
|
const V1_3::OptionalTimePoint& deadline,
|
||||||
|
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
|
||||||
|
executeSynchronously_1_3_cb cb),
|
||||||
|
(override));
|
||||||
|
MOCK_METHOD(Return<void>, executeFenced,
|
||||||
|
(const V1_3::Request& request, const hidl_vec<hidl_handle>& waitFor,
|
||||||
|
V1_2::MeasureTiming measure, const V1_3::OptionalTimePoint& deadline,
|
||||||
|
const V1_3::OptionalTimeoutDuration& loopTimeoutDuration,
|
||||||
|
const V1_3::OptionalTimeoutDuration& duration, executeFenced_cb cb),
|
||||||
|
(override));
|
||||||
|
|
||||||
|
// Helper methods.
|
||||||
|
void simulateCrash();
|
||||||
|
|
||||||
|
private:
|
||||||
|
sp<hidl_death_recipient> mDeathRecipient;
|
||||||
|
};
|
||||||
|
|
||||||
|
inline sp<MockPreparedModel> MockPreparedModel::create() {
|
||||||
|
auto mockPreparedModel = sp<MockPreparedModel>::make();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
const auto ret = []() -> Return<bool> { return true; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockPreparedModel, linkToDeathRet()).WillByDefault(testing::Invoke(ret));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockPreparedModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Return<bool> MockPreparedModel::linkToDeath(const sp<hidl_death_recipient>& recipient,
|
||||||
|
uint64_t /*cookie*/) {
|
||||||
|
mDeathRecipient = recipient;
|
||||||
|
return linkToDeathRet();
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void MockPreparedModel::simulateCrash() {
|
||||||
|
ASSERT_NE(nullptr, mDeathRecipient.get());
|
||||||
|
|
||||||
|
// Currently, the utils::PreparedModel will not use the `cookie` or `who` arguments, so we pass
|
||||||
|
// in 0 and nullptr for these arguments instead. Normally, they are used by the
|
||||||
|
// hidl_death_recipient to determine which object is dead. However, the utils::PreparedModel
|
||||||
|
// code only pairs a single death recipient with a single HIDL interface object, so these
|
||||||
|
// arguments are redundant.
|
||||||
|
mDeathRecipient->serviceDied(0, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_3::utils
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_1_3_UTILS_TEST_MOCK_PREPARED_MODEL
|
470
neuralnetworks/1.3/utils/test/PreparedModelTest.cpp
Normal file
470
neuralnetworks/1.3/utils/test/PreparedModelTest.cpp
Normal file
|
@ -0,0 +1,470 @@
|
||||||
|
/*
|
||||||
|
* 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 "MockFencedExecutionCallback.h"
|
||||||
|
#include "MockPreparedModel.h"
|
||||||
|
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IExecutionCallback.h>
|
||||||
|
#include <android/hardware/neuralnetworks/1.3/IFencedExecutionCallback.h>
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IPreparedModel.h>
|
||||||
|
#include <nnapi/TypeUtils.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
#include <nnapi/hal/1.3/PreparedModel.h>
|
||||||
|
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::V1_3::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::Invoke;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
|
||||||
|
const sp<V1_3::IPreparedModel> kInvalidPreparedModel;
|
||||||
|
constexpr auto kNoTiming = V1_2::Timing{.timeOnDevice = std::numeric_limits<uint64_t>::max(),
|
||||||
|
.timeInDriver = std::numeric_limits<uint64_t>::max()};
|
||||||
|
|
||||||
|
sp<MockPreparedModel> createMockPreparedModel() {
|
||||||
|
const auto mockPreparedModel = MockPreparedModel::create();
|
||||||
|
|
||||||
|
// Ensure that older calls are not used.
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_2(_, _, _)).Times(0);
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously(_, _, _)).Times(0);
|
||||||
|
|
||||||
|
return mockPreparedModel;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto makeExecuteSynchronously(V1_3::ErrorStatus status,
|
||||||
|
const std::vector<V1_2::OutputShape>& outputShapes,
|
||||||
|
const V1_2::Timing& timing) {
|
||||||
|
return [status, outputShapes, timing](
|
||||||
|
const V1_3::Request& /*request*/, V1_2::MeasureTiming /*measureTiming*/,
|
||||||
|
const V1_3::OptionalTimePoint& /*deadline*/,
|
||||||
|
const V1_3::OptionalTimeoutDuration& /*loopTimeoutDuration*/,
|
||||||
|
const V1_3::IPreparedModel::executeSynchronously_1_3_cb& cb) {
|
||||||
|
cb(status, outputShapes, timing);
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto makeExecuteAsynchronously(V1_3::ErrorStatus launchStatus, V1_3::ErrorStatus returnStatus,
|
||||||
|
const std::vector<V1_2::OutputShape>& outputShapes,
|
||||||
|
const V1_2::Timing& timing) {
|
||||||
|
return [launchStatus, returnStatus, outputShapes, timing](
|
||||||
|
const V1_3::Request& /*request*/, V1_2::MeasureTiming /*measureTiming*/,
|
||||||
|
const V1_3::OptionalTimePoint& /*deadline*/,
|
||||||
|
const V1_3::OptionalTimeoutDuration& /*loopTimeoutDuration*/,
|
||||||
|
const sp<V1_3::IExecutionCallback>& cb) -> Return<V1_3::ErrorStatus> {
|
||||||
|
cb->notify_1_3(returnStatus, outputShapes, timing);
|
||||||
|
return launchStatus;
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto makeExecuteFencedReturn(V1_3::ErrorStatus status, const hardware::hidl_handle& syncFence,
|
||||||
|
const sp<V1_3::IFencedExecutionCallback>& dispatchCallback) {
|
||||||
|
return [status, syncFence, dispatchCallback](
|
||||||
|
const V1_3::Request& /*request*/,
|
||||||
|
const hardware::hidl_vec<hardware::hidl_handle>& /*waitFor*/,
|
||||||
|
V1_2::MeasureTiming /*measure*/, const V1_3::OptionalTimePoint& /*deadline*/,
|
||||||
|
const V1_3::OptionalTimeoutDuration& /*loopTimeoutDuration*/,
|
||||||
|
const V1_3::OptionalTimeoutDuration& /*duration*/,
|
||||||
|
const V1_3::IPreparedModel::executeFenced_cb& cb) {
|
||||||
|
cb(status, syncFence, dispatchCallback);
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
auto makeExecuteFencedCallbackReturn(V1_3::ErrorStatus status, const V1_2::Timing& timingA,
|
||||||
|
const V1_2::Timing& timingB) {
|
||||||
|
return [status, timingA,
|
||||||
|
timingB](const V1_3::IFencedExecutionCallback::getExecutionInfo_cb& cb) {
|
||||||
|
cb(status, timingA, timingB);
|
||||||
|
return hardware::Void();
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
std::function<hardware::Status()> makeTransportFailure(status_t status) {
|
||||||
|
return [status] { return hardware::Status::fromStatusT(status); };
|
||||||
|
}
|
||||||
|
|
||||||
|
const auto makeGeneralTransportFailure = makeTransportFailure(NO_MEMORY);
|
||||||
|
const auto makeDeadObjectFailure = makeTransportFailure(DEAD_OBJECT);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, invalidPreparedModel) {
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(kInvalidPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto ret = []() -> Return<bool> { return false; };
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet()).Times(1).WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathTransportFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, linkToDeathDeadObject) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, linkToDeathRet())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSync) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteSynchronously(V1_3::ErrorStatus::NONE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSyncError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(
|
||||||
|
makeExecuteSynchronously(V1_3::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSyncTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeSyncDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeSynchronously_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsync) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteAsynchronously(V1_3::ErrorStatus::NONE,
|
||||||
|
V1_3::ErrorStatus::NONE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncLaunchError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteAsynchronously(V1_3::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
V1_3::ErrorStatus::GENERAL_FAILURE, {},
|
||||||
|
kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncReturnError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteAsynchronously(
|
||||||
|
V1_3::ErrorStatus::NONE, V1_3::ErrorStatus::GENERAL_FAILURE, {}, kNoTiming)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeAsyncCrash) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/false).value();
|
||||||
|
const auto ret = [&mockPreparedModel]() -> hardware::Return<V1_3::ErrorStatus> {
|
||||||
|
mockPreparedModel->simulateCrash();
|
||||||
|
return V1_3::ErrorStatus::NONE;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute_1_3(_, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(ret));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeFenced) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
const auto mockCallback = MockFencedExecutionCallback::create();
|
||||||
|
EXPECT_CALL(*mockCallback, getExecutionInfo(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteFencedCallbackReturn(V1_3::ErrorStatus::NONE, kNoTiming,
|
||||||
|
kNoTiming)));
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteFencedReturn(V1_3::ErrorStatus::NONE, {}, mockCallback)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& [syncFence, callback] = result.value();
|
||||||
|
EXPECT_EQ(syncFence.syncWait({}), nn::SyncFence::FenceState::SIGNALED);
|
||||||
|
ASSERT_NE(callback, nullptr);
|
||||||
|
|
||||||
|
// get results from callback
|
||||||
|
const auto callbackResult = callback();
|
||||||
|
ASSERT_TRUE(callbackResult.has_value()) << "Failed with " << callbackResult.error().code << ": "
|
||||||
|
<< callbackResult.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeFencedCallbackError) {
|
||||||
|
// setup call
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
const auto mockCallback = MockFencedExecutionCallback::create();
|
||||||
|
EXPECT_CALL(*mockCallback, getExecutionInfo(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteFencedCallbackReturn(V1_3::ErrorStatus::GENERAL_FAILURE,
|
||||||
|
kNoTiming, kNoTiming)));
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(makeExecuteFencedReturn(V1_3::ErrorStatus::NONE, {}, mockCallback)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
const auto& [syncFence, callback] = result.value();
|
||||||
|
EXPECT_NE(syncFence.syncWait({}), nn::SyncFence::FenceState::ACTIVE);
|
||||||
|
ASSERT_NE(callback, nullptr);
|
||||||
|
|
||||||
|
// verify callback failure
|
||||||
|
const auto callbackResult = callback();
|
||||||
|
ASSERT_FALSE(callbackResult.has_value());
|
||||||
|
EXPECT_EQ(callbackResult.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeFencedError) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Invoke(
|
||||||
|
makeExecuteFencedReturn(V1_3::ErrorStatus::GENERAL_FAILURE, {}, nullptr)));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeFencedTransportFailure) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeGeneralTransportFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, executeFencedDeadObject) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(InvokeWithoutArgs(makeDeadObjectFailure));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
// TODO: test burst execution if/when it is added to nn::IPreparedModel.
|
||||||
|
|
||||||
|
TEST(PreparedModelTest, getUnderlyingResource) {
|
||||||
|
// setup test
|
||||||
|
const auto mockPreparedModel = createMockPreparedModel();
|
||||||
|
const auto preparedModel =
|
||||||
|
PreparedModel::create(mockPreparedModel, /*executeSynchronously=*/true).value();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto resource = preparedModel->getUnderlyingResource();
|
||||||
|
|
||||||
|
// verify resource
|
||||||
|
const sp<V1_3::IPreparedModel>* maybeMock = std::any_cast<sp<V1_3::IPreparedModel>>(&resource);
|
||||||
|
ASSERT_NE(maybeMock, nullptr);
|
||||||
|
EXPECT_EQ(maybeMock->get(), mockPreparedModel.get());
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::V1_3::utils
|
|
@ -1,5 +1,20 @@
|
||||||
{
|
{
|
||||||
"presubmit": [
|
"presubmit": [
|
||||||
|
{
|
||||||
|
"name": "neuralnetworks_utils_hal_common_test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "neuralnetworks_utils_hal_1_0_test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "neuralnetworks_utils_hal_1_1_test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "neuralnetworks_utils_hal_1_2_test"
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"name": "neuralnetworks_utils_hal_1_3_test"
|
||||||
|
},
|
||||||
{
|
{
|
||||||
"name": "VtsHalNeuralnetworksV1_0TargetTest",
|
"name": "VtsHalNeuralnetworksV1_0TargetTest",
|
||||||
"options": [
|
"options": [
|
||||||
|
|
|
@ -28,3 +28,28 @@ cc_library_static {
|
||||||
"libhidlbase",
|
"libhidlbase",
|
||||||
],
|
],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
cc_test {
|
||||||
|
name: "neuralnetworks_utils_hal_common_test",
|
||||||
|
srcs: ["test/*.cpp"],
|
||||||
|
static_libs: [
|
||||||
|
"android.hardware.neuralnetworks@1.0",
|
||||||
|
"libgmock",
|
||||||
|
"libneuralnetworks_common",
|
||||||
|
"neuralnetworks_types",
|
||||||
|
"neuralnetworks_utils_hal_common",
|
||||||
|
],
|
||||||
|
shared_libs: [
|
||||||
|
"android.hidl.allocator@1.0",
|
||||||
|
"android.hidl.memory@1.0",
|
||||||
|
"libbase",
|
||||||
|
"libcutils",
|
||||||
|
"libfmq",
|
||||||
|
"libhidlbase",
|
||||||
|
"libhidlmemory",
|
||||||
|
"liblog",
|
||||||
|
"libnativewindow",
|
||||||
|
"libutils",
|
||||||
|
],
|
||||||
|
test_suites: ["general-tests"],
|
||||||
|
}
|
||||||
|
|
37
neuralnetworks/utils/common/test/MockBuffer.h
Normal file
37
neuralnetworks/utils/common/test/MockBuffer.h
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_BUFFER
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_BUFFER
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IBuffer.h>
|
||||||
|
#include <nnapi/Types.h>
|
||||||
|
|
||||||
|
namespace android::nn {
|
||||||
|
|
||||||
|
class MockBuffer final : public IBuffer {
|
||||||
|
public:
|
||||||
|
MOCK_METHOD(Request::MemoryDomainToken, getToken, (), (const, override));
|
||||||
|
MOCK_METHOD(GeneralResult<void>, copyTo, (const Memory& dst), (const, override));
|
||||||
|
MOCK_METHOD(GeneralResult<void>, copyFrom, (const Memory& src, const Dimensions& dimensions),
|
||||||
|
(const, override));
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace android::nn
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_BUFFER
|
57
neuralnetworks/utils/common/test/MockDevice.h
Normal file
57
neuralnetworks/utils/common/test/MockDevice.h
Normal file
|
@ -0,0 +1,57 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_DEVICE
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_DEVICE
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IDevice.h>
|
||||||
|
|
||||||
|
namespace android::nn {
|
||||||
|
|
||||||
|
class MockDevice final : public IDevice {
|
||||||
|
public:
|
||||||
|
MOCK_METHOD(const std::string&, getName, (), (const, override));
|
||||||
|
MOCK_METHOD(const std::string&, getVersionString, (), (const, override));
|
||||||
|
MOCK_METHOD(Version, getFeatureLevel, (), (const, override));
|
||||||
|
MOCK_METHOD(DeviceType, getType, (), (const, override));
|
||||||
|
MOCK_METHOD(const std::vector<Extension>&, getSupportedExtensions, (), (const, override));
|
||||||
|
MOCK_METHOD(const Capabilities&, getCapabilities, (), (const, override));
|
||||||
|
MOCK_METHOD((std::pair<uint32_t, uint32_t>), getNumberOfCacheFilesNeeded, (),
|
||||||
|
(const, override));
|
||||||
|
MOCK_METHOD(GeneralResult<void>, wait, (), (const, override));
|
||||||
|
MOCK_METHOD(GeneralResult<std::vector<bool>>, getSupportedOperations, (const Model& model),
|
||||||
|
(const, override));
|
||||||
|
MOCK_METHOD(GeneralResult<SharedPreparedModel>, prepareModel,
|
||||||
|
(const Model& model, ExecutionPreference preference, Priority priority,
|
||||||
|
OptionalTimePoint deadline, const std::vector<SharedHandle>& modelCache,
|
||||||
|
const std::vector<SharedHandle>& dataCache, const CacheToken& token),
|
||||||
|
(const, override));
|
||||||
|
MOCK_METHOD(GeneralResult<SharedPreparedModel>, prepareModelFromCache,
|
||||||
|
(OptionalTimePoint deadline, const std::vector<SharedHandle>& modelCache,
|
||||||
|
const std::vector<SharedHandle>& dataCache, const CacheToken& token),
|
||||||
|
(const, override));
|
||||||
|
MOCK_METHOD(GeneralResult<SharedBuffer>, allocate,
|
||||||
|
(const BufferDesc& desc, const std::vector<SharedPreparedModel>& preparedModels,
|
||||||
|
const std::vector<BufferRole>& inputRoles,
|
||||||
|
const std::vector<BufferRole>& outputRoles),
|
||||||
|
(const, override));
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace android::nn
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_DEVICE
|
43
neuralnetworks/utils/common/test/MockPreparedModel.h
Normal file
43
neuralnetworks/utils/common/test/MockPreparedModel.h
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
/*
|
||||||
|
* 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.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_PREPARED_MODEL
|
||||||
|
#define ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_PREPARED_MODEL
|
||||||
|
|
||||||
|
#include <gmock/gmock.h>
|
||||||
|
#include <gtest/gtest.h>
|
||||||
|
#include <nnapi/IPreparedModel.h>
|
||||||
|
|
||||||
|
namespace android::nn {
|
||||||
|
|
||||||
|
class MockPreparedModel final : public IPreparedModel {
|
||||||
|
public:
|
||||||
|
MOCK_METHOD((ExecutionResult<std::pair<std::vector<OutputShape>, Timing>>), execute,
|
||||||
|
(const Request& request, MeasureTiming measure, const OptionalTimePoint& deadline,
|
||||||
|
const OptionalDuration& loopTimeoutDuration),
|
||||||
|
(const, override));
|
||||||
|
MOCK_METHOD((GeneralResult<std::pair<SyncFence, ExecuteFencedInfoCallback>>), executeFenced,
|
||||||
|
(const Request& request, const std::vector<SyncFence>& waitFor,
|
||||||
|
MeasureTiming measure, const OptionalTimePoint& deadline,
|
||||||
|
const OptionalDuration& loopTimeoutDuration,
|
||||||
|
const OptionalDuration& timeoutDurationAfterFence),
|
||||||
|
(const, override));
|
||||||
|
MOCK_METHOD(std::any, getUnderlyingResource, (), (const, override));
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace android::nn
|
||||||
|
|
||||||
|
#endif // ANDROID_HARDWARE_INTERFACES_NEURALNETWORKS_UTILS_COMMON_TEST_MOCK_PREPARED_MODEL
|
266
neuralnetworks/utils/common/test/ResilientBufferTest.cpp
Normal file
266
neuralnetworks/utils/common/test/ResilientBufferTest.cpp
Normal file
|
@ -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
|
725
neuralnetworks/utils/common/test/ResilientDeviceTest.cpp
Normal file
725
neuralnetworks/utils/common/test/ResilientDeviceTest.cpp
Normal file
|
@ -0,0 +1,725 @@
|
||||||
|
/*
|
||||||
|
* 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/ResilientDevice.h>
|
||||||
|
#include <tuple>
|
||||||
|
#include <utility>
|
||||||
|
#include "MockBuffer.h"
|
||||||
|
#include "MockDevice.h"
|
||||||
|
#include "MockPreparedModel.h"
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
using ::testing::Return;
|
||||||
|
|
||||||
|
using SharedMockDevice = std::shared_ptr<const nn::MockDevice>;
|
||||||
|
using MockDeviceFactory = ::testing::MockFunction<nn::GeneralResult<nn::SharedDevice>(bool)>;
|
||||||
|
|
||||||
|
const std::string kName = "Google-MockV1";
|
||||||
|
const std::string kVersionString = "version1";
|
||||||
|
const auto kExtensions = std::vector<nn::Extension>{};
|
||||||
|
constexpr auto kNoInfo = std::numeric_limits<float>::max();
|
||||||
|
constexpr auto kNoPerformanceInfo =
|
||||||
|
nn::Capabilities::PerformanceInfo{.execTime = kNoInfo, .powerUsage = kNoInfo};
|
||||||
|
const auto kCapabilities = nn::Capabilities{
|
||||||
|
.relaxedFloat32toFloat16PerformanceScalar = kNoPerformanceInfo,
|
||||||
|
.relaxedFloat32toFloat16PerformanceTensor = kNoPerformanceInfo,
|
||||||
|
.operandPerformance = nn::Capabilities::OperandPerformanceTable::create({}).value(),
|
||||||
|
.ifPerformance = kNoPerformanceInfo,
|
||||||
|
.whilePerformance = kNoPerformanceInfo};
|
||||||
|
constexpr auto kNumberOfCacheFilesNeeded = std::pair<uint32_t, uint32_t>(5, 3);
|
||||||
|
|
||||||
|
SharedMockDevice createConfiguredMockDevice() {
|
||||||
|
auto mockDevice = std::make_shared<const nn::MockDevice>();
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
constexpr auto getName_ret = []() -> const std::string& { return kName; };
|
||||||
|
constexpr auto getVersionString_ret = []() -> const std::string& { return kVersionString; };
|
||||||
|
constexpr auto kFeatureLevel = nn::Version::ANDROID_OC_MR1;
|
||||||
|
constexpr auto kDeviceType = nn::DeviceType::ACCELERATOR;
|
||||||
|
constexpr auto getSupportedExtensions_ret = []() -> const std::vector<nn::Extension>& {
|
||||||
|
return kExtensions;
|
||||||
|
};
|
||||||
|
constexpr auto getCapabilities_ret = []() -> const nn::Capabilities& { return kCapabilities; };
|
||||||
|
|
||||||
|
// Setup default actions for each relevant call.
|
||||||
|
ON_CALL(*mockDevice, getName()).WillByDefault(getName_ret);
|
||||||
|
ON_CALL(*mockDevice, getVersionString()).WillByDefault(getVersionString_ret);
|
||||||
|
ON_CALL(*mockDevice, getFeatureLevel()).WillByDefault(Return(kFeatureLevel));
|
||||||
|
ON_CALL(*mockDevice, getType()).WillByDefault(Return(kDeviceType));
|
||||||
|
ON_CALL(*mockDevice, getSupportedExtensions()).WillByDefault(getSupportedExtensions_ret);
|
||||||
|
ON_CALL(*mockDevice, getCapabilities()).WillByDefault(getCapabilities_ret);
|
||||||
|
ON_CALL(*mockDevice, getNumberOfCacheFilesNeeded())
|
||||||
|
.WillByDefault(Return(kNumberOfCacheFilesNeeded));
|
||||||
|
|
||||||
|
// These EXPECT_CALL(...).Times(testing::AnyNumber()) calls are to suppress warnings on the
|
||||||
|
// uninteresting methods calls.
|
||||||
|
EXPECT_CALL(*mockDevice, getName()).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getVersionString()).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getFeatureLevel()).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getType()).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedExtensions()).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getCapabilities()).Times(testing::AnyNumber());
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded()).Times(testing::AnyNumber());
|
||||||
|
|
||||||
|
return mockDevice;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<SharedMockDevice, std::unique_ptr<MockDeviceFactory>,
|
||||||
|
std::shared_ptr<const ResilientDevice>>
|
||||||
|
setup() {
|
||||||
|
auto mockDevice = createConfiguredMockDevice();
|
||||||
|
|
||||||
|
auto mockDeviceFactory = std::make_unique<MockDeviceFactory>();
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(true)).Times(1).WillOnce(Return(mockDevice));
|
||||||
|
|
||||||
|
auto device = ResilientDevice::create(mockDeviceFactory->AsStdFunction()).value();
|
||||||
|
return std::make_tuple(std::move(mockDevice), std::move(mockDeviceFactory), std::move(device));
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, invalidDeviceFactory) {
|
||||||
|
// setup call
|
||||||
|
const auto invalidDeviceFactory = ResilientDevice::Factory{};
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = ResilientDevice::create(invalidDeviceFactory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, preparedModelFactoryFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto invalidDeviceFactory = kReturnGeneralFailure;
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = ResilientDevice::create(invalidDeviceFactory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, cachedData) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
|
||||||
|
// run test and verify results
|
||||||
|
EXPECT_EQ(device->getName(), kName);
|
||||||
|
EXPECT_EQ(device->getVersionString(), kVersionString);
|
||||||
|
EXPECT_EQ(device->getSupportedExtensions(), kExtensions);
|
||||||
|
EXPECT_EQ(device->getCapabilities(), kCapabilities);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getFeatureLevel) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
constexpr auto kFeatureLevel = nn::Version::ANDROID_OC_MR1;
|
||||||
|
EXPECT_CALL(*mockDevice, getFeatureLevel()).Times(1).WillOnce(Return(kFeatureLevel));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto featureLevel = device->getFeatureLevel();
|
||||||
|
|
||||||
|
// verify results
|
||||||
|
EXPECT_EQ(featureLevel, kFeatureLevel);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getType) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
constexpr auto kDeviceType = nn::DeviceType::ACCELERATOR;
|
||||||
|
EXPECT_CALL(*mockDevice, getType()).Times(1).WillOnce(Return(kDeviceType));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto type = device->getType();
|
||||||
|
|
||||||
|
// verify results
|
||||||
|
EXPECT_EQ(type, kDeviceType);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getNumberOfCacheFilesNeeded) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, getNumberOfCacheFilesNeeded())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(kNumberOfCacheFilesNeeded));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto numberOfCacheFilesNeeded = device->getNumberOfCacheFilesNeeded();
|
||||||
|
|
||||||
|
// verify results
|
||||||
|
EXPECT_EQ(numberOfCacheFilesNeeded, kNumberOfCacheFilesNeeded);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getDevice) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getDevice();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result == mockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, wait) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, wait()).Times(1).WillOnce(Return(nn::GeneralResult<void>{}));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, waitError) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, wait()).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, waitDeadObjectFailedRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, wait()).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(true)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, waitDeadObjectSuccessfulRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, wait()).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, wait()).Times(1).WillOnce(Return(nn::GeneralResult<void>{}));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(true)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->wait();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getSupportedOperations) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(nn::GeneralResult<std::vector<bool>>{}));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations({});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getSupportedOperationsError) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations({});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getSupportedOperationsDeadObjectFailedRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_)).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations({});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, getSupportedOperationsDeadObjectSuccessfulRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, getSupportedOperations(_)).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getSupportedOperations(_))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(nn::GeneralResult<std::vector<bool>>{}));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->getSupportedOperations({});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModel) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto mockPreparedModel = std::make_shared<const nn::MockPreparedModel>();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(mockPreparedModel));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel({}, {}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModelError) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel({}, {}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModelDeadObjectFailedRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnDeadObject);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel({}, {}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModelDeadObjectSuccessfulRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModel(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnDeadObject);
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
const auto mockPreparedModel = std::make_shared<const nn::MockPreparedModel>();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, prepareModel(_, _, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(mockPreparedModel));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModel({}, {}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModelFromCache) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto mockPreparedModel = std::make_shared<const nn::MockPreparedModel>();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(mockPreparedModel));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModelFromCacheError) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModelFromCacheDeadObjectFailedRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnDeadObject);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, prepareModelFromCacheDeadObjectSuccessfulRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnDeadObject);
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
const auto mockPreparedModel = std::make_shared<const nn::MockPreparedModel>();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, prepareModelFromCache(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(mockPreparedModel));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, allocate) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto mockBuffer = std::make_shared<const nn::MockBuffer>();
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _)).Times(1).WillOnce(Return(mockBuffer));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, allocateError) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, allocateDeadObjectFailedRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _)).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, allocateDeadObjectSuccessfulRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
EXPECT_CALL(*mockDevice, allocate(_, _, _, _)).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
const auto mockBuffer = std::make_shared<const nn::MockBuffer>();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, allocate(_, _, _, _)).Times(1).WillOnce(Return(mockBuffer));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recover) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() == recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(_)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_FALSE(result.has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, someoneElseRecovered) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() == recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchGetName) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
const std::string kDifferentName = "Google-DifferentName";
|
||||||
|
const auto ret = [&kDifferentName]() -> const std::string& { return kDifferentName; };
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getName()).Times(1).WillOnce(ret);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
EXPECT_TRUE(result.value() != mockDevice);
|
||||||
|
EXPECT_TRUE(result.value() != recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchGetVersionString) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
const std::string kDifferentVersionString = "differentversion";
|
||||||
|
const auto ret = [&kDifferentVersionString]() -> const std::string& {
|
||||||
|
return kDifferentVersionString;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getVersionString()).Times(1).WillOnce(ret);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
EXPECT_TRUE(result.value() != mockDevice);
|
||||||
|
EXPECT_TRUE(result.value() != recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchGetFeatureLevel) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getFeatureLevel())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(nn::Version::ANDROID_P));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
EXPECT_TRUE(result.value() != mockDevice);
|
||||||
|
EXPECT_TRUE(result.value() != recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchGetType) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getType()).Times(1).WillOnce(Return(nn::DeviceType::GPU));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
EXPECT_TRUE(result.value() != mockDevice);
|
||||||
|
EXPECT_TRUE(result.value() != recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchGetSupportedExtensions) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
const auto kDifferentExtensions =
|
||||||
|
std::vector<nn::Extension>{nn::Extension{.name = "", .operandTypes = {}}};
|
||||||
|
const auto ret = [&kDifferentExtensions]() -> const std::vector<nn::Extension>& {
|
||||||
|
return kDifferentExtensions;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getSupportedExtensions()).Times(1).WillOnce(ret);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
EXPECT_TRUE(result.value() != mockDevice);
|
||||||
|
EXPECT_TRUE(result.value() != recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchGetCapabilities) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
const auto kDifferentCapabilities = nn::Capabilities{
|
||||||
|
.relaxedFloat32toFloat16PerformanceTensor = {.execTime = 0.5f, .powerUsage = 0.5f},
|
||||||
|
.operandPerformance = nn::Capabilities::OperandPerformanceTable::create({}).value()};
|
||||||
|
const auto ret = [&kDifferentCapabilities]() -> const nn::Capabilities& {
|
||||||
|
return kDifferentCapabilities;
|
||||||
|
};
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getCapabilities()).Times(1).WillOnce(ret);
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
EXPECT_TRUE(result.value() != mockDevice);
|
||||||
|
EXPECT_TRUE(result.value() != recoveredMockDevice);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchInvalidPrepareModel) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getType()).Times(1).WillOnce(Return(nn::DeviceType::GPU));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
auto result = device->prepareModel({}, {}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchInvalidPrepareModelFromCache) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getType()).Times(1).WillOnce(Return(nn::DeviceType::GPU));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
auto result = device->prepareModelFromCache({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientDeviceTest, recoverCacheMismatchInvalidAllocate) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockDevice, mockDeviceFactory, device] = setup();
|
||||||
|
const auto recoveredMockDevice = createConfiguredMockDevice();
|
||||||
|
EXPECT_CALL(*recoveredMockDevice, getType()).Times(1).WillOnce(Return(nn::DeviceType::GPU));
|
||||||
|
EXPECT_CALL(*mockDeviceFactory, Call(false)).Times(1).WillOnce(Return(recoveredMockDevice));
|
||||||
|
device->recover(mockDevice.get(), /*blocking=*/false);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
auto result = device->allocate({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() != nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::utils
|
297
neuralnetworks/utils/common/test/ResilientPreparedModelTest.cpp
Normal file
297
neuralnetworks/utils/common/test/ResilientPreparedModelTest.cpp
Normal file
|
@ -0,0 +1,297 @@
|
||||||
|
/*
|
||||||
|
* 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/ResilientPreparedModel.h>
|
||||||
|
#include <utility>
|
||||||
|
#include "MockPreparedModel.h"
|
||||||
|
|
||||||
|
namespace android::hardware::neuralnetworks::utils {
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
using ::testing::_;
|
||||||
|
using ::testing::InvokeWithoutArgs;
|
||||||
|
using ::testing::Return;
|
||||||
|
|
||||||
|
using SharedMockPreparedModel = std::shared_ptr<const nn::MockPreparedModel>;
|
||||||
|
using MockPreparedModelFactory =
|
||||||
|
::testing::MockFunction<nn::GeneralResult<nn::SharedPreparedModel>()>;
|
||||||
|
|
||||||
|
SharedMockPreparedModel createConfiguredMockPreparedModel() {
|
||||||
|
return std::make_shared<const nn::MockPreparedModel>();
|
||||||
|
}
|
||||||
|
|
||||||
|
std::tuple<std::shared_ptr<const nn::MockPreparedModel>, std::unique_ptr<MockPreparedModelFactory>,
|
||||||
|
std::shared_ptr<const ResilientPreparedModel>>
|
||||||
|
setup() {
|
||||||
|
auto mockPreparedModel = std::make_shared<const nn::MockPreparedModel>();
|
||||||
|
|
||||||
|
auto mockPreparedModelFactory = std::make_unique<MockPreparedModelFactory>();
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call()).Times(1).WillOnce(Return(mockPreparedModel));
|
||||||
|
|
||||||
|
auto buffer = ResilientPreparedModel::create(mockPreparedModelFactory->AsStdFunction()).value();
|
||||||
|
return std::make_tuple(std::move(mockPreparedModel), std::move(mockPreparedModelFactory),
|
||||||
|
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 kNoExecutionError =
|
||||||
|
nn::ExecutionResult<std::pair<std::vector<nn::OutputShape>, nn::Timing>>{};
|
||||||
|
const auto kNoFencedExecutionError =
|
||||||
|
nn::GeneralResult<std::pair<nn::SyncFence, nn::ExecuteFencedInfoCallback>>(
|
||||||
|
std::make_pair(nn::SyncFence::createAsSignaled(), nullptr));
|
||||||
|
|
||||||
|
struct FakeResource {};
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, invalidPreparedModelFactory) {
|
||||||
|
// setup call
|
||||||
|
const auto invalidPreparedModelFactory = ResilientPreparedModel::Factory{};
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = ResilientPreparedModel::create(invalidPreparedModelFactory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::INVALID_ARGUMENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, preparedModelFactoryFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto invalidPreparedModelFactory = kReturnGeneralFailure;
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = ResilientPreparedModel::create(invalidPreparedModelFactory);
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, getPreparedModel) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->getPreparedModel();
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_TRUE(result == mockPreparedModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, execute) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(kNoExecutionError));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, executeError) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _, _, _)).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, executeDeadObjectFailedRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _, _, _)).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
constexpr auto ret = [] { return nn::error(nn::ErrorStatus::GENERAL_FAILURE); };
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call()).Times(1).WillOnce(ret);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, executeDeadObjectSuccessfulRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, execute(_, _, _, _)).Times(1).WillOnce(kReturnDeadObject);
|
||||||
|
const auto recoveredMockPreparedModel = createConfiguredMockPreparedModel();
|
||||||
|
EXPECT_CALL(*recoveredMockPreparedModel, execute(_, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(kNoExecutionError));
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(recoveredMockPreparedModel));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->execute({}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, executeFenced) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(kNoFencedExecutionError));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, executeFencedError) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::GENERAL_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, executeFencedDeadObjectFailedRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnDeadObject);
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_FALSE(result.has_value());
|
||||||
|
EXPECT_EQ(result.error().code, nn::ErrorStatus::DEAD_OBJECT);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, executeFencedDeadObjectSuccessfulRecovery) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, executeFenced(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(kReturnDeadObject);
|
||||||
|
const auto recoveredMockPreparedModel = createConfiguredMockPreparedModel();
|
||||||
|
EXPECT_CALL(*recoveredMockPreparedModel, executeFenced(_, _, _, _, _, _))
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(kNoFencedExecutionError));
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(recoveredMockPreparedModel));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->executeFenced({}, {}, {}, {}, {}, {});
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, getUnderlyingResource) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
EXPECT_CALL(*mockPreparedModel, getUnderlyingResource())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(FakeResource{}));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto resource = preparedModel->getUnderlyingResource();
|
||||||
|
|
||||||
|
// verify resource
|
||||||
|
const FakeResource* maybeFakeResource = std::any_cast<FakeResource>(&resource);
|
||||||
|
EXPECT_NE(maybeFakeResource, nullptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, recover) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
const auto recoveredMockPreparedModel = createConfiguredMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(recoveredMockPreparedModel));
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->recover(mockPreparedModel.get());
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() == recoveredMockPreparedModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, recoverFailure) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
const auto recoveredMockPreparedModel = createConfiguredMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call()).Times(1).WillOnce(kReturnGeneralFailure);
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->recover(mockPreparedModel.get());
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
EXPECT_FALSE(result.has_value());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(ResilientPreparedModelTest, someoneElseRecovered) {
|
||||||
|
// setup call
|
||||||
|
const auto [mockPreparedModel, mockPreparedModelFactory, preparedModel] = setup();
|
||||||
|
const auto recoveredMockPreparedModel = createConfiguredMockPreparedModel();
|
||||||
|
EXPECT_CALL(*mockPreparedModelFactory, Call())
|
||||||
|
.Times(1)
|
||||||
|
.WillOnce(Return(recoveredMockPreparedModel));
|
||||||
|
preparedModel->recover(mockPreparedModel.get());
|
||||||
|
|
||||||
|
// run test
|
||||||
|
const auto result = preparedModel->recover(mockPreparedModel.get());
|
||||||
|
|
||||||
|
// verify result
|
||||||
|
ASSERT_TRUE(result.has_value())
|
||||||
|
<< "Failed with " << result.error().code << ": " << result.error().message;
|
||||||
|
EXPECT_TRUE(result.value() == recoveredMockPreparedModel);
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace android::hardware::neuralnetworks::utils
|
Loading…
Reference in a new issue