|
| 1 | +"""Comprehensive tests for the generic ranker type system.""" |
| 2 | + |
| 3 | +from unittest.mock import MagicMock |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from ray.data._internal.execution.interfaces import PhysicalOperator |
| 8 | +from ray.data._internal.execution.ranker import DefaultRanker, Ranker |
| 9 | +from ray.data._internal.execution.resource_manager import ResourceManager |
| 10 | +from ray.data._internal.execution.streaming_executor_state import Topology |
| 11 | + |
| 12 | + |
| 13 | +def test_default_ranker(): |
| 14 | + """Test that the ranker interface works correctly.""" |
| 15 | + ranker = DefaultRanker() |
| 16 | + |
| 17 | + # Mock objects |
| 18 | + op1 = MagicMock() |
| 19 | + op1.throttling_disabled.return_value = False |
| 20 | + op2 = MagicMock() |
| 21 | + op2.throttling_disabled.return_value = True |
| 22 | + topology = {} |
| 23 | + resource_manager = MagicMock() |
| 24 | + resource_manager.get_op_usage.return_value = MagicMock() |
| 25 | + resource_manager.get_op_usage.return_value.object_store_memory = 1024 |
| 26 | + |
| 27 | + # Test rank_operator for first op |
| 28 | + rank1 = ranker.rank_operator(op1, topology, resource_manager) |
| 29 | + assert rank1 == (1, 1024) # throttling_disabled=False -> 1, memory=1024 |
| 30 | + |
| 31 | + # Test rank_operator for second op |
| 32 | + rank2 = ranker.rank_operator(op2, topology, resource_manager) |
| 33 | + assert rank2 == (0, 1024) # throttling_disabled=True -> 0, memory=1024 |
| 34 | + |
| 35 | + # Test rank_operators with both ops |
| 36 | + ops = [op1, op2] |
| 37 | + ranks = ranker.rank_operators(ops, topology, resource_manager) |
| 38 | + assert ranks == [(1, 1024), (0, 1024)] |
| 39 | + |
| 40 | + |
| 41 | +class IntRanker(Ranker[int]): |
| 42 | + """Ranker that returns integer rankings.""" |
| 43 | + |
| 44 | + def rank_operator( |
| 45 | + self, |
| 46 | + op: PhysicalOperator, |
| 47 | + topology: "Topology", |
| 48 | + resource_manager: ResourceManager, |
| 49 | + ) -> int: |
| 50 | + """Return integer ranking.""" |
| 51 | + return resource_manager.get_op_usage(op).object_store_memory |
| 52 | + |
| 53 | + |
| 54 | +def test_generic_types(): |
| 55 | + """Test that specific generic types work correctly.""" |
| 56 | + # Test integer ranker |
| 57 | + int_ranker = IntRanker() |
| 58 | + op1 = MagicMock() |
| 59 | + op2 = MagicMock() |
| 60 | + topology = {} |
| 61 | + resource_manager = MagicMock() |
| 62 | + resource_manager.get_op_usage.return_value = MagicMock() |
| 63 | + resource_manager.get_op_usage.return_value.object_store_memory = 1024 |
| 64 | + |
| 65 | + # Test rank_operator for first op |
| 66 | + rank1 = int_ranker.rank_operator(op1, topology, resource_manager) |
| 67 | + assert rank1 == 1024 |
| 68 | + |
| 69 | + # Test rank_operator for second op |
| 70 | + rank2 = int_ranker.rank_operator(op2, topology, resource_manager) |
| 71 | + assert rank2 == 1024 |
| 72 | + |
| 73 | + # Test rank_operators with both ops |
| 74 | + ops = [op1, op2] |
| 75 | + ranks = int_ranker.rank_operators(ops, topology, resource_manager) |
| 76 | + assert ranks == [1024, 1024] |
| 77 | + |
| 78 | + |
| 79 | +if __name__ == "__main__": |
| 80 | + import sys |
| 81 | + |
| 82 | + sys.exit(pytest.main(["-v", __file__])) |
0 commit comments