def test_event_chain(self): """ Test module EventChain. Add and remove SimEvents and check the correct order. """ # priorities: SC = 0, CA = 1, ST = 2 e = EventChain() e.insert(CustomerArrival(None, 10)) e.insert(SimulationTermination(None, 10)) e.insert(ServiceCompletion(None, 10)) e.insert(CustomerArrival(None, 5)) e.insert(ServiceCompletion(None, 2)) results = [[2, 0], [5, 1], [10, 0], [10, 1], [10, 2]] for r in results: ev = e.remove_oldest_event() self.assertEqual( [ev.timestamp, ev.priority], r, msg= "Error in EventChain or SimEvent. Events are sorted or returned in the wrong order." ) self.assertEqual( len(e.event_list), 0, msg="Error in EventChain or SimEvent. EventChain should be empty.")
def test_service_completion(self): """ Test ServiceCompletion process function. Check, whether processing of service completion events works as desired (making server idle again or triggering new service start). """ DESTest.sim.reset() # initialize system DESTest.sim.system_state.buffer_content = 1 DESTest.sim.system_state.server_busy = True self.assertEqual([DESTest.sim.system_state.buffer_content, DESTest.sim.system_state.server_busy], [1, True], msg="Error in ServiceCompletion test. Initialization failed.") # first service completion should insert new service completion and take packet from queue ServiceCompletion(DESTest.sim, 0).process() self.assertEqual([DESTest.sim.system_state.buffer_content, DESTest.sim.system_state.server_busy], [0, True], msg="Error in ServiceCompletion. Server should be busy and queue should be empty.") self.assertEqual(len(DESTest.sim.event_chain.event_list), 1, msg="Error in ServiceCompletion. Wrong number of new SimEvents created in process function.") # second service completion should make server idle again ServiceCompletion(DESTest.sim, 0).process() self.assertEqual([DESTest.sim.system_state.buffer_content, DESTest.sim.system_state.server_busy], [0, False], msg="Error in ServiceCompletion. Server should be idle and queue should be empty.") self.assertEqual(len(DESTest.sim.event_chain.event_list), 1, msg="Error in ServiceCompletion. Wrong number of new SimEvents created in process function.")
def pause_service(self): """ If the buffer is not empty, take the next packet from there and serve it. :return: True if buffer is not empty and a stored packet is being served. """ #self.counter_collection.count_throughput(self.served_packet.get_throughput()) # firstly throughput is calculated #self.event_chain.event_list.remove(ServiceCompletion(self, self.served_packet.t_finish)) self.event_chain.remove_event(ServiceCompletion(self, self.served_packet.t_finish)) self.served_packet.pause_service() self.server_busy = False
def remove_oldest_packet(self): # removes the oldest packet from server if self.served_packet == None: if self.buffer.is_empty(): raise RuntimeError("ERROR: buffer empty, cant remove the packet.") else: self.removed_packet = self.buffer.remove() else: if self.served_packet.served: self.pause_service() self.served_packet = None if self.start_service(): self.event_chain.insert(ServiceCompletion(self.slicesim, self.served_packet.t_finish)) else: self.served_packet = None self.server_state.packet_removed()