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 def to_connlog(self): pass def connlog_diff(self, *args): pass 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)
def testSamplesWraparound(self): NUM = sampling.HOST_STATS_AVERAGING_WINDOW + 1 samples = sampling.SampleWindow(sampling.HOST_STATS_AVERAGING_WINDOW) class FakeEvent(object): def __init__(self, *args): self.counter = 0 def isSet(self): return self.counter >= NUM def set(self): pass def wait(self, unused): self.counter += 1 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 def to_connlog(self): pass def connlog_diff(self, *args): pass with MonkeyPatchScope([(sampling, 'HostSample', FakeHostSample)]): self._hs = sampling.HostStatsThread(samples) self._hs._sampleInterval = 0 # we cannot monkey patch, it will interfer on threading internals self._hs._stopEvent = FakeEvent() self._hs.start() self._hs.wait() first, last, _ = samples.stats() self.assertEqual( first.id, FakeHostSample.counter - sampling.HOST_STATS_AVERAGING_WINDOW) self.assertEqual(last.id, FakeHostSample.counter - 1)
def test_last(self): win = sampling.SampleWindow(size=2) win.append(self._VALUES[0]) win.append(self._VALUES[1]) self.assertEqual(self._VALUES[1], win.last())
def setUp(self): self._counter = itertools.count(0) self.win = sampling.SampleWindow(size=2, timefn=lambda: next(self._counter))
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))