import unittest
import time
import logging
from src.scaling_policies.cpu_queue_policy import CPUQueuePolicy

logging.basicConfig(level=logging.INFO)

class TestCPUQueueScaling(unittest.TestCase):

    def test_scale_up_and_down(self):
        policy = CPUQueuePolicy(cpu_threshold=70, queue_threshold=50, cpu_low_threshold=30, queue_low_threshold=20, cooldown_time=1)

        # Initial state: no scaling
        self.assertFalse(policy.should_scale_up(50, 30))
        self.assertFalse(policy.should_scale_down(50, 30))

        # Scale up: CPU and Queue above thresholds
        self.assertTrue(policy.should_scale_up(80, 60))

        # Cooldown period: no scaling
        self.assertFalse(policy.should_scale_up(80, 60))
        self.assertFalse(policy.should_scale_down(20, 10))

        time.sleep(1.1)

        # Scale down: CPU and Queue below thresholds
        self.assertTrue(policy.should_scale_down(20, 10))

        # Cooldown period: no scaling
        self.assertFalse(policy.should_scale_down(20, 10))

        time.sleep(1.1)

        # Scale up: CPU high, Queue low (no scale)
        self.assertFalse(policy.should_scale_up(80, 10))

        # Scale up: CPU low, Queue high (no scale)
        self.assertFalse(policy.should_scale_up(10, 60))

        # Scale down: CPU high, Queue low (no scale)
        self.assertFalse(policy.should_scale_down(80, 10))

        # Scale down: CPU low, Queue high (no scale)
        self.assertFalse(policy.should_scale_down(10, 60))

if __name__ == '__main__':
    unittest.main()