2019-06-17 01:03:25 +02:00
|
|
|
// Copyright 2019 The MediaPipe Authors.
|
|
|
|
//
|
|
|
|
// 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 "mediapipe/framework/graph_service.h"
|
|
|
|
|
2023-05-18 00:48:18 +02:00
|
|
|
#include <type_traits>
|
|
|
|
|
2019-06-17 01:03:25 +02:00
|
|
|
#include "mediapipe/framework/calculator_contract.h"
|
|
|
|
#include "mediapipe/framework/calculator_framework.h"
|
|
|
|
#include "mediapipe/framework/port/canonical_errors.h"
|
|
|
|
#include "mediapipe/framework/port/gmock.h"
|
|
|
|
#include "mediapipe/framework/port/gtest.h"
|
|
|
|
#include "mediapipe/framework/port/logging.h"
|
|
|
|
#include "mediapipe/framework/port/parse_text_proto.h"
|
|
|
|
#include "mediapipe/framework/port/ret_check.h"
|
|
|
|
#include "mediapipe/framework/port/status.h"
|
|
|
|
#include "mediapipe/framework/port/status_matchers.h"
|
|
|
|
#include "mediapipe/framework/test_service.h"
|
|
|
|
|
|
|
|
namespace mediapipe {
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
using ::testing::HasSubstr;
|
|
|
|
using ::testing::Key;
|
|
|
|
using ::testing::UnorderedElementsAre;
|
|
|
|
|
|
|
|
// Returns the packet values for a vector of Packets.
|
|
|
|
template <typename T>
|
|
|
|
std::vector<T> PacketValues(const std::vector<Packet>& packets) {
|
|
|
|
std::vector<T> result;
|
|
|
|
for (const Packet& packet : packets) {
|
|
|
|
result.push_back(packet.Get<T>());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
class GraphServiceTest : public ::testing::Test {
|
|
|
|
protected:
|
|
|
|
void SetUp() override {
|
|
|
|
CalculatorGraphConfig config =
|
2021-04-01 01:20:27 +02:00
|
|
|
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
|
2019-06-17 01:03:25 +02:00
|
|
|
input_stream: "in"
|
|
|
|
node {
|
|
|
|
calculator: "TestServiceCalculator"
|
|
|
|
input_stream: "in"
|
|
|
|
output_stream: "out"
|
|
|
|
}
|
2021-04-01 01:20:27 +02:00
|
|
|
)pb");
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_ASSERT_OK(graph_.Initialize(config));
|
|
|
|
MP_ASSERT_OK(
|
2019-06-17 01:03:25 +02:00
|
|
|
graph_.ObserveOutputStream("out", [this](const Packet& packet) {
|
|
|
|
output_packets_.push_back(packet);
|
2021-02-27 09:21:16 +01:00
|
|
|
return absl::OkStatus();
|
2019-06-17 01:03:25 +02:00
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
CalculatorGraph graph_;
|
|
|
|
std::vector<Packet> output_packets_;
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(GraphServiceTest, SetOnGraph) {
|
|
|
|
EXPECT_EQ(graph_.GetServiceObject(kTestService).get(), nullptr);
|
|
|
|
auto service_object =
|
|
|
|
std::make_shared<TestServiceObject>(TestServiceObject{{"delta", 3}});
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(graph_.SetServiceObject(kTestService, service_object));
|
2019-06-17 01:03:25 +02:00
|
|
|
EXPECT_EQ(graph_.GetServiceObject(kTestService), service_object);
|
|
|
|
|
|
|
|
service_object = std::make_shared<TestServiceObject>(
|
|
|
|
TestServiceObject{{"delta", 5}, {"count", 0}});
|
|
|
|
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(graph_.SetServiceObject(kTestService, service_object));
|
2019-06-17 01:03:25 +02:00
|
|
|
EXPECT_EQ(graph_.GetServiceObject(kTestService), service_object);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GraphServiceTest, UseInCalculator) {
|
|
|
|
auto service_object = std::make_shared<TestServiceObject>(
|
|
|
|
TestServiceObject{{"delta", 5}, {"count", 0}});
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(graph_.SetServiceObject(kTestService, service_object));
|
2019-06-17 01:03:25 +02:00
|
|
|
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_ASSERT_OK(graph_.StartRun({}));
|
|
|
|
MP_ASSERT_OK(
|
2019-06-17 01:03:25 +02:00
|
|
|
graph_.AddPacketToInputStream("in", MakePacket<int>(3).At(Timestamp(0))));
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_ASSERT_OK(graph_.CloseAllInputStreams());
|
|
|
|
MP_ASSERT_OK(graph_.WaitUntilDone());
|
2019-06-17 01:03:25 +02:00
|
|
|
EXPECT_EQ(PacketValues<int>(output_packets_), (std::vector<int>{8}));
|
|
|
|
EXPECT_EQ(1, (*service_object)["count"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GraphServiceTest, Contract) {
|
|
|
|
const CalculatorGraphConfig::Node node =
|
2021-04-01 01:20:27 +02:00
|
|
|
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig::Node>(R"pb(
|
2019-06-17 01:03:25 +02:00
|
|
|
calculator: "TestServiceCalculator"
|
|
|
|
input_stream: "in"
|
|
|
|
output_stream: "out"
|
2021-04-01 01:20:27 +02:00
|
|
|
)pb");
|
2019-06-17 01:03:25 +02:00
|
|
|
CalculatorContract contract;
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(contract.Initialize(node));
|
|
|
|
MP_EXPECT_OK(TestServiceCalculator::GetContract(&contract));
|
2019-06-17 01:03:25 +02:00
|
|
|
EXPECT_THAT(
|
|
|
|
contract.ServiceRequests(),
|
|
|
|
UnorderedElementsAre(Key(kTestService.key), Key(kAnotherService.key)));
|
|
|
|
EXPECT_EQ(contract.ServiceRequests().at(kTestService.key).IsOptional(),
|
|
|
|
false);
|
|
|
|
EXPECT_EQ(contract.ServiceRequests().at(kAnotherService.key).IsOptional(),
|
|
|
|
true);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GraphServiceTest, MustHaveRequired) {
|
|
|
|
// Do not provide any service.
|
|
|
|
auto status = graph_.StartRun({});
|
|
|
|
EXPECT_THAT(status.message(), HasSubstr(kTestService.key));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GraphServiceTest, OptionalIsOptional) {
|
|
|
|
// Provide only required service.
|
|
|
|
auto service_object = std::make_shared<TestServiceObject>(
|
|
|
|
TestServiceObject{{"delta", 5}, {"count", 0}});
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(graph_.SetServiceObject(kTestService, service_object));
|
2019-06-17 01:03:25 +02:00
|
|
|
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(graph_.StartRun({}));
|
|
|
|
MP_ASSERT_OK(
|
2019-06-17 01:03:25 +02:00
|
|
|
graph_.AddPacketToInputStream("in", MakePacket<int>(3).At(Timestamp(0))));
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_ASSERT_OK(graph_.CloseAllInputStreams());
|
|
|
|
MP_ASSERT_OK(graph_.WaitUntilDone());
|
2019-06-17 01:03:25 +02:00
|
|
|
EXPECT_EQ(PacketValues<int>(output_packets_), (std::vector<int>{8}));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(GraphServiceTest, OptionalIsAvailable) {
|
|
|
|
auto service_object = std::make_shared<TestServiceObject>(
|
|
|
|
TestServiceObject{{"delta", 5}, {"count", 0}});
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(graph_.SetServiceObject(kTestService, service_object));
|
|
|
|
MP_EXPECT_OK(
|
2019-06-17 01:03:25 +02:00
|
|
|
graph_.SetServiceObject(kAnotherService, std::make_shared<int>(100)));
|
|
|
|
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_EXPECT_OK(graph_.StartRun({}));
|
|
|
|
MP_ASSERT_OK(
|
2019-06-17 01:03:25 +02:00
|
|
|
graph_.AddPacketToInputStream("in", MakePacket<int>(3).At(Timestamp(0))));
|
2019-09-11 22:58:46 +02:00
|
|
|
MP_ASSERT_OK(graph_.CloseAllInputStreams());
|
|
|
|
MP_ASSERT_OK(graph_.WaitUntilDone());
|
2019-06-17 01:03:25 +02:00
|
|
|
EXPECT_EQ(PacketValues<int>(output_packets_), (std::vector<int>{108}));
|
|
|
|
}
|
|
|
|
|
2022-05-04 00:29:57 +02:00
|
|
|
TEST_F(GraphServiceTest, CreateDefault) {
|
|
|
|
EXPECT_FALSE(kTestService.CreateDefaultObject().ok());
|
|
|
|
MP_EXPECT_OK(kAnotherService.CreateDefaultObject());
|
|
|
|
EXPECT_FALSE(kNoDefaultService.CreateDefaultObject().ok());
|
|
|
|
MP_EXPECT_OK(kNeedsCreateService.CreateDefaultObject());
|
|
|
|
}
|
|
|
|
|
2023-03-04 01:59:29 +01:00
|
|
|
struct TestServiceData {};
|
|
|
|
|
2023-05-18 00:48:18 +02:00
|
|
|
constexpr GraphService<TestServiceData> kTestServiceAllowDefaultInitialization(
|
2023-03-04 01:59:29 +01:00
|
|
|
"kTestServiceAllowDefaultInitialization",
|
|
|
|
GraphServiceBase::kAllowDefaultInitialization);
|
|
|
|
|
|
|
|
// This is only for test purposes. Ideally, a calculator that fails when service
|
|
|
|
// is not available, should request the service as non-Optional.
|
|
|
|
class FailOnUnavailableOptionalServiceCalculator : public CalculatorBase {
|
|
|
|
public:
|
|
|
|
static absl::Status GetContract(CalculatorContract* cc) {
|
|
|
|
cc->UseService(kTestServiceAllowDefaultInitialization).Optional();
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
absl::Status Open(CalculatorContext* cc) final {
|
|
|
|
RET_CHECK(cc->Service(kTestServiceAllowDefaultInitialization).IsAvailable())
|
|
|
|
<< "Service is unavailable.";
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
absl::Status Process(CalculatorContext* cc) final { return absl::OkStatus(); }
|
|
|
|
};
|
|
|
|
REGISTER_CALCULATOR(FailOnUnavailableOptionalServiceCalculator);
|
|
|
|
|
|
|
|
// Documents and ensures current behavior for requesting optional
|
|
|
|
// "AllowDefaultInitialization" services:
|
|
|
|
// - Service object is created by default.
|
|
|
|
TEST(AllowDefaultInitializationGraphServiceTest,
|
|
|
|
ServiceIsAvailableWithOptionalUse) {
|
|
|
|
CalculatorGraphConfig config =
|
|
|
|
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
|
|
|
|
node { calculator: 'FailOnUnavailableOptionalServiceCalculator' }
|
|
|
|
)pb");
|
|
|
|
|
|
|
|
CalculatorGraph graph;
|
|
|
|
MP_ASSERT_OK(graph.Initialize(config));
|
|
|
|
MP_ASSERT_OK(graph.StartRun({}));
|
|
|
|
MP_EXPECT_OK(graph.WaitUntilIdle());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Documents and ensures current behavior for setting `nullptr` service objects
|
|
|
|
// for "AllowDefaultInitialization" optional services.
|
|
|
|
// - It's allowed.
|
|
|
|
// - It disables creation of "AllowDefaultInitialization" service objects, hence
|
|
|
|
// results in optional service unavailability.
|
|
|
|
TEST(AllowDefaultInitializationGraphServiceTest,
|
|
|
|
NullServiceObjectIsAllowAndResultsInOptionalServiceUnavailability) {
|
|
|
|
CalculatorGraphConfig config =
|
|
|
|
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
|
|
|
|
node { calculator: 'FailOnUnavailableOptionalServiceCalculator' }
|
|
|
|
)pb");
|
|
|
|
|
|
|
|
CalculatorGraph graph;
|
|
|
|
std::shared_ptr<TestServiceData> object = nullptr;
|
|
|
|
MP_ASSERT_OK(
|
|
|
|
graph.SetServiceObject(kTestServiceAllowDefaultInitialization, object));
|
|
|
|
MP_ASSERT_OK(graph.Initialize(config));
|
|
|
|
MP_ASSERT_OK(graph.StartRun({}));
|
|
|
|
EXPECT_THAT(graph.WaitUntilIdle(),
|
|
|
|
StatusIs(absl::StatusCode::kInternal,
|
|
|
|
HasSubstr("Service is unavailable.")));
|
|
|
|
}
|
|
|
|
|
|
|
|
class FailOnUnavailableServiceCalculator : public CalculatorBase {
|
|
|
|
public:
|
|
|
|
static absl::Status GetContract(CalculatorContract* cc) {
|
|
|
|
cc->UseService(kTestServiceAllowDefaultInitialization);
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
absl::Status Open(CalculatorContext* cc) final {
|
|
|
|
RET_CHECK(cc->Service(kTestServiceAllowDefaultInitialization).IsAvailable())
|
|
|
|
<< "Service is unavailable.";
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
absl::Status Process(CalculatorContext* cc) final { return absl::OkStatus(); }
|
|
|
|
};
|
|
|
|
REGISTER_CALCULATOR(FailOnUnavailableServiceCalculator);
|
|
|
|
|
|
|
|
// Documents and ensures current behavior for requesting optional
|
|
|
|
// "AllowDefaultInitialization" services:
|
|
|
|
// - Service object is created by default.
|
|
|
|
TEST(AllowDefaultInitializationGraphServiceTest, ServiceIsAvailable) {
|
|
|
|
CalculatorGraphConfig config =
|
|
|
|
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
|
|
|
|
node { calculator: 'FailOnUnavailableServiceCalculator' }
|
|
|
|
)pb");
|
|
|
|
|
|
|
|
CalculatorGraph graph;
|
|
|
|
MP_ASSERT_OK(graph.Initialize(config));
|
|
|
|
MP_ASSERT_OK(graph.StartRun({}));
|
|
|
|
MP_EXPECT_OK(graph.WaitUntilIdle());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Documents and ensures current behavior for setting `nullptr` service objects
|
|
|
|
// for "AllowDefaultInitialization" services.
|
|
|
|
// - It's allowed.
|
|
|
|
// - It disables creation of "AllowDefaultInitialization" service objects, hence
|
|
|
|
// in service unavaialbility.
|
|
|
|
TEST(AllowDefaultInitializationGraphServiceTest,
|
|
|
|
NullServiceObjectIsAllowAndResultsInServiceUnavailability) {
|
|
|
|
CalculatorGraphConfig config =
|
|
|
|
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
|
|
|
|
node { calculator: 'FailOnUnavailableServiceCalculator' }
|
|
|
|
)pb");
|
|
|
|
|
|
|
|
CalculatorGraph graph;
|
|
|
|
std::shared_ptr<TestServiceData> object = nullptr;
|
|
|
|
MP_ASSERT_OK(
|
|
|
|
graph.SetServiceObject(kTestServiceAllowDefaultInitialization, object));
|
|
|
|
MP_ASSERT_OK(graph.Initialize(config));
|
|
|
|
MP_ASSERT_OK(graph.StartRun({}));
|
|
|
|
EXPECT_THAT(graph.WaitUntilIdle(),
|
|
|
|
StatusIs(absl::StatusCode::kInternal,
|
|
|
|
HasSubstr("Service is unavailable.")));
|
|
|
|
}
|
|
|
|
|
2023-05-18 00:48:18 +02:00
|
|
|
constexpr GraphService<TestServiceData>
|
|
|
|
kTestServiceDisallowDefaultInitialization(
|
|
|
|
"kTestServiceDisallowDefaultInitialization",
|
|
|
|
GraphServiceBase::kDisallowDefaultInitialization);
|
|
|
|
|
|
|
|
static_assert(std::is_trivially_destructible_v<GraphService<TestServiceData>>,
|
|
|
|
"GraphService is not trivially destructible");
|
2023-03-04 01:59:29 +01:00
|
|
|
|
|
|
|
class FailOnUnavailableOptionalDisallowDefaultInitServiceCalculator
|
|
|
|
: public CalculatorBase {
|
|
|
|
public:
|
|
|
|
static absl::Status GetContract(CalculatorContract* cc) {
|
|
|
|
cc->UseService(kTestServiceDisallowDefaultInitialization).Optional();
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
absl::Status Open(CalculatorContext* cc) final {
|
|
|
|
RET_CHECK(
|
|
|
|
cc->Service(kTestServiceDisallowDefaultInitialization).IsAvailable())
|
|
|
|
<< "Service is unavailable.";
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
absl::Status Process(CalculatorContext* cc) final { return absl::OkStatus(); }
|
|
|
|
};
|
|
|
|
REGISTER_CALCULATOR(
|
|
|
|
FailOnUnavailableOptionalDisallowDefaultInitServiceCalculator);
|
|
|
|
|
|
|
|
// Documents and ensures current behavior for requesting optional
|
|
|
|
// "DisallowDefaultInitialization" services:
|
|
|
|
// - Service object is not created by default.
|
|
|
|
TEST(DisallowDefaultInitializationGraphServiceTest,
|
|
|
|
ServiceIsUnavailableWithOptionalUse) {
|
|
|
|
CalculatorGraphConfig config = mediapipe::ParseTextProtoOrDie<
|
|
|
|
CalculatorGraphConfig>(R"pb(
|
|
|
|
node {
|
|
|
|
calculator: 'FailOnUnavailableOptionalDisallowDefaultInitServiceCalculator'
|
|
|
|
}
|
|
|
|
)pb");
|
|
|
|
|
|
|
|
CalculatorGraph graph;
|
|
|
|
MP_ASSERT_OK(graph.Initialize(config));
|
|
|
|
MP_ASSERT_OK(graph.StartRun({}));
|
|
|
|
EXPECT_THAT(graph.WaitUntilIdle(),
|
|
|
|
StatusIs(absl::StatusCode::kInternal,
|
|
|
|
HasSubstr("Service is unavailable.")));
|
|
|
|
}
|
|
|
|
|
|
|
|
class UseDisallowDefaultInitServiceCalculator : public CalculatorBase {
|
|
|
|
public:
|
|
|
|
static absl::Status GetContract(CalculatorContract* cc) {
|
|
|
|
cc->UseService(kTestServiceDisallowDefaultInitialization);
|
|
|
|
return absl::OkStatus();
|
|
|
|
}
|
|
|
|
absl::Status Open(CalculatorContext* cc) final { return absl::OkStatus(); }
|
|
|
|
absl::Status Process(CalculatorContext* cc) final { return absl::OkStatus(); }
|
|
|
|
};
|
|
|
|
REGISTER_CALCULATOR(UseDisallowDefaultInitServiceCalculator);
|
|
|
|
|
|
|
|
// Documents and ensures current behavior for requesting
|
|
|
|
// "DisallowDefaultInitialization" services:
|
|
|
|
// - Service object is not created by default.
|
|
|
|
// - Graph run fails.
|
|
|
|
TEST(DisallowDefaultInitializationGraphServiceTest,
|
|
|
|
StartRunFailsMissingService) {
|
|
|
|
CalculatorGraphConfig config =
|
|
|
|
mediapipe::ParseTextProtoOrDie<CalculatorGraphConfig>(R"pb(
|
|
|
|
node { calculator: 'UseDisallowDefaultInitServiceCalculator' }
|
|
|
|
)pb");
|
|
|
|
|
|
|
|
CalculatorGraph graph;
|
|
|
|
MP_ASSERT_OK(graph.Initialize(config));
|
|
|
|
EXPECT_THAT(graph.StartRun({}),
|
|
|
|
StatusIs(absl::StatusCode::kInternal,
|
|
|
|
HasSubstr("was not provided and cannot be created")));
|
|
|
|
}
|
|
|
|
|
2023-03-04 02:03:18 +01:00
|
|
|
TEST(ServiceBindingTest, CrashesWhenGettingNullServiceObject) {
|
|
|
|
ASSERT_DEATH(
|
|
|
|
{
|
|
|
|
ServiceBinding<TestServiceData> binding(nullptr);
|
|
|
|
(void)binding.GetObject();
|
|
|
|
},
|
|
|
|
testing::ContainsRegex("Check failed: [a-z_]* Service is unavailable"));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(ServiceBindingTest, IsAvailableReturnsFalsOnNullServiceObject) {
|
|
|
|
ServiceBinding<TestServiceData> binding(nullptr);
|
|
|
|
EXPECT_FALSE(binding.IsAvailable());
|
|
|
|
}
|
|
|
|
|
2019-06-17 01:03:25 +02:00
|
|
|
} // namespace
|
|
|
|
} // namespace mediapipe
|