Exemple #1
0
    def test_endService_empty_queue(self):
        mock_input = Queue()
        mock_input.getQueueLength = MagicMock(return_value=0)
        self.processorObj.addInput(mock_input)
        self.processorObj.hostedEntity = 1

        self.assertFalse(self.processorObj.isIdle(),
                         "The processor shouldn't be idle")
        self.processorObj.endService()
        self.assertTrue(self.processorObj.isIdle(),
                        "The processor should be idle")
Exemple #2
0
    def test_enqueue(self):
        q = Queue()

        self.assertTrue(q.is_empty())
        q.enqueue(1)
        self.assertFalse(q.is_empty())

        self.assertEqual(q.peek(), 1)

        q.enqueue(2)
        self.assertNotEqual(q.peek(), 2)
    def test_nextArrival_unlimited_capacity_idle_output(self):
        self.queueObj = Queue(0)

        output_queue = Queue(0)

        self.queueObj.addOutput(output_queue)

        self.queueObj.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 0,
                         "The queue can transmit the entity")
    def test_can_host_entity_unlimited_busy_output(self):
        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(0)

        self.assertEqual(self.queueObj.getQueueLength(), 1,
                         "The queue mustn't be empty")
Exemple #5
0
    def __init__(self, page_sequence, num_of_frames):

        # Initializing a list attribute of page sequences
        self.sequence = page_sequence

        # Initializing the number of page faults
        self.page_fault = 0

        # Initializing the number off frames
        self.frame_size = num_of_frames

        # Creating a queue to use
        self.queue = Queue()
    def __init__(self, page_sequence, num_of_frames):

        # Initializing a list attribute containing the sequence of pages
        self.sequence = []
        self.sequence = page_sequence

        # Initializing the variable holding the number of page faults
        self.page_fault = 0

        # Initializing the variable telling us the number of frames to use
        self.frame_size = num_of_frames

        # Creating the queue data structure to be used
        self.queue = Queue()
Exemple #7
0
 def test_addOutput(self):
     source = Source(self.coreObj)
     self.assertIsNone(source.outputModule,
                       "The output module should be empty")
     source.addOutput(Queue())
     self.assertIsNotNone(source.outputModule,
                          "The output module shouldn't be empty")
    def test_can_host_entity_unlimited_not_busy_output(self):
        queueObj2 = Queue(0)
        queueObj = Queue(0)
        queueObj.addOutput(queueObj2)

        queueObj.nextArrival(0)

        self.assertEqual(queueObj.getQueueLength(), 0,
                         "The queue must be empty")
Exemple #9
0
 def test_executeEvent_Not_NEXT_ARRIVAL(self):
     source = Source(self.coreObj)
     source.addOutput(Queue(1))
     self.assertEqual(self.coreObj.entitiesSystem, 0,
                      "The system should not have any entity")
     event = Event(eventName=Constants.END_SERVICE)
     source.executeEvent(event)
     self.assertEqual(self.coreObj.entitiesSystem, 0,
                      "The system should not have any entity")
Exemple #10
0
def test_nonempty_queue():
    queue = Queue()
    queue.enqueue(1)
    queue.enqueue(2)
    queue.enqueue(3)

    assert len(queue) == 3
    assert queue.dequeue() == 1

    assert queue.front() == 2

    assert len(queue) == 2
    assert queue.dequeue() == 2

    assert len(queue) == 1
    assert queue.dequeue() == 3

    assert len(queue) == 0
Exemple #11
0
 def test_executeEvent_before_SIMULATION_DURATION(self):
     source = Source(self.coreObj)
     queue = Queue(1)
     source.addOutput(queue)
     self.assertTrue(self.coreObj.eventsList.empty(),
                     "The system should not have any entity")
     source.nextArrival()
     self.assertFalse(self.coreObj.eventsList.empty(),
                      "The system should have one entity")
Exemple #12
0
 def test_executeEvent_Not_None_output(self):
     source = Source(self.coreObj)
     queue = Queue(1)
     source.addOutput(queue)
     self.assertEqual(len(queue.entitiesList), 0,
                      "The queue should not have any entity")
     source.nextArrival()
     self.assertEqual(len(queue.entitiesList), 1,
                      "The queue should have one entity")
Exemple #13
0
 def test_executeEvent_after_SIMULATION_DURATION(self):
     self.coreObj.currentTime = Constants.SIMULATION_FINAL_TIME
     source = Source(self.coreObj)
     queue = Queue(1)
     source.addOutput(queue)
     self.assertTrue(self.coreObj.eventsList.empty(),
                     "The system should not have any entity")
     source.nextArrival()
     self.assertTrue(self.coreObj.eventsList.empty(),
                     "The system should not have any entity")
Exemple #14
0
    def __init__(self, inputFeed):
        '''
        In the initiation of a scheduler we set up the variables that will
        be needed, it takes a process feed as input so it can work with a
        group of processes, note that it copies the feed so that it doesn't
        change the feed for other schedulers. It also creates two queues,
        the arrival queue, which stores processes for which their arrival time
        has not yet occured, and the waiting queue which is where the processes
        that have arrived go.
        '''

        processFeed = copy.deepcopy(inputFeed)
        processFeed.sort(key=lambda x: x.getArrivalTime())
        processQ = Queue()
        for process in processFeed:
            processQ.add(process)

        self.arrivalQueue = processQ
        self.waitingQueue = Queue()

        self.clock = -1  # clock starts from -1 because the start of a tick method
        # always increments the clock at the start, so it will
        # effectively start from 0

        self.currentProcess = None
        self.finishedArray = [
        ]  # processes that have finished get put here to be returned at the end
        self.count = 0  #used for prememption if needed, such as in RRS when the time slice is 2
Exemple #15
0
    def __init__(self, parameters=None):
        if parameters is None:
            self.parameters = Parameters()
        else:
            self.parameters = parameters
        num_sources = Constants.DEFAULT_SOURCES
        num_processors = self.parameters.num_processors
        # Attributes initialization
        self.processors = []
        self.sources = []
        self.eventsList = PriorityQueue(0)  # maxsize = 0 (infinite)
        self.previousTime = Constants.SIMULATION_INITIAL_TIME
        self.currentTime = Constants.SIMULATION_INITIAL_TIME
        self.idleProcessors = 0
        self.serviceProcessors = 0
        self.entitiesSystem = 0
        self.service_per_shift = []
        self.service_per_total = []
        self.shift_durations = self.parameters.getParameters()[1]
        self.shift_next_time = self.shift_durations[0]
        self.shift_next_index = 1

        # Instance creation
        self.queue = Queue(Constants.SLOTS_BUFFER)
        self.parking = Queue(Constants.SLOTS_QUEUE)
        self.random = Random()
        for _ in range(0, num_processors):
            self.processors.append(Processor(self))
        for _ in range(0, num_sources):
            self.sources.append(Source(self))
        # Dependency injection
        for source in self.sources:
            source.addOutput(self.queue)  # source -> queue
        self.queue.addOutput(self.parking)  # queue -> parking
        self.parking.addInput(self.queue)  # parking <- queue
        for processor in self.processors:
            self.parking.addOutput(processor)  # parking -> processor
            processor.addInput(self.parking)  # processor <- parking
        self.output_file = None
        self.numberOfIdleProcessors = num_processors
Exemple #16
0
    def test_is_empty(self):
        q = Queue()

        self.assertTrue(q.is_empty())

        for i in range(1000):
            n = random.randint(0, 1000)
            q.enqueue(n)

        counter = 0
        while (not q.is_empty()):
            q.dequeue()
            counter += 1

        self.assertEqual(counter, 1000)
    def test_nextArrival_unlimited_capacity_idle_output(self):
        self.queueObj = Queue(0)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=True)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 0,
                         "The queue can transmit the entity")
    def test_nextArrival_unlimited_capacity_busy_output_has_already_an_entity(
            self):
        self.queueObj = Queue(0)

        output_queue = Queue(1)
        output_queue.nextArrival(1)

        self.queueObj.addOutput(output_queue)

        self.queueObj.nextArrival(1)
        self.queueObj.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 2,
                         "The queue cannot transmit the entity")
    def test_nextArrival_unlimited_capacity_busy_output_has_already_an_entity(
            self):
        self.queueObj = Queue(0)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(1)
        self.queueObj.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 2,
                         "The queue cannot transmit the entity")
    def test_nextArrival_limited_capacity_busy_output_has_no_elements_above_limit(
            self):
        self.queueObj = Queue(1)

        output_queue = Queue(1)
        output_queue.nextArrival(1)

        self.queueObj.addOutput(output_queue)

        self.queueObj.nextArrival(1)

        try:
            self.queueObj.nextArrival(1)
            self.fail()
        except:
            pass
    def test_nextArrival_limited_capacity_busy_output_has_no_elements_above_limit(
            self):
        self.queueObj = Queue(1)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(1)

        try:
            self.queueObj.nextArrival(1)
            self.fail()
        except:
            pass
Exemple #22
0
    def test_str(self):
        q = Queue()

        self.assertEqual(str(q), '')

        s = ''

        for i in range(50):
            r = random.randint(1, 200)

            q.enqueue(r)
            if (s == ''):
                s = str(r)
            else:
                s = str(r) + ' ' + s

        k = len(str(q.peek()))
        s = s[:-(k + 1)]
        q.dequeue()

        self.assertEqual(str(q), s)
Exemple #23
0
    def test_equals(self):
        qa = Queue()
        qb = Queue()

        self.assertEqual(qa, qb)

        for i in range(50):
            r = random.randint(1, 200)

            qa.enqueue(r)
            self.assertNotEqual(qa, qb)
            qb.enqueue(r)
            self.assertEqual(qa, qb)

        for i in range(50):
            qa.dequeue()
            self.assertNotEqual(qa, qb)
            qb.dequeue()
            self.assertEqual(qa, qb)
Exemple #24
0
    def test_dequeue(self):
        q = Queue()

        self.assertRaises(IndexError, q.dequeue)

        for i in range(10):
            q.enqueue(i)

        for i in range(9):
            self.assertEqual(q.peek(), i)
            q.dequeue()
            self.assertNotEqual(q.peek(), i)

        self.assertFalse(q.is_empty())
        q.dequeue()
        self.assertTrue(q.is_empty())
Exemple #25
0
    def test_peek(self):
        q = Queue()

        self.assertRaises(IndexError, q.peek)

        q.enqueue(0)
        self.assertEqual(q.peek(), 0)

        q.enqueue(1)
        self.assertNotEqual(q.peek(), 1)
        self.assertEqual(q.peek(), 0)

        q.dequeue()
        self.assertEqual(q.peek(), 1)
class TestQueue(unittest.TestCase):
    def setUp(self):
        self.queueObj = Queue()

    def tearDown(self):
        self.queueObj = None

    def test_can_host_entity_unlimited_not_busy_output(self):
        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=True)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(0)

        self.assertEqual(self.queueObj.getQueueLength(), 0,
                         "The queue must be empty")

    def test_can_host_entity_unlimited_busy_output(self):
        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(0)

        self.assertEqual(self.queueObj.getQueueLength(), 1,
                         "The queue mustn't be empty")

    def test_can_host_entity_limited_not_busy_output(self):
        self.queueObj = Queue(1)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=True)
        mock_output.nextArrival = MagicMock()
        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(0)
        self.queueObj.nextArrival(0)
        self.queueObj.nextArrival(0)

        self.assertEqual(self.queueObj.getQueueLength(), 0,
                         "The queue must be empty")

    def test_can_host_entity_limited_busy_output(self):
        self.queueObj = Queue(1)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()
        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(0)

        self.assertEqual(self.queueObj.getQueueLength(), 1,
                         "The queue mustn't be empty")
        try:
            self.queueObj.nextArrival(0)
            self.fail()
        except:
            pass

    def test_canHostEntity_unlimited_queue(self):
        self.queueObj.nextArrival(0)
        self.queueObj.nextArrival(0)
        self.queueObj.nextArrival(0)

        self.assertTrue(self.queueObj.canHostEntity(),
                        "The queue should hold all elements")

    def test_canHostEntity_limited_queue(self):
        self.queueObj = Queue(1)
        self.queueObj.nextArrival(0)

        self.assertFalse(self.queueObj.canHostEntity(),
                         "The queue shouldn't hold more elements")

    def test_getEntity(self):
        # def getEntity(self, outputModule)
        # queue2 <-> main queue -> mock output
        self.queueObj = Queue(1)

        queueObj2 = Queue()
        queueObj2.addOutput(self.queueObj)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addInput(queueObj2)
        self.queueObj.addOutput(mock_output)

        queueObj2.nextArrival(1)
        queueObj2.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 1,
                         "The queue mustn't be empty")
        self.assertEqual(queueObj2.getQueueLength(), 1,
                         "The queue mustn't be empty")

        # The exception is risen because que mock_output queue cannot host any entity
        try:
            self.queueObj.getEntity(mock_output)
        except:
            pass
        try:
            self.queueObj.getEntity(mock_output)
        except:
            pass

        self.assertEqual(self.queueObj.getQueueLength(), 0,
                         "The queue must be empty")
        self.assertEqual(queueObj2.getQueueLength(), 0,
                         "The queue must be empty")

    def test_nextArrival_unlimited_capacity_no_output(self):
        self.queueObj = Queue(0)
        self.queueObj.nextArrival(1)
        self.assertEqual(self.queueObj.getQueueLength(), 1,
                         "The queue cannot transmit the entity")

    def test_nextArrival_unlimited_capacity_busy_output(self):
        self.queueObj = Queue(0)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 1,
                         "The queue cannot transmit the entity")

    def test_nextArrival_unlimited_capacity_idle_output(self):
        self.queueObj = Queue(0)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=True)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 0,
                         "The queue can transmit the entity")

    def test_nextArrival_unlimited_capacity_busy_output_has_already_an_entity(
            self):
        self.queueObj = Queue(0)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(1)
        self.queueObj.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 2,
                         "The queue cannot transmit the entity")

    def test_nextArrival_limited_capacity_busy_output_has_no_elements_above_limit(
            self):
        self.queueObj = Queue(1)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addOutput(mock_output)

        self.queueObj.nextArrival(1)

        try:
            self.queueObj.nextArrival(1)
            self.fail()
        except:
            pass
Exemple #27
0
class Scheduler:
    def __init__(self, inputFeed):
        '''
        In the initiation of a scheduler we set up the variables that will
        be needed, it takes a process feed as input so it can work with a
        group of processes, note that it copies the feed so that it doesn't
        change the feed for other schedulers. It also creates two queues,
        the arrival queue, which stores processes for which their arrival time
        has not yet occured, and the waiting queue which is where the processes
        that have arrived go.
        '''

        processFeed = copy.deepcopy(inputFeed)
        processFeed.sort(key=lambda x: x.getArrivalTime())
        processQ = Queue()
        for process in processFeed:
            processQ.add(process)

        self.arrivalQueue = processQ
        self.waitingQueue = Queue()

        self.clock = -1  # clock starts from -1 because the start of a tick method
        # always increments the clock at the start, so it will
        # effectively start from 0

        self.currentProcess = None
        self.finishedArray = [
        ]  # processes that have finished get put here to be returned at the end
        self.count = 0  #used for prememption if needed, such as in RRS when the time slice is 2

    def isFinished(self):
        '''
        useful to know when a sheduler is done processing all the processes,
        this has a few elements, but essentially we know the scheduler is done
        only when both the queues are empty, and there is no current process
        '''
        return self.arrivalQueue.isEmpty() and self.waitingQueue.isEmpty(
        ) and self.currentProcess is None

    def getAvgTurnaroundTime(self):
        '''
        After asserting the scheduler is finished, it goes through each process
        in the finished array and increments the total turnaround time
        by the current processes turnaround time. It then divides this total by
        the number of processes to get the average turnaround time
        '''
        assert (self.isFinished())
        totalTurnaroundTime = 0
        for process in self.finishedArray:
            totalTurnaroundTime += process.getTurnAroundTime()

        avgTurnaroundTime = totalTurnaroundTime / len(self.finishedArray)
        return avgTurnaroundTime

    def getAvgWaitingTime(self):
        '''
        After asserting the scheduler is finished, it goes through each process
        in the finished array and increments the total waiting time
        by the current processes waiting time. It then divides this total by
        the number of processes to get the average waiting time
        '''
        assert (self.isFinished())
        totalWaitingTime = 0
        for process in self.finishedArray:
            totalWaitingTime += process.getWaitingTime()

        avgWaitingTime = totalWaitingTime / len(self.finishedArray)
        return avgWaitingTime

    def getThroughput(self):
        '''
        After asserting the scheduler is finished it calculates the throughput,
        the throughput is the number of processes completed per second,
        since we know the clock wont be incremented after all the processes are
        done, this is simply the number of processes divided by the clock (since
        every process in the finished array must be finished).
        '''
        assert (self.isFinished())
        throughput = len(self.finishedArray) / self.clock
        return throughput
    def test_getEntity(self):
        # def getEntity(self, outputModule)
        # queue2 <-> main queue -> mock output
        self.queueObj = Queue(1)

        queueObj2 = Queue()
        queueObj2.addOutput(self.queueObj)

        mock_output = Queue()
        mock_output.canHostEntity = MagicMock(return_value=False)
        mock_output.nextArrival = MagicMock()

        self.queueObj.addInput(queueObj2)
        self.queueObj.addOutput(mock_output)

        queueObj2.nextArrival(1)
        queueObj2.nextArrival(1)

        self.assertEqual(self.queueObj.getQueueLength(), 1,
                         "The queue mustn't be empty")
        self.assertEqual(queueObj2.getQueueLength(), 1,
                         "The queue mustn't be empty")

        # The exception is risen because que mock_output queue cannot host any entity
        try:
            self.queueObj.getEntity(mock_output)
        except:
            pass
        try:
            self.queueObj.getEntity(mock_output)
        except:
            pass

        self.assertEqual(self.queueObj.getQueueLength(), 0,
                         "The queue must be empty")
        self.assertEqual(queueObj2.getQueueLength(), 0,
                         "The queue must be empty")
    def test_canHostEntity_limited_queue(self):
        self.queueObj = Queue(1)
        self.queueObj.nextArrival(0)

        self.assertFalse(self.queueObj.canHostEntity(),
                         "The queue shouldn't hold more elements")
 def setUp(self):
     self.queueObj = Queue()