Repository: ray-project/ray
File: python/ray/tests/test_utils.py (Python)

Code before:
        {"CPU_group_e765be422c439de2cd263c5d9d1701000000": 1, "memory": 100}
    )
    assert out == {"CPU": 1, "memory": 100}

    out = parse_pg_formatted_resources_to_original(
        {
            "memory_group_4da1c24ac25bec85bc817b258b5201000000": 100.0,
            "memory_group_0_4da1c24ac25bec85bc817b258b5201000000": 100.0,
            "CPU_group_0_4da1c24ac25bec85bc817b258b5201000000": 1.0,
            "CPU_group_4da1c24ac25bec85bc817b258b5201000000": 1.0,
        }
    )
    assert out == {"CPU": 1, "memory": 100}


@pytest.mark.skipif(
    not sys.platform.startswith("linux"), reason="Doesn't support non-linux"
)
def test_get_current_node_cpu_model_name():
    with patch(
        "builtins.open", mock_open(read_data="processor: 0\nmodel name: Intel Xeon")
    ):
        assert get_current_node_cpu_model_name() == "Intel Xeon"


def test_explicit_object_store_memory():
    """When object_store_memory is explicitly provided, return it directly."""
    available_memory = 16 * (1024**3)  # 16 GB
    explicit_memory = 4 * (1024**3)  # 4 GB

    result = resolve_object_store_memory(available_memory, explicit_memory)
    assert result == explicit_memory
    assert (
        result
        != available_memory * ray_constants.DEFAULT_OBJECT_STORE_MEMORY_PROPORTION
    )


def test_default_object_store_memory():
    """Test default calculation with small available memory."""
    available_memory = 4 * (1024**3)  # 4 GB
    # Use large shm so it doesn't cap the result
    mock_shm_bytes = 100 * (1024**3)  # 100 GB
    expected = int(
        available_memory * ray_constants.DEFAULT_OBJECT_STORE_MEMORY_PROPORTION
    )

    with patch("ray._private.utils.sys.platform", "linux"):
        with patch(
            "ray._private.utils.get_shared_memory_bytes",
            return_value=mock_shm_bytes,

Review comment (suggestion):
```suggestion
def test_resolve_object_store_memory_returns_explicitly_specified_memory_instead_of_default():
```

verbose is fine as long as its self-documenting

Code after:
        {"CPU_group_e765be422c439de2cd263c5d9d1701000000": 1, "memory": 100}
    )
    assert out == {"CPU": 1, "memory": 100}

    out = parse_pg_formatted_resources_to_original(
        {
            "memory_group_4da1c24ac25bec85bc817b258b5201000000": 100.0,
            "memory_group_0_4da1c24ac25bec85bc817b258b5201000000": 100.0,
            "CPU_group_0_4da1c24ac25bec85bc817b258b5201000000": 1.0,
            "CPU_group_4da1c24ac25bec85bc817b258b5201000000": 1.0,
        }
    )
    assert out == {"CPU": 1, "memory": 100}


@pytest.mark.skipif(
    not sys.platform.startswith("linux"), reason="Doesn't support non-linux"
)
def test_get_current_node_cpu_model_name():
    with patch(
        "builtins.open", mock_open(read_data="processor: 0\nmodel name: Intel Xeon")
    ):
        assert get_current_node_cpu_model_name() == "Intel Xeon"


def test_object_store_memory_resolve_to_specified_memory():
    """
    Test object store memory resolves to user specified memory
    when provided.
    """
    available_memory_bytes = 16 * (1024**3)  # 16 GB
    explicit_memory = 4 * (1024**3)  # 4 GB

    result = resolve_object_store_memory(available_memory_bytes, explicit_memory)
    assert result == explicit_memory
    assert (
        result
        != available_memory_bytes * ray_constants.DEFAULT_OBJECT_STORE_MEMORY_PROPORTION
    )


def test_object_store_memory_resolve_to_default_memory():
    """
    Test object store memory resolves to default memory when no user
    specified object store memory is provided.
    """
    available_memory_bytes = 4 * (1024**3)  # 4 GB
    # Use large shm so it doesn't cap the result
    mock_shm_bytes = 100 * (1024**3)  # 100 GB
    expected = int(
        available_memory_bytes * ray_constants.DEFAULT_OBJECT_STORE_MEMORY_PROPORTION