Jump to content

Connect SuperML | Leeroopedia MCP: Equip your AI agents with best practices, code verification, and debugging knowledge. Powered by Leeroo — building Organizational Superintelligence. Contact us at founders@leeroo.com.

Implementation:Tensorflow Serving Resource Util Test

From Leeroopedia
Knowledge Sources
Domains Testing, Resource Management
Last Updated 2026-02-13 00:00 GMT

Overview

Test suite validating the ResourceUtil component, which provides arithmetic, comparison, normalization, and binding operations on ResourceAllocation protocol buffer objects.

Description

This test file comprehensively exercises the ResourceUtil class, which encodes knowledge of the device topology (e.g., 1 main device, 2 GPU instances) and provides operations on ResourceAllocation protos. The fixture ResourceUtilTest initializes a ResourceUtil with a device map of {{"main", 1}, {"gpu", 2}}. With 35+ test cases spanning 1877 lines, this is the most thorough resource management test file. Tests cover validity verification, normalization, bound/unbound resource checks, resource creation, quantity get/set, arithmetic operations (add, subtract, multiply), comparison operations (equal, less-than-or-equal), overbinding of unbound resources to specific device instances, min/max operations, and device override validation.

Usage

Run these tests to validate that all resource allocation arithmetic, comparison, and normalization operations produce correct results for both bound (device-instance-specific) and unbound (device-agnostic) resource allocations.

Code Reference

Source Location

  • Repository: Tensorflow_Serving
  • File: tensorflow_serving/resources/resource_util_test.cc
  • Lines: 1-1877

Test Fixture

class ResourceUtilTest : public ::testing::Test {
 protected:
  ResourceUtilTest() : util_({{{"main", 1}, {"gpu", 2}}}) {}

  // The object under testing.
  ResourceUtil util_;
};

Build Target

bazel test //tensorflow_serving/resources:resource_util_test

Test Coverage

Key Test Cases

Test Name Category Description
VerifyValidity Validation Tests validity checks for empty, unbound, bound, non-existent device, invalid instance, and duplicate entries
VerifyResourceValidity Validation Tests validity of individual Resource proto entries
Normalize Normalization Verifies normalization removes zero-quantity entries and merges duplicates
IsNormalized Normalization Tests the IsNormalized predicate on various allocations
IsBound Binding Checks whether allocations are fully bound to device instances
CreateBoundResource Creation Tests creation of bound Resource protos with specific device instances
GetQuantity / SetQuantity Accessors Validates getting and setting quantities in allocations
AddEmpty / AddBasic / AddBoundAndUnbound Arithmetic Tests addition of resource allocations across empty, basic, and mixed bound/unbound cases
SubtractEmpty / SubtractBasic / SubtractNegativeResult Arithmetic Tests subtraction including error on negative results
SubtractNormalizeOutput / SubtractBoundAndUnbound Arithmetic Tests subtraction normalization and mixed binding scenarios
MultiplyEmpty / MultiplyBasic Arithmetic Tests scalar multiplication of allocations
Equal / EqualOrderInsensitive / ResourcesEqual Comparison Tests equality checks including order-insensitive comparison
LessThanOrEqualEmpty / LessThanOrEqualOneEntry / LessThanOrEqualTwoEntries Comparison Tests less-than-or-equal comparisons at various complexities
LessThanOrEqualImplicitZero / LessThanOrEqualUnbound Comparison Tests comparisons with implicit zeros and unbound resources
Overbind Binding Tests overbinding unbound resources to specific device instances
MaxEmpty / MaxBound / MaxBoundAndUnbound / MaxUnbound Aggregation Tests element-wise max across allocations
MinEmpty / MinBound / MinBoundAndUnbound / MinUnbound Aggregation Tests element-wise min across allocations
OverrideDeviceValidity Device Override Tests device override validation

Usage Examples

Test Pattern

TEST_F(ResourceUtilTest, VerifyValidity) {
  // Empty allocation is valid.
  TF_EXPECT_OK(util_.VerifyValidity(CreateProto<ResourceAllocation>("")));

  // Unbound resource is valid.
  TF_EXPECT_OK(util_.VerifyValidity(
      CreateProto<ResourceAllocation>("resource_quantities { "
                                      "  resource { "
                                      "    device: 'main' "
                                      "    kind: 'processing' "
                                      "  } "
                                      "  quantity: 100 "
                                      "} ")));

  // Non-existent device is invalid.
  EXPECT_FALSE(util_
                   .VerifyValidity(CreateProto<ResourceAllocation>(
                       "resource_quantities { "
                       "  resource { "
                       "    device: 'nonexistent_device' "
                       "    kind: 'processing' "
                       "  } "
                       "  quantity: 100 "
                       "} "))
                   .ok());
}

Related Pages

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment