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())
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))
 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')
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))
Example #6
0
    def configurationOne(self):
        # create some configuration for kernel

        lockReadyQueue = Condition()
        lockInstructions = Condition()
        lockPcb = Condition()
        pcbTable = PcbTable()
        irqQueue = Queue()
        lockIrqQueue = Condition()
        lockProcessing = Condition()
        swapDisk = SwapDisk()

        InterruptionManagerBuilder = interruptionManagerBuilder()
        readyQueue = Queue()
        disk = diskFactory().configurationOfTheDiskWithThreePrograms()

        memory = MemoryFactory().createElement(lockInstructions)
        frames = FramesFactory().createElement(len(memory.cells))
        memoryManager = MemoryManager(memory, disk, swapDisk, frames)
        interruptionManager = InterruptionManagerBuilder.createElement(
            lockReadyQueue, lockProcessing, irqQueue, lockIrqQueue)

        cpu = CpuBuilder().createElement(memoryManager, interruptionManager,
                                         lockPcb, irqQueue, lockIrqQueue,
                                         lockInstructions, "1")
        scheduler = SchedulerBuilder().createElement(readyQueue, 2,
                                                     lockReadyQueue)
        scheduler.registryCpu(cpu)
        ##todo remove memory
        monitorDevice = Monitor(interruptionManager, lockInstructions)
        printerDevice = Printer(interruptionManager, lockInstructions)
        monitorDevice.start()
        printerDevice.start()

        deviceManager = DeviceManagerBuilder().createElement(
            ResourceType.Monitor, monitorDevice)
        deviceManager.registerDevice(ResourceType.Printer, printerDevice)
        InterruptionManagerBuilder.registryInterruptionManager(
            interruptionManager, deviceManager, scheduler, memoryManager,
            readyQueue, lockReadyQueue, pcbTable)

        interruptionManager.start()
        clock = ClockBuilder().createElement(lockProcessing)
        clock.registrycpu(cpu)

        programLoader = ProgramLoaderBuilder().createElement(
            disk, memoryManager, interruptionManager, pcbTable, lockIrqQueue)

        clock.setDaemon(True)

        clock.start()
        deviceManager.start()
        kernel = Kernel()
        kernel.initializeKernel(clock, programLoader, scheduler)
        kernel.start()
        return kernel
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)
 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 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())