Add a CHECK for the cases when null service is accessed unconditionally.
PiperOrigin-RevId: 513956583
This commit is contained in:
parent
9c3abcd06f
commit
5b2678a49f
|
@ -107,7 +107,10 @@ template <typename T>
|
|||
class ServiceBinding {
|
||||
public:
|
||||
bool IsAvailable() { return service_ != nullptr; }
|
||||
T& GetObject() { return *service_; }
|
||||
T& GetObject() {
|
||||
CHECK(service_) << "Service is unavailable.";
|
||||
return *service_;
|
||||
}
|
||||
|
||||
ServiceBinding() {}
|
||||
explicit ServiceBinding(std::shared_ptr<T> service) : service_(service) {}
|
||||
|
|
|
@ -343,5 +343,19 @@ TEST(DisallowDefaultInitializationGraphServiceTest,
|
|||
HasSubstr("was not provided and cannot be created")));
|
||||
}
|
||||
|
||||
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());
|
||||
}
|
||||
|
||||
} // namespace
|
||||
} // namespace mediapipe
|
||||
|
|
Loading…
Reference in New Issue
Block a user