def test_zero_timeout(self): succession = Succession() for i in [1, 2, 3, 4, 5]: succession.push(i) self.assertEqual(list(succession.iter(timeout=0)), [1, 2, 3, 4, 5])
def test_succession(self): succession = Succession() for i in [1, 2, 3, 4, 5]: succession.push(i) succession.close() self.assertEqual(list(succession), [1, 2, 3, 4, 5])
def test_release_iter(self): succession = Succession(compress=lambda hd: []) root = weakref.ref(succession._root) iterator = weakref.ref(iter(succession)) for i in [1, 2, 3, 4, 5]: succession.push(i) gc.collect() self.assertIsNone(root()) self.assertIsNone(iterator())
def test_nonzero_timeout(self): succession = Succession() for i in [1, 2, 3, 4, 5]: succession.push(i) result = [] try: for item in succession.iter(timeout=0.01): result.append(item) except TimeoutError: self.assertEqual(result, [1, 2, 3, 4, 5]) else: # pragma: no cover self.fail()
def test_drop(self): succession = Succession() for i in [1, 2, 3, 4, 5]: succession.push(i) succession.drop() for i in [6, 7, 8, 9, 10]: succession.push(i) succession.close() self.assertEqual(list(succession), [6, 7, 8, 9, 10])
def test_head(self): succession = Succession() self.assertEqual(list(succession.head()), []) for i in [1, 2, 3, 4, 5]: succession.push(i) self.assertEqual(list(succession.head()), [1, 2, 3, 4, 5]) succession.close()
def test_compress_iter_saved(self): succession = Succession(compress=lambda hd: (i for i in hd)) succession.push(0) succession.push(1) succession.push(2) succession.push(3) first = succession.head() second = succession.head() self.assertEqual(list(first), [0, 1, 2, 3]) self.assertEqual(list(second), [0, 1, 2, 3])
def test_compress_after_push(self): succession = Succession(compress=lambda items: [sum(items)]) from_start = succession.iter() for i in [1, 2, 3, 4, 5]: succession.push(i) succession.close() from_end = succession.iter() self.assertEqual(list(from_start), [1, 2, 3, 4, 5]) self.assertEqual(list(from_end), [15])
def test_compress(self): def accumulate(start, new): return [sum(start) + new] succession = Succession(compress=accumulate) from_start = succession.iter() for i in [1, 2, 3, 4, 5]: succession.push(i) succession.close() from_end = succession.iter() self.assertEqual(list(from_start), [1, 2, 3, 4, 5]) self.assertEqual(list(from_end), [15])
def test_echo(self): req = Succession() res = Succession() def echo(): for m in req: res.push(m) res.close() t = threading.Thread(target=echo) t.start() res_iter = iter(res) req.push(1) self.assertEqual(next(res_iter), 1) req.push(2) self.assertEqual(next(res_iter), 2) req.push(3) self.assertEqual(next(res_iter), 3) req.close() t.join()
def test_drop_after_push(self): succession = Succession(compress=lambda hd: []) from_start = succession.iter() for i in [1, 2, 3]: succession.push(i) from_middle = succession.iter() for i in [4, 5, 6]: succession.push(i) succession.close() from_end = succession.iter() self.assertEqual(list(from_start), [1, 2, 3, 4, 5, 6]) self.assertEqual(list(from_middle), [4, 5, 6]) self.assertEqual(list(from_end), [])