def setup_load_of_a_program_in_memory(self,scheduler,pcb_table,cpu,memory,amount_instructions, program, pcb_id):
     block_holder = BlockHolder(program)
     block_holder.set_representation([0,1])
     pcb = PCB(amount_instructions, pcb_id, block_holder)
     scheduler.policy.add_pcb(pcb)
     memory_admin = ToyMemoryAdmin(memory)
     cpu.set_actual_pcb(pcb)
     pcb_table.add(cpu.get_actual_pcb())
     cpu.set_memory_manager(memory_admin)
 def two_programs_in_ready_queue(self):
     instruction_io = InstructionIO()
     instruction = Instruction("text")
     hold1 = [1,2,3,4]
     hold2 = [5,6,7,8]
     self.block_holder1 = BlockHolder(self.a_program_with_instruction(instruction_io))
     self.block_holder1.set_representation(hold1)
     self.pcb1 = PCB(1, 1, self.block_holder1)
     self.block_holder2 = BlockHolder(self.a_program_with_instruction(instruction))
     self.block_holder2.set_representation(hold2)
     self.pcb2 = PCB(1, 2, self.block_holder2)
     self.kernel.scheduler.push_to_queue(self.pcb1)
     self.kernel.scheduler.push_to_queue(self.pcb2)
class TestIOInterruption(unittest.TestCase):
    def setUp(self):
        self.kernel = Kernel(None)
        self.kernel.to_user_mode()
        self.kernel.scheduler = Scheduler(None)
        self.kernel.scheduler.set_as_fifo()
        self.interruption_manager = InterruptionHandler(self.kernel.cpu)
        self.kernel.set_interruption_handler(self.interruption_manager)
        load_in_interruption_manager = Handle_Loaders()
        load_in_interruption_manager.load_handlers(self, self.interruption_manager)
        self.memory = ToyMemory()
        self.memory_manager = ToyMemoryAdmin(self.memory)
        self.kernel.set_memory_manager(self.memory_manager)

    def two_programs_in_ready_queue(self):
        instruction_io = InstructionIO()
        instruction = Instruction("text")
        hold1 = [1,2,3,4]
        hold2 = [5,6,7,8]
        self.block_holder1 = BlockHolder(self.a_program_with_instruction(instruction_io))
        self.block_holder1.set_representation(hold1)
        self.pcb1 = PCB(1, 1, self.block_holder1)
        self.block_holder2 = BlockHolder(self.a_program_with_instruction(instruction))
        self.block_holder2.set_representation(hold2)
        self.pcb2 = PCB(1, 2, self.block_holder2)
        self.kernel.scheduler.push_to_queue(self.pcb1)
        self.kernel.scheduler.push_to_queue(self.pcb2)

    def a_program_with_instruction(self,instruction_io):
        a_program = Program("P")
        for i in range(0,5):
            a_program.addInstruction(instruction_io)
        self.memory.write_program(a_program)
        return a_program

    def test_when_a_process_is_io_then_goes_to_the_waiting_queue(self):
        self.two_programs_in_ready_queue()
        io_pcb = self.kernel.get_ready_queue._get()
        self.kernel.cpu.set_actual_pcb(io_pcb)
        without_io_pcb = self.kernel.get_ready_queue._get()
        self.assertEqual(io_pcb.get_pid,self.kernel.cpu.actual_pcb.get_pid)
        self.kernel.cpu.complete_instruction_cycle()
        self.assertEqual(ProcessState.ProcessState.waiting, io_pcb.get_state)
        self.kernel.cpu.set_actual_pcb(without_io_pcb)
        self.assertEqual(without_io_pcb.get_pid,self.kernel.cpu.actual_pcb.get_pid)
        without_io_pcb.set_state(ProcessState.ProcessState.running)
        self.assertEqual(ProcessState.ProcessState.running, without_io_pcb.get_state)
class TestInterruption(unittest.TestCase):
    def setUp(self):
        self.cpu = Cpu(None)
        self.kernel = Kernel(None, self.cpu)
        self.cpu.kernel = self.kernel
        self.pcb_table = self.kernel.pcb_table
        self.memory_manager = self.kernel.memory_manager
        self.fifo = FifoPolicy(self.kernel.scheduler)
        self.program = Program("Pepe")
        self.instruction = Instruction("first instruction")
        self.program.addInstruction(self.instruction)
        self.info_unit = [1, 2, 3, 4]
        self.block_holder = BlockHolder(self.program)
        self.block_holder.set_representation(self.info_unit)
        self.pcb = PCB(1, 4, self.block_holder)
        self.pcb_table.add(self.pcb)
        self.cpu.set_actual_pcb(self.pcb)
        self.manager = Manager(self.kernel.scheduler, self.pcb_table, self.memory_manager)
        self.interruption_handler = InterruptionHandler(self.manager)
        self.kernel.set_interruption_handler(self.interruption_handler)

    def test_when_a_process_is_killed_then_it_is_removed_from_the_pcb_table(self):
        self.cpu.execute_single_instruction(self.program.get_instructions().pop())
        self.assertEquals(self.pcb_table.size(), 0)
 def setUp(self):
     self.cpu = Cpu(None)
     self.kernel = Kernel(None, self.cpu)
     self.cpu.kernel = self.kernel
     self.pcb_table = self.kernel.pcb_table
     self.memory_manager = self.kernel.memory_manager
     self.fifo = FifoPolicy(self.kernel.scheduler)
     self.program = Program("Pepe")
     self.instruction = Instruction("first instruction")
     self.program.addInstruction(self.instruction)
     self.info_unit = [1, 2, 3, 4]
     self.block_holder = BlockHolder(self.program)
     self.block_holder.set_representation(self.info_unit)
     self.pcb = PCB(1, 4, self.block_holder)
     self.pcb_table.add(self.pcb)
     self.cpu.set_actual_pcb(self.pcb)
     self.manager = Manager(self.kernel.scheduler, self.pcb_table, self.memory_manager)
     self.interruption_handler = InterruptionHandler(self.manager)
     self.kernel.set_interruption_handler(self.interruption_handler)