class MultipleEndsTest(unittest.TestCase):
    def __init__(self, *args):
        super(MultipleEndsTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_multiple_sources_single_sink(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw=1.0 * 10**6,
                                         pktsize=1500,
                                         duration=5.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test2 = self.srcnode.create_test(bw=1.0 * 10**6,
                                         pktsize=1500,
                                         duration=10.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test3 = self.srcnode.create_test(bw=1.0 * 10**6,
                                         pktsize=1500,
                                         duration=15.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test1.start()
        test2.start()
        test3.start()

        time.sleep(3.0)

        bwsum = test1.bandwidth.movavg(5) + test2.bandwidth.movavg(
            5) + test3.bandwidth.movavg(5)
        self.assertTrue(
            bwsum > 0.8e6 and bwsum < 1.2e6,
            "Expected the combined bandwidth of three tests to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (bwsum))

        time.sleep(2.5)

        self.assertTrue(test1.done,
                        "Expected test1 to have finished at time = 5.5s")
        self.assertFalse(test2.done,
                         "Expected test2 to be still running at time = 5.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 5.5s")

        time.sleep(2.5)

        bwsum = test2.bandwidth.movavg(5) + test3.bandwidth.movavg(5)
        self.assertTrue(
            bwsum > 0.8e6 and bwsum < 1.2e6,
            "Expected the combined bandwidth of two tests to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (bwsum))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 10.5s")

        time.sleep(2.5)

        self.assertTrue(
            test3.bandwidth.movavg(5) > 0.80e6
            and test3.bandwidth.movavg(5) < 1.20e6,
            "Expected test3 bandwidth to be ~1Mbit/s (with 1 test running concurrently)"
            + ", instead it was %.2fMbit/s" %
            (test3.bandwidth.movavg(5) / 1e6))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")

        self.assertTrue(
            test3.latency.duration() > test2.latency.duration()
            and test2.latency.duration() > test1.latency.duration(),
            "Expected test3 duration (%.2fs) to be greater than test2 (%.2fs)"
            " and still greater than test1 (%.2fs)" %
            (test3.latency.duration(), test2.latency.duration(),
             test1.latency.duration()))

    def test_multiple_sinks(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw=5.0 * 10**6,
                                         pktsize=1500,
                                         duration=3.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12345,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test2 = self.srcnode.create_test(bw=5.0 * 10**6,
                                         pktsize=1500,
                                         duration=3.0,
                                         sink_ip="127.0.0.1",
                                         sink_port=12346,
                                         bw_type=LinktestGoal.BW_CONSTANT,
                                         update_interval=0.2)
        test1.start()
        test2.start()
        time.sleep(3.5)

        self.assertTrue(test1.done,
                        "Expected test2 to have finished at time = 3.5s")
        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 3.5s")

        self.assertTrue(
            test2.overall_bandwidth > 4 * test1.overall_bandwidth,
            "Expected test2 bw (%.2fMbit/s) to be at least 4x larger"
            " than test1 bw (%.2fMbit/s)" %
            (test2.overall_bandwidth / 1e6, test1.overall_bandwidth / 1e6))
class ConstantBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(ConstantBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_basic(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 3.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT)
        test.start()
        time.sleep(3.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(test.overall_latency > 0.0 and test.overall_latency < 0.005,
                        "Expected latency on loopback interface to be positive and under 5ms, instead it was %.2fms"%
                        (test.overall_latency * 1000))
        self.assertAlmostEqual(test.overall_loss, 0.0, 2,
                               "Expected packet loss on loopback interface to be zero, instead it was %.2f%%"%
                               (test.overall_loss))
        self.assertTrue(test.overall_bandwidth > 0.9 * 10**6,
                        "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"%
                        (test.overall_bandwidth/1e6))
                    

    def test_bw_measurement(self):
        test = self.srcnode.create_test(bw = 3.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(test.overall_bandwidth < 1e6 and test.overall_bandwidth > 0.9e6,
                        "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"%
                        (test.overall_bandwidth/1e6))

    def test_bw_measurement_variable_bw(self):   
        test = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.15)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(2.0)
        self.assertTrue(test.bandwidth.movavg(5) < 1.15e6 and test.bandwidth.movavg(5) > 0.85e6,
                        "Expected useful bandwidth on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(4e6)
        time.sleep(2.0)
        self.assertTrue(test.bandwidth.movavg(5) < 4.2e6 and test.bandwidth.movavg(5) > 3.8e6,
                        "Expected useful bandwidth on loopback interface to be ~4Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(0.25e6)
        time.sleep(2.0)
        self.assertTrue(test.bandwidth.movavg(5) < 0.35e6 and test.bandwidth.movavg(5) > 0.15e6,
                        "Expected useful bandwidth on loopback interface to be ~0.25Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))

    def test_loss_measurement(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 200, duration = 12.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.15)
        self.tc.set_latency_loss(loss = 5)
        test.start()
        time.sleep(4.0)
        self.assertTrue(test.loss.movavg() < 8.5 and test.loss.movavg() > 1.5,
                        "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"%
                        (test.loss.movavg()))
        self.tc.set_latency_loss(loss = 20)
        time.sleep(4.0)
        self.assertTrue(test.loss.movavg() < 25.0 and test.loss.movavg() > 15.0,
                        "Expected packet loss on loopback interface to be ~20%%, instead it was %.2f%%"%
                        (test.loss.movavg()))
        self.tc.set_latency_loss(loss = 0)
        time.sleep(4.0)
        self.assertTrue(test.loss.movavg() < 1.0,
                        "Expected packet loss on loopback interface to be ~0%%, instead it was %.2f%%"%
                        (test.loss.movavg()))

    def test_latency_measurement(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 200, duration = 6.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.2)
        test.start()
        time.sleep(1.5)
        lo_latency = test.latency.movavg(3)        
        self.tc.set_latency_loss(latency = 0.02)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(added_latency > 0.015 and added_latency < 0.025,
                        "Expected added latency on loopback interface to be ~20ms, instead it was %.2fms"%
                        (added_latency * 1000))
        self.tc.set_latency_loss(latency = 0.06)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(added_latency > 0.052 and added_latency < 0.068,
                        "Expected added latency on loopback interface to be ~60ms, instead it was %.2fms"%
                        (added_latency * 1000))
        self.tc.set_latency_loss(latency = 0)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(added_latency < 0.002,
                        "Expected added latency on loopback interface to be ~0ms, instead it was %.2fms"%
                        (added_latency * 1000))

    def test_latency_loss(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 200, duration = 3.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_CONSTANT, update_interval = 0.2)
        self.tc.set_latency_loss(loss = 5, latency = 0.05)
        test.start()
        time.sleep(3.5)
        self.assertTrue(test.loss.movavg(10) < 7.5 and test.loss.movavg(10) > 2.5,
                        "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"%
                        (test.loss.movavg(10)))
        self.assertTrue(test.latency.movavg(10) < 0.07 and test.latency.movavg(10) > 0.05,
                        "Expected latency on loopback interface to be ~60ms, instead it was %.2fms"%
                        (test.latency.movavg(10)* 1000))
class AdaptiveBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(AdaptiveBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_ramp_up(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_ADAPTIVE,
                                        update_interval=0.2,
                                        latency_threshold=0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(
            test.bandwidth.movavg(5) > 4.3e6
            and test.bandwidth.movavg(5) < 5.7e6,
            "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_ramp_down(self):
        test = self.srcnode.create_test(bw=5.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_ADAPTIVE,
                                        update_interval=0.2,
                                        latency_threshold=0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(2.0)
        self.tc.set_rate_limit(1e6)
        time.sleep(3.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 0.6e6
            and test.bandwidth.movavg(5) < 1.4e6,
            "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_varying_capacity(self):
        test = self.srcnode.create_test(bw=2 * 10**6,
                                        pktsize=1500,
                                        duration=15.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_ADAPTIVE,
                                        update_interval=0.2,
                                        latency_threshold=0.1)
        self.tc.set_rate_limit(10e6)
        test.start()
        time.sleep(4.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 9e6 and test.bandwidth.movavg(5) < 11e6,
            "Expected capacity on loopback interface to be ~10Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(1e6)
        time.sleep(4.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 0.6e6
            and test.bandwidth.movavg(5) < 1.4e6,
            "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(5e6)
        time.sleep(6.0)
        self.assertTrue(
            test.bandwidth.movavg(5) > 3.5e6
            and test.bandwidth.movavg(5) < 5.7e6,
            "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_get_capacity(self):
        self.tc.set_rate_limit(5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip="127.0.0.1",
                                                  sink_port=12345,
                                                  latency_threshold=0.1)
        self.assertTrue(
            capacity > 4.3e6 and capacity < 5.7e6,
            "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"
            % (capacity / 1e6))
        self.tc.set_rate_limit(20e6)
        capacity = self.srcnode.get_link_capacity(sink_ip="127.0.0.1",
                                                  sink_port=12345,
                                                  latency_threshold=0.1)
        self.assertTrue(
            capacity > 15e6 and capacity < 25e6,
            "Expected capacity on loopback interface to be ~20Mbit/s, instead it was %.2fMbit/s"
            % (capacity / 1e6))
        self.tc.set_rate_limit(0.5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip="127.0.0.1",
                                                  sink_port=12345,
                                                  latency_threshold=0.1)
        self.assertTrue(
            capacity > 0.3e6 and capacity < 0.7e6,
            "Expected capacity on loopback interface to be ~0.5Mbit/s, instead it was %.2fMbit/s"
            % (capacity / 1e6))
class AdaptiveBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(AdaptiveBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_ramp_up(self):
        test = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_ADAPTIVE, update_interval = 0.2,
                                        latency_threshold = 0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(test.bandwidth.movavg(5) > 4.3e6 and test.bandwidth.movavg(5) < 5.7e6,
                   "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"%
                   (test.bandwidth.movavg(5)/1e6))

    def test_ramp_down(self):
        test = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 5.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_ADAPTIVE, update_interval = 0.2,
                                        latency_threshold = 0.1)
        self.tc.set_rate_limit(5e6)
        test.start()
        time.sleep(2.0)
        self.tc.set_rate_limit(1e6)
        time.sleep(3.0)
        self.assertTrue(test.bandwidth.movavg(5) > 0.6e6 and test.bandwidth.movavg(5) < 1.4e6,
                   "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"%
                   (test.bandwidth.movavg(5)/1e6))

    def test_varying_capacity(self):
        test = self.srcnode.create_test(bw = 2*10**6, pktsize = 1500, duration = 15.0,
                                        sink_ip = "127.0.0.1", sink_port = 12345,
                                        bw_type = LinktestGoal.BW_ADAPTIVE, update_interval = 0.2,
                                        latency_threshold = 0.1)
        self.tc.set_rate_limit(10e6)
        test.start()
        time.sleep(4.0)
        self.assertTrue(test.bandwidth.movavg(5) > 9e6 and test.bandwidth.movavg(5) < 11e6,
                        "Expected capacity on loopback interface to be ~10Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(1e6)
        time.sleep(4.0)
        self.assertTrue(test.bandwidth.movavg(5) > 0.6e6 and test.bandwidth.movavg(5) < 1.4e6,
                        "Expected capacity on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))
        self.tc.set_rate_limit(5e6)
        time.sleep(6.0)
        self.assertTrue(test.bandwidth.movavg(5) > 3.5e6 and test.bandwidth.movavg(5) < 5.7e6,
                        "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"%
                        (test.bandwidth.movavg(5)/1e6))

    def test_get_capacity(self):
        self.tc.set_rate_limit(5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip = "127.0.0.1", sink_port = 12345, latency_threshold = 0.1)
        self.assertTrue(capacity > 4.3e6 and capacity < 5.7e6,
                        "Expected capacity on loopback interface to be ~5Mbit/s, instead it was %.2fMbit/s"%
                        (capacity/1e6))
        self.tc.set_rate_limit(20e6)
        capacity = self.srcnode.get_link_capacity(sink_ip = "127.0.0.1", sink_port = 12345, latency_threshold = 0.1)
        self.assertTrue(capacity > 15e6 and capacity < 25e6,
                        "Expected capacity on loopback interface to be ~20Mbit/s, instead it was %.2fMbit/s"%
                        (capacity/1e6))
        self.tc.set_rate_limit(0.5e6)
        capacity = self.srcnode.get_link_capacity(sink_ip = "127.0.0.1", sink_port = 12345, latency_threshold = 0.1)
        self.assertTrue(capacity > 0.3e6 and capacity < 0.7e6,
                        "Expected capacity on loopback interface to be ~0.5Mbit/s, instead it was %.2fMbit/s"%
                        (capacity/1e6))
class ConstantBandwidthTest(unittest.TestCase):
    def __init__(self, *args):
        super(ConstantBandwidthTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_basic(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=1500,
                                        duration=3.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT)
        test.start()
        time.sleep(3.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(
            test.overall_latency > 0.0 and test.overall_latency < 0.005,
            "Expected latency on loopback interface to be positive and under 5ms, instead it was %.2fms"
            % (test.overall_latency * 1000))
        self.assertAlmostEqual(
            test.overall_loss, 0.0, 2,
            "Expected packet loss on loopback interface to be zero, instead it was %.2f%%"
            % (test.overall_loss))
        self.assertTrue(
            test.overall_bandwidth > 0.9 * 10**6,
            "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"
            % (test.overall_bandwidth / 1e6))

    def test_bw_measurement(self):
        test = self.srcnode.create_test(bw=3.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(5.5)
        self.assertTrue(test.done, "Test should have finished already")
        self.assertTrue(
            test.overall_bandwidth < 1e6 and test.overall_bandwidth > 0.9e6,
            "Expected useful bandwidth on loopback interface to be at least 0.9Mbit/s, instead it was %.2fMbit/s"
            % (test.overall_bandwidth / 1e6))

    def test_bw_measurement_variable_bw(self):
        test = self.srcnode.create_test(bw=5.0 * 10**6,
                                        pktsize=1500,
                                        duration=5.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.15)
        self.tc.set_rate_limit(1e6)
        test.start()
        time.sleep(2.0)
        self.assertTrue(
            test.bandwidth.movavg(5) < 1.15e6
            and test.bandwidth.movavg(5) > 0.85e6,
            "Expected useful bandwidth on loopback interface to be ~1Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(4e6)
        time.sleep(2.0)
        self.assertTrue(
            test.bandwidth.movavg(5) < 4.2e6
            and test.bandwidth.movavg(5) > 3.8e6,
            "Expected useful bandwidth on loopback interface to be ~4Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))
        self.tc.set_rate_limit(0.25e6)
        time.sleep(2.0)
        self.assertTrue(
            test.bandwidth.movavg(5) < 0.35e6
            and test.bandwidth.movavg(5) > 0.15e6,
            "Expected useful bandwidth on loopback interface to be ~0.25Mbit/s, instead it was %.2fMbit/s"
            % (test.bandwidth.movavg(5) / 1e6))

    def test_loss_measurement(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=200,
                                        duration=12.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.15)
        self.tc.set_latency_loss(loss=5)
        test.start()
        time.sleep(4.0)
        self.assertTrue(
            test.loss.movavg() < 8.5 and test.loss.movavg() > 1.5,
            "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"
            % (test.loss.movavg()))
        self.tc.set_latency_loss(loss=20)
        time.sleep(4.0)
        self.assertTrue(
            test.loss.movavg() < 25.0 and test.loss.movavg() > 15.0,
            "Expected packet loss on loopback interface to be ~20%%, instead it was %.2f%%"
            % (test.loss.movavg()))
        self.tc.set_latency_loss(loss=0)
        time.sleep(4.0)
        self.assertTrue(
            test.loss.movavg() < 1.0,
            "Expected packet loss on loopback interface to be ~0%%, instead it was %.2f%%"
            % (test.loss.movavg()))

    def test_latency_measurement(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=200,
                                        duration=6.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.2)
        test.start()
        time.sleep(1.5)
        lo_latency = test.latency.movavg(3)
        self.tc.set_latency_loss(latency=0.02)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(
            added_latency > 0.015 and added_latency < 0.025,
            "Expected added latency on loopback interface to be ~20ms, instead it was %.2fms"
            % (added_latency * 1000))
        self.tc.set_latency_loss(latency=0.06)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(
            added_latency > 0.052 and added_latency < 0.068,
            "Expected added latency on loopback interface to be ~60ms, instead it was %.2fms"
            % (added_latency * 1000))
        self.tc.set_latency_loss(latency=0)
        time.sleep(1.5)
        added_latency = test.latency.movavg(3) - lo_latency
        self.assertTrue(
            added_latency < 0.002,
            "Expected added latency on loopback interface to be ~0ms, instead it was %.2fms"
            % (added_latency * 1000))

    def test_latency_loss(self):
        test = self.srcnode.create_test(bw=1.0 * 10**6,
                                        pktsize=200,
                                        duration=3.0,
                                        sink_ip="127.0.0.1",
                                        sink_port=12345,
                                        bw_type=LinktestGoal.BW_CONSTANT,
                                        update_interval=0.2)
        self.tc.set_latency_loss(loss=5, latency=0.05)
        test.start()
        time.sleep(3.5)
        self.assertTrue(
            test.loss.movavg(10) < 7.5 and test.loss.movavg(10) > 2.5,
            "Expected packet loss on loopback interface to be ~5%%, instead it was %.2f%%"
            % (test.loss.movavg(10)))
        self.assertTrue(
            test.latency.movavg(10) < 0.07 and test.latency.movavg(10) > 0.05,
            "Expected latency on loopback interface to be ~60ms, instead it was %.2fms"
            % (test.latency.movavg(10) * 1000))
class MultipleEndsTest(unittest.TestCase):
    def __init__(self, *args):
        super(MultipleEndsTest, self).__init__(*args)
        rospy.init_node('network_monitor_udp_test')
        self.srcnode = UdpmonsourceHandle('performance_test')
        self.tc = TcPortControl(self)
        self.tc.reset()

    def setUp(self):
        self.srcnode.cancel_all_tests()
        self.tc.init()

    def tearDown(self):
        self.tc.reset()

    def test_multiple_sources_single_sink(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 5.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test2 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 10.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test3 = self.srcnode.create_test(bw = 1.0*10**6, pktsize = 1500, duration = 15.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test1.start()
        test2.start()
        test3.start()

        time.sleep(3.0)

        bwsum = test1.bandwidth.movavg(5) + test2.bandwidth.movavg(5) + test3.bandwidth.movavg(5)
        self.assertTrue(bwsum > 0.8e6 and bwsum < 1.2e6,
                        "Expected the combined bandwidth of three tests to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (bwsum))

        time.sleep(2.5)
        
        self.assertTrue(test1.done,
                        "Expected test1 to have finished at time = 5.5s")
        self.assertFalse(test2.done,
                         "Expected test2 to be still running at time = 5.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 5.5s")

        time.sleep(2.5)
        
        bwsum = test2.bandwidth.movavg(5) + test3.bandwidth.movavg(5)
        self.assertTrue(bwsum > 0.8e6 and bwsum < 1.2e6,
                        "Expected the combined bandwidth of two tests to be ~1Mbit/s, instead it was %.2fMbit/s"%
                        (bwsum))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")
        self.assertFalse(test3.done,
                         "Expected test3 to be still running at time = 10.5s")

        time.sleep(2.5)

        self.assertTrue(test3.bandwidth.movavg(5) > 0.80e6 and test3.bandwidth.movavg(5) < 1.20e6,
                        "Expected test3 bandwidth to be ~1Mbit/s (with 1 test running concurrently)" + 
                        ", instead it was %.2fMbit/s"%
                        (test3.bandwidth.movavg(5)/1e6))

        time.sleep(2.5)

        self.assertTrue(test2.done,
                        "Expected test2 to have finished at time = 10.5s")

        self.assertTrue(test3.latency.duration() > test2.latency.duration() and 
                        test2.latency.duration() > test1.latency.duration(),
                        "Expected test3 duration (%.2fs) to be greater than test2 (%.2fs)"
                        " and still greater than test1 (%.2fs)"%
                        (test3.latency.duration(), test2.latency.duration(), test1.latency.duration()))
        
    def test_multiple_sinks(self):
        self.tc.set_rate_limit(1e6)

        test1 = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 3.0,
                                         sink_ip = "127.0.0.1", sink_port = 12345,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test2 = self.srcnode.create_test(bw = 5.0*10**6, pktsize = 1500, duration = 3.0,
                                         sink_ip = "127.0.0.1", sink_port = 12346,
                                         bw_type = LinktestGoal.BW_CONSTANT, 
                                         update_interval = 0.2)
        test1.start()
        test2.start()
        time.sleep(3.5)
        
        self.assertTrue(test1.done, "Expected test2 to have finished at time = 3.5s")
        self.assertTrue(test2.done, "Expected test2 to have finished at time = 3.5s")
        
        self.assertTrue(test2.overall_bandwidth > 4 * test1.overall_bandwidth,
                        "Expected test2 bw (%.2fMbit/s) to be at least 4x larger"
                        " than test1 bw (%.2fMbit/s)"%
                        (test2.overall_bandwidth/1e6, test1.overall_bandwidth/1e6))