diff options
author | jhaydaman <33549221+jhaydaman@users.noreply.github.com> | 2018-05-30 02:15:06 -0500 |
---|---|---|
committer | Andrew Svetlov <andrew.svetlov@gmail.com> | 2018-05-30 10:15:06 +0300 |
commit | 0a28c0d12ee7201de039ced4d815f57f1f8fd48c (patch) | |
tree | 9fa72f88b1f3035ea9fe5a4bde10215a432abe30 /Lib/test/test_concurrent_futures.py | |
parent | bb9474f1fb2fc7c7ed9f826b78262d6a12b5f9e8 (diff) | |
download | cpython-git-0a28c0d12ee7201de039ced4d815f57f1f8fd48c.tar.gz |
bpo-33238: Add InvalidStateError to concurrent.futures. (GH-7056)
Future.set_result and Future.set_exception now raise InvalidStateError
if the futures are not pending or running. This mirrors the behavior
of asyncio.Future, and prevents AssertionErrors in asyncio.wrap_future
when set_result is called multiple times.
Diffstat (limited to 'Lib/test/test_concurrent_futures.py')
-rw-r--r-- | Lib/test/test_concurrent_futures.py | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/Lib/test/test_concurrent_futures.py b/Lib/test/test_concurrent_futures.py index b258a0eafd..f2c28ac12b 100644 --- a/Lib/test/test_concurrent_futures.py +++ b/Lib/test/test_concurrent_futures.py @@ -1206,6 +1206,34 @@ class FutureTests(BaseTestCase): self.assertTrue(isinstance(f1.exception(timeout=5), OSError)) t.join() + def test_multiple_set_result(self): + f = create_future(state=PENDING) + f.set_result(1) + + with self.assertRaisesRegex( + futures.InvalidStateError, + 'FINISHED: <Future at 0x[0-9a-f]+ ' + 'state=finished returned int>' + ): + f.set_result(2) + + self.assertTrue(f.done()) + self.assertEqual(f.result(), 1) + + def test_multiple_set_exception(self): + f = create_future(state=PENDING) + e = ValueError() + f.set_exception(e) + + with self.assertRaisesRegex( + futures.InvalidStateError, + 'FINISHED: <Future at 0x[0-9a-f]+ ' + 'state=finished raised ValueError>' + ): + f.set_exception(Exception()) + + self.assertEqual(f.exception(), e) + @test.support.reap_threads def test_main(): |