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_package_info_at_timestamp(self): data_presenter = Data_Presenter.get_instance() time = datetime.now() data_presenter.append(SentPackage("1", time)) data_presenter.append(SentPackage("2", time)) # get time table package_info = data_presenter.get_time_table() assert package_info[time].sent == 2
def test_calculate_packet_loss_should_answer_in_pct_of_lost_packets(self): server = Server() time = datetime.now() server.save_entry(SentPackage("1", time)) server.save_entry(SentPackage("2", time)) server.save_entry(ReceivePackage("1", "1", time)) assert server.calculate_packet_loss_in_pct(time) == 0.5
def test_get_latest_data_by_amount_of_entries(self): data_presenter = Data_Presenter.get_instance() test_data_1 = SentPackage("1") test_data_2 = SentPackage("2") data_presenter.append(test_data_1) data_presenter.append(test_data_2) latest_package = data_presenter.latest_packages(1).pop() assert latest_package.sent_at == test_data_2.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_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
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_can_add_packet_entry(self, freezer): loss_calculator = Loss_Calculator() packet = SentPackage('1', datetime.now()) loss_calculator.add(packet) assert packet in loss_calculator
def test_data_presenter_can_take_an_argument(self): test_data = SentPackage("1") data_presenter = Data_Presenter.get_instance() data_presenter.append(test_data) assert data_presenter.latest_packages().pop().sent_at == test_data.time
def send_packet(self, address): packet = "%d" % self.id package = SentPackage(packet, self.timestamp()) self.save_entry(package) self.id += 1 self.server_socket.sendto(packet.encode(), address)
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_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_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_data_from_different_get_instance(self): data_presenter1 = Data_Presenter.get_instance() data_presenter2 = Data_Presenter.get_instance() test_data = SentPackage("1") data_presenter1.append(test_data) from_different_object = data_presenter2.latest_packages().pop() assert from_different_object.sent_at == test_data.time
def test_append_method_appends_data(self): data_presenter = Data_Presenter.get_instance() test_data_1 = SentPackage("1") test_data_2 = ReceivePackage("1", "1") data_presenter.append(test_data_1) data_presenter.append(test_data_2) latest = data_presenter.latest_packages().pop() assert latest.sent_at == test_data_1.time assert latest.received_at == test_data_2.time