def test_pcb_equality(self): pcb1 = ProcessControlBlock(0, 0, 5, 0) pcb1a = ProcessControlBlock(0, 0, 5, 0) pcb2 = ProcessControlBlock(0, 1, 5, 0) assert pcb1 == pcb1a assert pcb1 != pcb2
def test_nearest_preemption_time(self): mlfq1 = mlfq.MLFQ([], []) assert mlfq1._nearest_preemption_time(1) is None mlfq1 = mlfq.MLFQ( [ProcessControlBlock(0, 0, 1, 0), ProcessControlBlock(1, 5, 3, 0)], []) assert mlfq1._nearest_preemption_time(1) == 5
def test_one_queue(self): # SCENARIO: Testing higher over lower priority MLFQ scheduling. # Test with one process only. processes = [ProcessControlBlock(0, 0, 5, 1)] queues = [mlfq.MLFQQueue(algorithms.fcfs)] mlfq1 = mlfq.MLFQ(processes, queues) schedule, process, run_time = mlfq1.simulate() assert schedule == [0] assert process[0].get_pid() == 0 assert run_time == 5 # Test with three processes. processes = [ ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 3, 5, 0), ProcessControlBlock(2, 3, 5, 0) ] queues = [mlfq.MLFQQueue(algorithms.fcfs)] mlfq1 = mlfq.MLFQ(processes, queues) schedule, process, run_time = mlfq1.simulate() assert schedule == [0, 1, 2] assert process[0].get_pid() == 0 assert process[1].get_pid() == 1 assert process[2].get_pid() == 2 assert run_time == 15 # SCENARIO: Testing fixed time slot MLFQ scheduling. # Test with one process only. processes = [ProcessControlBlock(0, 0, 5, 1)] queues = [mlfq.MLFQQueue(algorithms.fcfs)] mlfq1 = mlfq.MLFQ(processes, queues, time_slot=5) schedule, process, run_time = mlfq1.simulate() assert schedule == [0] assert process[0].get_pid() == 0 assert run_time == 5 # Test with three processes. processes = [ ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 3, 5, 0), ProcessControlBlock(2, 3, 5, 0) ] queues = [mlfq.MLFQQueue(algorithms.fcfs)] mlfq1 = mlfq.MLFQ(processes, queues, time_slot=5) schedule, process, run_time = mlfq1.simulate() assert schedule == [0, 1, 2] assert process[0].get_pid() == 0 assert process[1].get_pid() == 1 assert process[2].get_pid() == 2 assert run_time == 15
def test_execute_history(self): test_pcb = ProcessControlBlock(0, 0, 3, 1) test_pcb.execute(0, 1) test_pcb.execute(1, 1) assert len(test_pcb.get_execution_history()) == 1 test_pcb.execute(3, 1) assert len(test_pcb.get_execution_history()) == 2 execution_history = test_pcb.get_execution_history() assert execution_history[0].get_start() == 0 assert execution_history[0].get_length() == 2 assert execution_history[0].get_end() == 2 assert execution_history[1].get_start() == 3 assert execution_history[1].get_length() == 1 assert execution_history[1].get_end() == 4 assert test_pcb.get_remaining_time() == 0
def test_fcfs(self): # Test for case where there is no overlapping arrival times. processes = [ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 5, 5, 0)] assert algorithms.fcfs(processes) == ([0, 1], [], [], [], [], 10) # Test for case where there is one overlapping arrival times. processes = [ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 3, 5, 0)] assert algorithms.fcfs(processes) == ([0, 1], [], [], [], [], 10) # Test for case where there are same arrival times for two processes but no running process. processes = [ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 0, 5, 0)] assert algorithms.fcfs(processes) == ([0, 1], [], [], [], [], 10) # Test for case where there are two processes that arrived at the same time while another process is running. processes = [ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 3, 5, 0), ProcessControlBlock(2, 3, 5, 0)] assert algorithms.fcfs(processes) == ([0, 1, 2], [], [], [], [], 15) # Test for case where there is a gap between process arrivals. processes = [ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 10, 5, 0)] assert algorithms.fcfs(processes) == ([0, 1], [], [], [], [], 15) # Test for case where queue is preempted because the time slot was already filled up. processes = [ProcessControlBlock(0, 0, 5, 0)] resulting_process = ProcessControlBlock(0, 0, 5, 0) resulting_process.execute(0, 2) assert algorithms.fcfs(processes, time_allotment=2) == ([0], [], [], [resulting_process], [], 2) # Test for case where queue is preempted even though there are still processes arriving. processes = [ProcessControlBlock(0, 10, 5, 0)] assert algorithms.fcfs(processes, time_allotment=5) == ([], [ProcessControlBlock(0, 10, 5, 0)], [], [], [], 5) # Test for case where queue is preempted even though there are still processes waiting to have a first run. processes = [ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 1, 5, 0)] promoted_process = ProcessControlBlock(0, 0, 5, 0) promoted_process.execute(0, 2) assert algorithms.fcfs(processes, time_allotment=2) == ([0], [], [ProcessControlBlock(1, 1, 5, 0)], [promoted_process], [], 2) # Test for case where another set of processes are added from another queue. processes = [ProcessControlBlock(0, 0, 5, 0)] additional_processes = [ProcessControlBlock(1, 1, 5, 0)] assert algorithms.fcfs(processes, additional_processes) == ([0, 1], [], [], [], [], 10)
def test_no_history_recording(self): test_pcb = ProcessControlBlock(0, 0, 1, 1) test_pcb.execute(0, 1, record=False) assert len(test_pcb.get_execution_history()) == 0
def setup_class(self): self.pcb = ProcessControlBlock(0, 12, 12, 1)
class TestProcessControlBlock: @classmethod def setup_class(self): self.pcb = ProcessControlBlock(0, 12, 12, 1) def test_pid(self): assert self.pcb.get_pid() == 0 def test_arrival_time(self): assert self.pcb.get_arrival_time() == 12 def test_burst_time(self): assert self.pcb.get_burst_time() == 12 def test_remaining_time(self): assert self.pcb.get_remaining_time() == 12 def test_priority(self): assert self.pcb.get_priority() == 1 def test_execute_func(self): # Test that we get the correct remaining time after executing # the process starting at 12 for 6 units. self.pcb.execute(12, 6) assert self.pcb.get_remaining_time() == 6 # Test that we do not reduce the remaining time when the start # time is less than the time when the process was last executed. with pytest.raises(pcb.ExecutionRecordingException) as ee_info: self.pcb.execute(11, 6) with pytest.raises(pcb.ExecutionRecordingException) as ee_info: self.pcb.execute(12, 6) with pytest.raises(pcb.ExecutionRecordingException) as ee_info: self.pcb.execute(13, 5) assert self.pcb.get_remaining_time() == 6 execution_history_item = self.pcb.get_execution_history()[0] assert execution_history_item.get_start() == 12 assert execution_history_item.get_length() == 6 assert execution_history_item.get_end() == 18 self.pcb.execute(20, 6) assert len(self.pcb.get_execution_history()) == 2 with pytest.raises(pcb.ExecutionRecordingException) as ee_info: self.pcb.execute(11, 6) def test_execute_history(self): test_pcb = ProcessControlBlock(0, 0, 3, 1) test_pcb.execute(0, 1) test_pcb.execute(1, 1) assert len(test_pcb.get_execution_history()) == 1 test_pcb.execute(3, 1) assert len(test_pcb.get_execution_history()) == 2 execution_history = test_pcb.get_execution_history() assert execution_history[0].get_start() == 0 assert execution_history[0].get_length() == 2 assert execution_history[0].get_end() == 2 assert execution_history[1].get_start() == 3 assert execution_history[1].get_length() == 1 assert execution_history[1].get_end() == 4 assert test_pcb.get_remaining_time() == 0 def test_no_history_recording(self): test_pcb = ProcessControlBlock(0, 0, 1, 1) test_pcb.execute(0, 1, record=False) assert len(test_pcb.get_execution_history()) == 0 def test_pcb_equality(self): pcb1 = ProcessControlBlock(0, 0, 5, 0) pcb1a = ProcessControlBlock(0, 0, 5, 0) pcb2 = ProcessControlBlock(0, 1, 5, 0) assert pcb1 == pcb1a assert pcb1 != pcb2
def test_two_queues(self): # SCENARIO: Testing higher over lower priority MLFQ scheduling. # Test with one process only. processes = [ProcessControlBlock(0, 0, 10, 1)] queues = [ mlfq.MLFQQueue(algorithms.round_robin, quanta=5), mlfq.MLFQQueue(algorithms.fcfs) ] mlfq1 = mlfq.MLFQ(processes, queues) schedule, process, run_time = mlfq1.simulate() assert schedule == [0] assert process[0].get_pid() == 0 assert run_time == 10 # Test with three processes. processes = [ ProcessControlBlock(0, 0, 20, 0), ProcessControlBlock(1, 15, 5, 0), ProcessControlBlock(2, 20, 5, 0) ] queues = [ mlfq.MLFQQueue(algorithms.round_robin, quanta=5), mlfq.MLFQQueue(algorithms.fcfs) ] mlfq1 = mlfq.MLFQ(processes, queues) schedule, process, run_time = mlfq1.simulate() assert schedule == [0, 1, 2, 0] assert process[0].get_pid() == 0 assert process[1].get_pid() == 1 assert process[2].get_pid() == 2 assert run_time == 30 # Test with gaps in between processes. processes = [ ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 10, 5, 0) ] queues = [ mlfq.MLFQQueue(algorithms.round_robin, quanta=5), mlfq.MLFQQueue(algorithms.fcfs) ] mlfq1 = mlfq.MLFQ(processes, queues) schedule, process, run_time = mlfq1.simulate() assert schedule == [0, 1] assert process[0].get_pid() == 0 assert process[1].get_pid() == 1 assert run_time == 15 # SCENARIO: Testing fixed time slot MLFQ scheduling. # Test with one process only. processes = [ProcessControlBlock(0, 0, 10, 1)] queues = [ mlfq.MLFQQueue(algorithms.round_robin, quanta=5), mlfq.MLFQQueue(algorithms.fcfs) ] mlfq1 = mlfq.MLFQ(processes, queues, time_slot=5) schedule, process, run_time = mlfq1.simulate() assert schedule == [0] assert process[0].get_pid() == 0 assert run_time == 10 # Test with three processes. processes = [ ProcessControlBlock(0, 0, 20, 0), ProcessControlBlock(1, 15, 5, 0), ProcessControlBlock(2, 20, 5, 0) ] queues = [ mlfq.MLFQQueue(algorithms.round_robin, quanta=5), mlfq.MLFQQueue(algorithms.fcfs) ] mlfq1 = mlfq.MLFQ(processes, queues, time_slot=5) schedule, process, run_time = mlfq1.simulate() assert schedule == [0, 1, 2] assert process[0].get_pid() == 0 assert process[1].get_pid() == 1 assert process[2].get_pid() == 2 assert run_time == 30 # Test with gaps in between processes. processes = [ ProcessControlBlock(0, 0, 5, 0), ProcessControlBlock(1, 10, 5, 0) ] queues = [ mlfq.MLFQQueue(algorithms.round_robin, quanta=5), mlfq.MLFQQueue(algorithms.fcfs) ] mlfq1 = mlfq.MLFQ(processes, queues, time_slot=5) schedule, process, run_time = mlfq1.simulate() assert schedule == [0, 1] assert process[0].get_pid() == 0 assert process[1].get_pid() == 1 assert run_time == 15 # Invoke processes to remain in the same queue. processes = [ ProcessControlBlock(0, 0, 10, 0), ProcessControlBlock(1, 0, 10, 0), ProcessControlBlock(2, 10, 10, 0) ] queues = [ mlfq.MLFQQueue(algorithms.round_robin, quanta=5), mlfq.MLFQQueue(algorithms.round_robin, quanta=5) ] mlfq1 = mlfq.MLFQ(processes, queues) schedule, process, run_time = mlfq1.simulate() assert schedule == [0, 1, 2, 0, 1, 2] assert process[0].get_pid() == 0 assert process[1].get_pid() == 1 assert process[2].get_pid() == 2 assert run_time == 30