def test_loss_calculator_can_add_packet_entry(self, freezer): loss_calculator = Loss_Calculator() packet = SentPackage('1', datetime.now()) loss_calculator.add(packet) assert packet in loss_calculator
class Data_Presenter(object): __instance__ = None def __init__(self): if Data_Presenter.__instance__ is not None: raise RuntimeError( "Cannot init class twice, as it is as singelton") else: self.data = Loss_Calculator() Data_Presenter.__instance__ = self @classmethod def get_instance(cls): """ :rtype: Data_Presenter """ if cls.__instance__ is None: Data_Presenter() return cls.__instance__ @classmethod def clear_instance(cls): cls.__instance__ = None def append(self, packet): self.data.add(packet) def latest_packages(self, amount=1) -> [PacketEntry]: return self.data.get_last_packages(amount) def get_time_table(self) -> TimeTable: return self.data.time_table def get_packet_table(self): return self.data.packet_table
def test_sent_package_added_to_calculator_is_not_received(self): loss_calculator = Loss_Calculator() time_stamp = datetime.now() package = SentPackage('1', time_stamp) loss_calculator.add(package) assert not loss_calculator[package.id].is_received()
def test_loss_calculator_counts_received_packages_at_timestamp(self): loss_calculator = Loss_Calculator() time_stamp = datetime.now() loss_calculator.add(SentPackage('1', time_stamp)) loss_calculator.add(ReceivePackage('1', '1', time_stamp)) assert loss_calculator[time_stamp].received == 1
def test_loss_calculator_counts_sent_packages_at_timestamp(self): loss_calculator = Loss_Calculator() time_stamp = datetime.now() loss_calculator.add(SentPackage('1', time_stamp)) loss_calculator.add(SentPackage('2', time_stamp)) assert loss_calculator[time_stamp].sent == 2
def test_get_n_latest_packages_where_n_is_equal_to_length(self): loss_calculator = Loss_Calculator() loss_calculator.add(SentPackage("1")) loss_calculator.add(SentPackage("2")) packages = loss_calculator.get_last_packages(2) assert len(packages) == 2
def test_get_last_package(self): loss_calculator = Loss_Calculator() package = SentPackage("1") loss_calculator.add(package) last_package = loss_calculator.get_last_packages(1).pop() assert last_package.sent_at == package.time
def test_loss_calculator_can_add_two_entries_at_same_timestamp(self, freezer): loss_calculator = Loss_Calculator() at_zero_second = datetime.now() at_zero_point_5_second = datetime.now() packet_at_zero_second = SentPackage('1', at_zero_second) packet_at_zero_point_5_second = SentPackage('2', at_zero_point_5_second) loss_calculator.add(packet_at_zero_second) loss_calculator.add(packet_at_zero_point_5_second) assert loss_calculator[at_zero_second].sent == 2
def test_two_entries_with_more_than_a_second_apart_should_not_be_in_same_key(self, freezer): loss_calculator = Loss_Calculator() first_time = datetime.now() second_time = datetime.now() packet_one = SentPackage('1', first_time) packet_two = SentPackage('2', second_time) loss_calculator.add(packet_one) loss_calculator.add(packet_two) assert loss_calculator[first_time].sent == 1 assert loss_calculator[second_time].sent == 1
def test_get_n_latest_packages(self): loss_calculator = Loss_Calculator() p1 = SentPackage("1", datetime.now()) p2 = SentPackage("2") p3 = SentPackage("3") loss_calculator.add(p1) loss_calculator.add(p2) loss_calculator.add(p3) packages = loss_calculator.get_last_packages(2) assert not any(pack.sent_at == p1.time for pack in packages) assert any(pack.sent_at == p2.time for pack in packages) assert any(pack.sent_at == p3.time for pack in packages)
def test_loss_calculator_counts_loss_when_packet_do_not_return(self): loss_calculator = Loss_Calculator() time_stamp = datetime.now() loss_calculator.add(SentPackage('1', time_stamp)) loss_calculator.add(SentPackage('2', time_stamp)) loss_calculator.add(SentPackage('3', time_stamp)) loss_calculator.add(ReceivePackage('1', '1', time_stamp)) loss_calculator.add(ReceivePackage('3', '2', time_stamp)) assert loss_calculator[time_stamp].loss == 1