# Copyright (c) Huawei Technologies Co., Ltd. 2026. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

"""
End-to-end test for idle timeout eviction feature.

Creates a function with idle_timeout=5s, invokes it once to warm up an instance,
then waits for the instance to be evicted by the idle timer.
"""

import logging
import time

import yr

LOGGER = logging.getLogger(__name__)

yr.init()


@yr.invoke(num_cpus=0.1, idle_timeout=5)
def idle_test_func(x):
    return x * 2

logging.basicConfig(level=logging.INFO, format="%(levelname)s:%(name)s:%(message)s")

LOGGER.info("[1] Invoking idle_test_func to create an instance...")
ref = idle_test_func.invoke(10)
result = yr.get(ref)
LOGGER.info("[1] Result: %s", result)
assert result == 20, f"Expected 20, got {result}"

LOGGER.info("[2] Waiting 12s for idle timeout eviction (timeout=5s)...")
time.sleep(12)

LOGGER.info("[3] Invoking again - should create a NEW instance after eviction...")
ref2 = idle_test_func.invoke(21)
result2 = yr.get(ref2)
LOGGER.info("[3] Result: %s", result2)
assert result2 == 42, f"Expected 42, got {result2}"

LOGGER.info("\n[OK] idle timeout e2e test passed!")

yr.finalize()