Beispiel #1
0
    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])
Beispiel #2
0
    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])
Beispiel #3
0
    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])
Beispiel #4
0
    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())
Beispiel #5
0
    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])
Beispiel #6
0
    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])
Beispiel #7
0
    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])
Beispiel #8
0
    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()
Beispiel #9
0
    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])
Beispiel #10
0
    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), [])
Beispiel #11
0
    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()
Beispiel #12
0
    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()