Пример #1
0
    def test_basic(self):
        """Basic test of Speed Estimator functionality."""

        # make sure that we test all of the interval handling logic
        interval = progress.SpeedEstimator(0).INTERVAL
        time_to_test = interval * 2.5
        hunkspersec = 30
        hunktime = 1.0 / hunkspersec
        hunk = 1024
        goalbytes = time_to_test * hunkspersec * hunk

        #
        # Test that estimator won't give out estimates when constructed
        #
        sp = progress.SpeedEstimator(goalbytes)
        self.assertTrue(sp.get_speed_estimate() == None)
        self.assertTrue(sp.elapsed() == None)
        self.assertTrue(sp.get_final_speed() == None)

        timestamp = 1000.0

        #
        # Test again after starting, but before adding data
        #
        sp.start(timestamp)
        self.assertTrue(sp.get_speed_estimate() == None)
        self.assertTrue(sp.elapsed() == None)
        self.assertTrue(sp.get_final_speed() == None)

        #
        # We record transactions of one hunk each until there
        # are no more transactions left, and we claim that each
        # transaction took 0.01 second.  Therefore, the final speed
        # should be 100 * hunksize/second.
        #
        while goalbytes > 0:
            est = sp.get_speed_estimate()
            self.assertTrue(est is None or est > 0)
            sp.newdata(hunk, timestamp)
            goalbytes -= hunk
            timestamp += hunktime

        self.assertTrue(sp.get_final_speed() is None)
        sp.done(timestamp)
        self.debug("-- final speed: {0:f}".format(sp.get_final_speed()))
        self.debug("-- expected final speed: {0:f}".format(hunk * hunkspersec))
        self.debug(str(sp))
        self.assertTrue(int(sp.get_final_speed()) == hunk * hunkspersec)
Пример #2
0
    def test_stall(self):
        """Test that the ProgressTracker correctly diagnoses a
                "stall" in the download process."""
        hunk = 1024
        timestamp = 1000.0
        goalbytes = 10 * hunk * hunk

        #
        # Play records at the estimator until it starts giving out
        # estimates.
        #
        sp = progress.SpeedEstimator(goalbytes)
        sp.start(timestamp)
        while sp.get_speed_estimate() == None:
            sp.newdata(hunk, timestamp)
            timestamp += 0.01

        #
        # Now jump the timestamp forward by a lot-- 1000 seconds-- much
        # longer than the interval inside the estimator.  We should see
        # it stop giving us estimates.
        #
        timestamp = 2000.0
        sp.newdata(hunk, timestamp)
        self.assertTrue(sp.get_speed_estimate() == None)
Пример #3
0
    def test_format_speed(self):
        """Test that format_speed works as expected."""
        hunk = 1024
        goalbytes = 10 * hunk * hunk
        sp = progress.SpeedEstimator(goalbytes)

        testdata = {
            0: "0B/s",
            999: "999B/s",
            1000: "1000B/s",
            1024: "1.0k/s",
            10 * 1024: "10.0k/s",
            999 * 1024: "999k/s",
            1001 * 1024: "1001k/s",
            1024 * 1024: "1.0M/s"
        }

        for (val, expected) in testdata.items():
            str = sp.format_speed(val)
            self.assertTrue(len(str) <= 7)
            self.assertTrue(str == expected)