def test_long_callback(self): self.d = FlushingDict(TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.long_callback) expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) # We add another element while the callback is idling, to see # if the new element is flushed later. We need to wait 2 # latencies for the callback, one more for the time-based # second flush, and we add some tolerance. gevent.sleep(0) self.d.add(TestFlushingDict.SIZE, TestFlushingDict.SIZE) gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS * 3 + 0.1) self.assertEqual(TestFlushingDict.SIZE + 1, sum(len(data) for data in self.received_data))
def test_long_callback(self): self.d = FlushingDict( TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.long_callback) expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) # We add another element while the callback is idling, to see # if the new element is flushed later. We need to wait 2 # latencies for the callback, one more for the time-based # second flush, and we add some tolerance. gevent.sleep(0) self.d.add(TestFlushingDict.SIZE, TestFlushingDict.SIZE) gevent.sleep((TestFlushingDict.FLUSH_LATENCY_SECONDS + 0.1) * 3) self.assertEqual(TestFlushingDict.SIZE + 1, sum(len(data) for data in self.received_data))
def setUp(self): super(TestFlushingDict, self).setUp() self.received_data = [] self.d = FlushingDict( TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.callback)
class TestFlushingDict(unittest.TestCase): SIZE = 3 FLUSH_LATENCY_SECONDS = 0.2 def setUp(self): super(TestFlushingDict, self).setUp() self.received_data = [] self.d = FlushingDict( TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.callback) def test_success_latency(self): self.d.add(0, 0) gevent.sleep(2 * TestFlushingDict.FLUSH_LATENCY_SECONDS) self.assertEqual(1, len(self.received_data)) six.assertCountEqual(self, [(0, 0)], self.received_data[0]) def test_success_size(self): expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) gevent.sleep(0) self.assertEqual(1, len(self.received_data)) six.assertCountEqual(self, expected_data, self.received_data[0]) def test_success_size_latency(self): expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) gevent.sleep(0) self.assertEqual(1, len(self.received_data)) six.assertCountEqual(self, expected_data, self.received_data[0]) self.d.add(TestFlushingDict.SIZE, TestFlushingDict.SIZE) gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS + 0.1) self.assertEqual(2, len(self.received_data)) six.assertCountEqual( self, [(TestFlushingDict.SIZE, TestFlushingDict.SIZE)], self.received_data[1]) def test_long_callback(self): self.d = FlushingDict( TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.long_callback) expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) # We add another element while the callback is idling, to see # if the new element is flushed later. We need to wait 2 # latencies for the callback, one more for the time-based # second flush, and we add some tolerance. gevent.sleep(0) self.d.add(TestFlushingDict.SIZE, TestFlushingDict.SIZE) gevent.sleep((TestFlushingDict.FLUSH_LATENCY_SECONDS + 0.1) * 3) self.assertEqual(TestFlushingDict.SIZE + 1, sum(len(data) for data in self.received_data)) def test_many_elements(self): expected_data = [] for i in range(20): self.d.add(i, i) expected_data.append((i, i)) gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS + 0.1) six.assertCountEqual(self, expected_data, sum(self.received_data, [])) def callback(self, data): self.received_data.append(data) def long_callback(self, data): gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS * 2) self.callback(data)
def setUp(self): super(TestFlushingDict, self).setUp() self.received_data = [] self.d = FlushingDict(TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.callback)
class TestFlushingDict(unittest.TestCase): SIZE = 3 FLUSH_LATENCY_SECONDS = 0.2 def setUp(self): super(TestFlushingDict, self).setUp() self.received_data = [] self.d = FlushingDict(TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.callback) def test_success_latency(self): self.d.add(0, 0) gevent.sleep(2 * TestFlushingDict.FLUSH_LATENCY_SECONDS) self.assertEqual(1, len(self.received_data)) self.assertItemsEqual([(0, 0)], self.received_data[0]) def test_success_size(self): expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) gevent.sleep(0) self.assertEqual(1, len(self.received_data)) self.assertItemsEqual(expected_data, self.received_data[0]) def test_success_size_latency(self): expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) gevent.sleep(0) self.assertEqual(1, len(self.received_data)) self.assertItemsEqual(expected_data, self.received_data[0]) self.d.add(TestFlushingDict.SIZE, TestFlushingDict.SIZE) gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS) self.assertEqual(2, len(self.received_data)) self.assertItemsEqual([(TestFlushingDict.SIZE, TestFlushingDict.SIZE)], self.received_data[1]) def test_long_callback(self): self.d = FlushingDict(TestFlushingDict.SIZE, TestFlushingDict.FLUSH_LATENCY_SECONDS, self.long_callback) expected_data = [] for i in range(TestFlushingDict.SIZE): self.d.add(i, i) expected_data.append((i, i)) # We add another element while the callback is idling, to see # if the new element is flushed later. We need to wait 2 # latencies for the callback, one more for the time-based # second flush, and we add some tolerance. gevent.sleep(0) self.d.add(TestFlushingDict.SIZE, TestFlushingDict.SIZE) gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS * 3 + 0.1) self.assertEqual(TestFlushingDict.SIZE + 1, sum(len(data) for data in self.received_data)) def test_many_elements(self): expected_data = [] for i in range(20): self.d.add(i, i) expected_data.append((i, i)) gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS) self.assertItemsEqual(expected_data, sum(self.received_data, [])) def callback(self, data): self.received_data.append(data) def long_callback(self, data): gevent.sleep(TestFlushingDict.FLUSH_LATENCY_SECONDS * 2) self.callback(data)