class TestsProgramLoader(unittest.TestCase):
    def setUp(self):
        self.disk = Disk()
        self.irqQueueMock = Queue()
        self.killHandleMock = Mock()
        self.timeOutHandleMock = Mock()
        self.ioHandleMock = Mock()

        self.lockProcessingMock = Condition()
        self.lockIrqQueueMock = Condition()
        self.lockReadyQueueMock = Condition()
        self.lockInstructions = Condition()
        self.lockIrq = Condition()
        self.memory = Memory(self.lockInstructions)

        self.interruptionManager = InterruptionManager(self.lockReadyQueueMock,
                                                       self.lockProcessingMock,
                                                       self.irqQueueMock,
                                                       self.lockIrqQueueMock)
        self.interruptionManager.registerHandler(IRQ.kill, self.killHandleMock)
        self.interruptionManager.registerHandler(IRQ.timeOut,
                                                 self.timeOutHandleMock)
        self.interruptionManager.registerHandler(IRQ.IO, self.ioHandleMock)
        self.programLoader = ProgramLoader(self.disk, self.memory,
                                           self.interruptionManager,
                                           PcbTable(), self.lockIrq)
        self.memoryManager = MemoryManager(self.memory, self.disk, Mock(),
                                           Mock())

    def test_when_have_a_program_Then_I_want_to_create_a_pcb_with_pid_1(self):

        program = Program()
        self.assertEqual(
            self.programLoader.createPcb(program, "program0").pid, 1)

    def test_when_have_a_program_Then_the_memory_is_write_with_a_new_program(
            self):

        program = Program()
        instruction1 = Instruction("Cpu Instruction1", InstructionType.cpu,
                                   ResourceType.Monitor)
        instruction2 = Instruction("Cpu Instruction1", InstructionType.cpu,
                                   ResourceType.Monitor)
        instruction3 = Instruction("Cpu Instruction1", InstructionType.cpu,
                                   ResourceType.Monitor)
        instruction4 = Instruction("Cpu Instruction1", InstructionType.cpu,
                                   ResourceType.Monitor)
        program.add(instruction1)
        program.add(instruction2)
        program.add(instruction3)
        program.add(instruction4)
        self.memoryManager.loadToMemory(
            self.programLoader.createPcb(program, "program0"),
            program.instructions)
        self.assertEqual(self.memory.cells[1], instruction2)
class TestsCpu(unittest.TestCase):
    def setUp(self):
        self.LockInstructionMock = Mock()
        self.memory = Memory(self.LockInstructionMock)
        self.interruptionManagerMock = Mock()
        self.quantum = 1
        self.lockPcbMock = Mock()
        self.irqQueueMock = Mock()
        self.lockIrqQueueMock = Mock()
        self.disk = Disk()
        self.frames = FramesFactory().createElement(16)
        self.swapDisk = SwapDisk()
        self.memoryManager = MemoryManager(self.memory, self.disk,
                                           self.swapDisk, self.frames)
        self.cpu = Cpu(self.memoryManager, self.interruptionManagerMock,
                       self.lockPcbMock, self.irqQueueMock,
                       self.lockIrqQueueMock, Mock(), '1')

    def test_when_fetch_end_of_program_then_call_kill_handler(self):
        queueInstruction = Queue()
        instruction = Instruction("", InstructionType.kill,
                                  ResourceType.Monitor)
        pcbfinished = Pcb(0, 0, 1)
        queueInstruction.put(instruction)
        self.memoryManager.loadToMemory(pcbfinished, queueInstruction)
        self.cpu.setPcb(pcbfinished, self.quantum)
        self.cpu.fetch()
        irq = IRQ(IRQ.kill, pcbfinished, self.memoryManager, self.cpu)
        self.interruptionManagerMock.handle.assert_called_with(Matcher(irq))

    def test_when_fetch_io_instruction_then_call_handle_io(self):
        queueInstruction = Queue()
        instruction = Instruction("", InstructionType.io, ResourceType.Monitor)
        pcb = Pcb(0, 0, 1)
        queueInstruction.put(instruction)
        self.memoryManager.loadToMemory(pcb, queueInstruction)
        self.cpu.setPcb(pcb, self.quantum)
        self.cpu.fetch()
        irq = IRQ(IRQ.IO, pcb, instruction, self.cpu)
        self.interruptionManagerMock.handle.assert_called_with(Matcher(irq))

    def test_when_fetch_quantum_equal_zero_then_call_time_out(self):
        pcb = Pcb(0, 0, 0)
        self.quantum = 0
        self.cpu.setPcb(pcb, self.quantum)
        self.cpu.fetch()
        irq = IRQ(IRQ.timeOut, pcb, None, self.cpu)
        self.interruptionManagerMock.handle.assert_called_with(Matcher(irq))
class TestsCpu(unittest.TestCase):
    def setUp(self):
        self.LockInstructionMock= Mock()
        self.memory = Memory(self.LockInstructionMock)
        self.interruptionManagerMock = Mock()
        self.quantum = 1
        self.lockPcbMock =Mock()
        self.irqQueueMock =Mock()
        self.lockIrqQueueMock = Mock()
        self.disk= Disk()
        self.frames= FramesFactory().createElement(16)
        self.swapDisk= SwapDisk()
        self.memoryManager= MemoryManager(self.memory,self.disk, self.swapDisk,self.frames)
        self.cpu = Cpu(self.memoryManager, self.interruptionManagerMock, self.lockPcbMock, self.irqQueueMock, self.lockIrqQueueMock, Mock(),'1')

    def test_when_fetch_end_of_program_then_call_kill_handler(self):
        queueInstruction= Queue()
        instruction = Instruction("", InstructionType.kill, ResourceType.Monitor)
        pcbfinished = Pcb(0, 0, 1)
        queueInstruction.put(instruction)
        self.memoryManager.loadToMemory(pcbfinished, queueInstruction)
        self.cpu.setPcb(pcbfinished, self.quantum)
        self.cpu.fetch()
        irq = IRQ(IRQ.kill, pcbfinished, self.memoryManager,self.cpu)
        self.interruptionManagerMock.handle.assert_called_with(Matcher(irq))

    def test_when_fetch_io_instruction_then_call_handle_io(self):
        queueInstruction= Queue()
        instruction = Instruction("", InstructionType.io, ResourceType.Monitor)
        pcb = Pcb(0, 0, 1)
        queueInstruction.put(instruction)
        self.memoryManager.loadToMemory(pcb, queueInstruction)
        self.cpu.setPcb(pcb, self.quantum)
        self.cpu.fetch()
        irq = IRQ(IRQ.IO, pcb, instruction,self.cpu)
        self.interruptionManagerMock.handle.assert_called_with(Matcher(irq))

    def test_when_fetch_quantum_equal_zero_then_call_time_out(self):
        pcb = Pcb(0, 0, 0)
        self.quantum = 0
        self.cpu.setPcb(pcb, self.quantum)
        self.cpu.fetch()
        irq = IRQ(IRQ.timeOut, pcb, None,self.cpu)
        self.interruptionManagerMock.handle.assert_called_with(Matcher(irq))
class TestsProgramLoader(unittest.TestCase):

    def setUp(self):
        self.disk= Disk()
        self.irqQueueMock =Queue()
        self.killHandleMock = Mock()
        self.timeOutHandleMock = Mock()
        self.ioHandleMock = Mock()

        self.lockProcessingMock= Condition()
        self.lockIrqQueueMock = Condition()
        self.lockReadyQueueMock= Condition()
        self.lockInstructions= Condition()
        self.lockIrq= Condition()
        self.memory= Memory(self.lockInstructions)

        self.interruptionManager = InterruptionManager(self.lockReadyQueueMock,self.lockProcessingMock, self.irqQueueMock, self.lockIrqQueueMock)
        self.interruptionManager.registerHandler(IRQ.kill, self.killHandleMock)
        self.interruptionManager.registerHandler(IRQ.timeOut, self.timeOutHandleMock)
        self.interruptionManager.registerHandler(IRQ.IO, self.ioHandleMock)
        self.programLoader= ProgramLoader(self.disk, self.memory, self.interruptionManager, PcbTable(), self.lockIrq)
        self.memoryManager= MemoryManager(self.memory, self.disk,Mock(),Mock())

    def test_when_have_a_program_Then_I_want_to_create_a_pcb_with_pid_1(self):

        program= Program()
        self.assertEqual(self.programLoader.createPcb(program, "program0").pid, 1)

    def test_when_have_a_program_Then_the_memory_is_write_with_a_new_program(self):

        program= Program()
        instruction1= Instruction("Cpu Instruction1", InstructionType.cpu, ResourceType.Monitor)
        instruction2= Instruction("Cpu Instruction1", InstructionType.cpu, ResourceType.Monitor)
        instruction3= Instruction("Cpu Instruction1", InstructionType.cpu, ResourceType.Monitor)
        instruction4= Instruction("Cpu Instruction1", InstructionType.cpu, ResourceType.Monitor)
        program.add(instruction1)
        program.add(instruction2)
        program.add(instruction3)
        program.add(instruction4)
        self.memoryManager.loadToMemory(self.programLoader.createPcb(program,"program0"), program.instructions)
        self.assertEqual(self.memory.cells[1], instruction2)