예제 #1
0
    def testSamplesWraparound(self):
        NUM = sampling.HOST_STATS_AVERAGING_WINDOW + 1

        samples = sampling.SampleWindow(
            sampling.HOST_STATS_AVERAGING_WINDOW)

        class FakeHostSample(object):

            counter = 0

            def __repr__(self):
                return "FakeHostSample(id=%i)" % self.id

            def __init__(self, *args):
                self.id = FakeHostSample.counter
                FakeHostSample.counter += 1

        with MonkeyPatchScope([(sampling, 'HostSample', FakeHostSample)]):
            hs = sampling.HostMonitor(samples=samples)
            for _ in range(NUM):
                hs()

            first, last, _ = samples.stats()
            self.assertEqual(first.id,
                             FakeHostSample.counter -
                             sampling.HOST_STATS_AVERAGING_WINDOW)
            self.assertEqual(last.id,
                             FakeHostSample.counter - 1)
예제 #2
0
 def test_last_error(self):
     win = sampling.SampleWindow(size=2)
     win.append(self._VALUES[0])
     win.append(self._VALUES[1])
     self.assertEqual(None, win.last(nth=3))
예제 #3
0
 def test_second_last(self):
     win = sampling.SampleWindow(size=2)
     win.append(self._VALUES[0])
     win.append(self._VALUES[1])
     self.assertEqual(self._VALUES[0], win.last(nth=2))
예제 #4
0
 def setUp(self):
     self._counter = itertools.count(0)
     self.win = sampling.SampleWindow(
         size=2, timefn=lambda: next(self._counter))
예제 #5
0
 def test_last_error(self):
     win = sampling.SampleWindow(size=2)
     win.append(self._VALUES[0])
     win.append(self._VALUES[1])
     _, collected = win.last(nth=3)
     assert collected is None
예제 #6
0
 def test_second_last(self):
     win = sampling.SampleWindow(size=2)
     win.append(self._VALUES[0])
     win.append(self._VALUES[1])
     _, collected = win.last(nth=2)
     assert self._VALUES[0] == collected
예제 #7
0
 def test_window_size_bad_values(self, size):
     with pytest.raises(ValueError):
         sampling.SampleWindow(size)
예제 #8
0
 def test_last(self):
     win = sampling.SampleWindow(size=2)
     win.append(self._VALUES[0])
     win.append(self._VALUES[1])
     _, collected = win.last()
     self.assertEqual(self._VALUES[1], collected)