Esempio n. 1
0
    def test_completion_queue(self):
        async def square(x):
            return x * x

        cq = futures.CompletionQueue()

        t1 = tasks.Task(None, square(5))
        t1.tick(None, None)
        self.assertTrue(t1.is_completed())

        t2 = tasks.Task(None, square(7))
        self.assertFalse(t2.is_completed())

        cq.put(t1)
        cq.put(t2)
        self.assertEqual(len(cq), 2)

        self.assertIs(cq.get(timeout=0), t1)

        with self.assertRaises(queues.Empty):
            cq.get(timeout=0)

        t2.tick(None, None)
        self.assertTrue(t2.is_completed())
        self.assertEqual(len(cq), 1)
        self.assertIs(cq.get(timeout=0), t2)

        self.assertEqual(len(cq), 0)
Esempio n. 2
0
 def test_close_not_graceful(self):
     f = futures.Future()
     cq = futures.CompletionQueue([f])
     self.assertEqual(cq.close(False), [f])
     with self.assertRaises(queues.Closed):
         cq.get()
     with self.assertRaises(queues.Closed):
         cq.put(f)
     for _ in cq.as_completed():
         self.fail()
Esempio n. 3
0
 def test_close_repeatedly(self):
     f = futures.Future()
     cq = futures.CompletionQueue([f])
     self.assertFalse(cq.is_closed())
     self.assertEqual(cq.close(True), [])
     self.assertTrue(cq.is_closed())
     self.assertEqual(cq.close(False), [f])
     self.assertTrue(cq.is_closed())
     self.assertEqual(cq.close(True), [])
     self.assertTrue(cq.is_closed())
     self.assertEqual(cq.close(False), [])
     self.assertTrue(cq.is_closed())
Esempio n. 4
0
def main(executor: LABELS.executor):
    start = time.perf_counter()
    queue = futures.CompletionQueue()
    for i in range(10):
        queue.put(executor.submit(square, i + 1))
    queue.close()
    total = 0
    for future in queue:
        answer, duration = future.get_result()
        total += answer
        print('spend %.3f seconds computing' % duration)
    elapsed = time.perf_counter() - start
    print('result: %d' % total)
    print('total elapsed time: %.3f seconds' % elapsed)
    return 0
Esempio n. 5
0
    def test_duplicated_futures(self):
        f = futures.Future()
        cq = futures.CompletionQueue()

        cq.put(f)
        cq.put(f)
        cq.put(f)
        self.assertEqual(len(cq), 3)

        f.set_result(42)
        self.assertIs(cq.get(), f)
        self.assertEqual(len(cq), 2)
        self.assertIs(cq.get(), f)
        self.assertEqual(len(cq), 1)
        self.assertIs(cq.get(), f)
        self.assertEqual(len(cq), 0)
Esempio n. 6
0
    def test_get(self):
        f = futures.Future()
        cq = futures.CompletionQueue([f])

        with self.assertRaises(queues.Empty):
            cq.get(timeout=0)

        cq.close()
        with self.assertRaises(queues.Empty):
            cq.get(timeout=0)

        f.set_result(42)
        self.assertIs(cq.get(timeout=0), f)

        with self.assertRaises(queues.Closed):
            cq.get(timeout=0)
Esempio n. 7
0
 def test_iter(self):
     fs = [futures.Future() for _ in range(3)]
     f = futures.Future()
     f.set_result(42)
     expect = set(fs)
     expect.add(f)
     cq = futures.CompletionQueue([f])
     actual = set()
     for f in cq:
         actual.add(f)
         if fs:
             f = fs.pop()
             f.set_result(42)
             cq.put(f)
         else:
             cq.close()
     self.assertEqual(actual, expect)
Esempio n. 8
0
 def test_as_completed(self):
     for timeout in (None, 0):
         with self.subTest(check=timeout):
             fs = [futures.Future() for _ in range(3)]
             f = futures.Future()
             f.set_result(42)
             expect = set(fs)
             expect.add(f)
             cq = futures.CompletionQueue([f])
             # Test putting more futures into the queue while
             # iterating over it.
             actual = set()
             for f in cq.as_completed(timeout):
                 actual.add(f)
                 if fs:
                     f = fs.pop()
                     f.set_result(42)
                     cq.put(f)
                 else:
                     cq.close()
             self.assertEqual(actual, expect)
Esempio n. 9
0
    def test_completion_queue(self):
        fs = [futures.Future(), futures.Future(), futures.Future()]
        fs[0].set_result(42)
        cq = futures.CompletionQueue(fs)

        self.assertTrue(cq)
        self.assertEqual(len(cq), 3)
        self.assertFalse(cq.is_closed())

        self.assertEqual(set(cq.as_completed(0)), set(fs[:1]))
        self.assertTrue(cq)
        self.assertEqual(len(cq), 2)

        cq.close()
        self.assertTrue(cq.is_closed())

        fs[1].set_result(42)
        fs[2].set_result(42)
        self.assertEqual(set(cq.as_completed()), set(fs[1:]))

        self.assertFalse(cq)
        self.assertEqual(len(cq), 0)
Esempio n. 10
0
 def test_as_completed_empty(self):
     cq = futures.CompletionQueue()
     cq.put(futures.Future())
     for _ in cq.as_completed(timeout=0):
         self.fail()
     self.assertEqual(len(cq), 1)
Esempio n. 11
0
 def test_put(self):
     f = futures.Future()
     cq = futures.CompletionQueue()
     cq.close()
     with self.assertRaises(queues.Closed):
         cq.put(f)