@@ -0,0 +1,2552 @@
+import contextlib%0Aimport mock%0Aimport testify as T%0A%0Afrom uwsgi_metrics.reservoir import Reservoir%0A%0Aclass ReservoirTest(T.TestCase):%0A%0A    def assert_all_values_between(self, reservoir, low, high):%0A        for value in reservoir.snapshot().values:%0A            T.assert_gte(value, low)%0A            T.assert_lt(value, high)%0A%0A    def test_insert_1000_elements_into_a_reservoir_of_100(self):%0A        reservoir = Reservoir(size=100, alpha=0.99)%0A        for i in xrange(1000):%0A            reservoir.update(i)%0A%0A        T.assert_equal(reservoir.snapshot().size(), 100)%0A        self.assert_all_values_between(reservoir, 0, 1000)%0A%0A    def test_insert_10_elements_into_a_reservoir_of_100(self):%0A        reservoir = Reservoir(size=100, alpha=0.99)%0A        for i in xrange(10):%0A            reservoir.update(i)%0A%0A        T.assert_equal(reservoir.snapshot().size(), 10)%0A        self.assert_all_values_between(reservoir, 0, 10)%0A%0A    def test_insert_100_elemnts_into_a_heavily_baised_reservoir_of_1000(self):%0A        reservoir = Reservoir(size=1000, alpha=0.01)%0A        for i in xrange(100):%0A            reservoir.update(i)%0A%0A        T.assert_equal(reservoir.snapshot().size(), 100)%0A        self.assert_all_values_between(reservoir, 0, 100)%0A%0A    def test_long_periods_of_inactivity_should_not_corrupt_sampling_state(self):%0A%0A        class Clock(object):%0A            def __init__(self, time):%0A                self._time = time%0A%0A            def __call__(self):%0A                return self._time%0A%0A            def add_millis(self, millis):%0A                self._time += 0.001 * millis%0A%0A            def add_hours(self, hours):%0A                self._time += 60 * 60 * hours%0A%0A        with mock.patch('time.time', Clock(42.0)) as clock:%0A            reservoir = Reservoir(size=10, alpha=0.015)%0A%0A            # Add 1000 values at a rate of 10 values/second%0A            for i in xrange(1000):%0A                reservoir.update(1000 + i)%0A                clock.add_millis(100)%0A%0A            T.assert_equal(reservoir.snapshot().size(), 10)%0A            self.assert_all_values_between(reservoir, 1000, 2000)%0A%0A            # Wait for 15 hours and add another value.  This should trigger a%0A            # rescale. Note that the number of samples will be reduced to 2%0A            # because of the very small scaling factor that will make all%0A            # existing priorities equal to zero after rescale.%0A            clock.add_hours(15);%0A            reservoir.update(2000);%0A            T.assert_equal(reservoir.snapshot().size(), 2)%0A            self.assert_all_values_between(reservoir, 1000, 3000)%0A

Add tests for the reservoir.