Implementation:Tensorflow Serving Servable State Monitor Test
| Knowledge Sources | |
|---|---|
| Domains | Testing, Core Serving Infrastructure |
| Last Updated | 2026-02-13 00:00 GMT |
Overview
Test suite validating the ServableStateMonitor component, which tracks and reports the state of servables managed by the serving system.
Description
This test file exercises the ServableStateMonitor class, verifying its ability to track servable states through an EventBus<ServableState>. The fixture ServableStateMonitorTest sets up a FakeClockEnv for deterministic time control and an event bus for publishing state transitions. Tests cover state addition, state updates, bounded logging, live/available state queries, version ordering, servable forgetting, and asynchronous notification callbacks including error propagation.
Usage
Run these tests to validate that servable lifecycle state tracking, event-driven notification, and bounded log retention behave correctly when servables transition through start, available, unloading, and end states.
Code Reference
Source Location
- Repository: Tensorflow_Serving
- File:
tensorflow_serving/core/servable_state_monitor_test.cc - Lines: 1-625
Test Fixture
class ServableStateMonitorTest : public ::testing::Test {
protected:
ServableStateMonitorTest() {
env_ = std::make_unique<test_util::FakeClockEnv>(Env::Default());
EventBus<ServableState>::Options bus_options;
bus_options.env = env_.get();
bus_ = EventBus<ServableState>::CreateEventBus(bus_options);
}
void CreateMonitor(int max_count_log_events = 0) {
ServableStateMonitor::Options monitor_options;
monitor_options.max_count_log_events = max_count_log_events;
monitor_ =
std::make_unique<ServableStateMonitor>(bus_.get(), monitor_options);
}
std::unique_ptr<test_util::FakeClockEnv> env_;
std::shared_ptr<EventBus<ServableState>> bus_;
std::unique_ptr<ServableStateMonitor> monitor_;
};
Build Target
bazel test //tensorflow_serving/core:servable_state_monitor_test
Test Coverage
Key Test Cases
| Test Name | Category | Description |
|---|---|---|
| AddingStates | State Tracking | Verifies states are added for new servable versions and new servable names |
| UpdatingStates | State Tracking | Verifies state updates overwrite previous states for the same servable |
| DisableBoundedLogging | Configuration | Ensures bounded log is empty when max_count_log_events is 0 |
| GetLiveServableStates | Query | Validates retrieval of servables that are not in kEnd state |
| GetAvailableServableStates | Query | Validates retrieval of servables in kAvailable state |
| VersionMapDescendingOrder | Ordering | Confirms version maps are returned in descending version order |
| ForgetUnloadedServableStates | Cleanup | Tests that unloaded servable states are properly forgotten |
| NotifyWhenServablesReachStateZeroServables | Notification | Tests notification callback with an empty servable set |
| NotifyWhenServablesReachStateSpecificError | Notification | Tests error-specific notification when a servable enters error state |
| NotifyWhenServablesReachStateLatestError | Notification | Tests notification with the latest error when multiple errors occur |
Usage Examples
Test Pattern
TEST_F(ServableStateMonitorTest, AddingStates) {
CreateMonitor(/*max_count_log_events=*/4);
ServableState notified_state;
monitor_->Notify([&](const ServableState& servable_state) {
notified_state = servable_state;
});
EXPECT_FALSE(monitor_->GetState(ServableId{"foo", 42}));
EXPECT_TRUE(monitor_->GetVersionStates("foo").empty());
EXPECT_TRUE(monitor_->GetAllServableStates().empty());
EXPECT_TRUE(monitor_->GetBoundedLog().empty());
// Initial servable.
const ServableState state_0 = {ServableId{"foo", 42},
ServableState::ManagerState::kStart,
absl::OkStatus()};
env_->AdvanceByMicroseconds(1);
bus_->Publish(state_0);
ASSERT_TRUE(monitor_->GetState(ServableId{"foo", 42}));
EXPECT_EQ(state_0, *monitor_->GetState(ServableId{"foo", 42}));
EXPECT_EQ(state_0, notified_state);
}