Skip to content

Commit

Permalink
Merge pull request #742 from carlwgeorge/no_implicit_event_loop
Browse files Browse the repository at this point in the history
Avoid manual async loop management
  • Loading branch information
amoffat authored Dec 8, 2024
2 parents c767bc4 + 484399f commit b658ce2
Showing 1 changed file with 12 additions and 12 deletions.
24 changes: 12 additions & 12 deletions tests/sh_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -1707,7 +1707,6 @@ def test_async(self):
)

alternating = []
q = AQueue()

async def producer(q):
alternating.append(1)
Expand All @@ -1722,9 +1721,11 @@ async def consumer(q):
self.assertEqual(msg, "hello")
alternating.append(2)

loop = asyncio.get_event_loop()
fut = asyncio.gather(producer(q), consumer(q))
loop.run_until_complete(fut)
async def main():
q = AQueue()
await asyncio.gather(producer(q), consumer(q))

asyncio.run(main())
self.assertListEqual(alternating, [1, 2, 1, 2])

def test_async_exc(self):
Expand All @@ -1733,8 +1734,7 @@ def test_async_exc(self):
async def producer():
await python(py.name, _async=True)

loop = asyncio.get_event_loop()
self.assertRaises(sh.ErrorReturnCode_34, loop.run_until_complete, producer())
self.assertRaises(sh.ErrorReturnCode_34, asyncio.run, producer())

def test_async_iter(self):
py = create_tmp_test(
Expand All @@ -1743,7 +1743,6 @@ def test_async_iter(self):
print(i)
"""
)
q = AQueue()

# this list will prove that our coroutines are yielding to eachother as each
# line is produced
Expand All @@ -1763,9 +1762,11 @@ async def consumer(q):
return
alternating.append(2)

loop = asyncio.get_event_loop()
res = asyncio.gather(producer(q), consumer(q))
loop.run_until_complete(res)
async def main():
q = AQueue()
await asyncio.gather(producer(q), consumer(q))

asyncio.run(main())
self.assertListEqual(alternating, [1, 2, 1, 2, 1, 2, 1, 2, 1, 2])

def test_async_iter_exc(self):
Expand All @@ -1783,8 +1784,7 @@ async def producer():
async for line in python(py.name, _async=True):
lines.append(int(line.strip()))

loop = asyncio.get_event_loop()
self.assertRaises(sh.ErrorReturnCode_34, loop.run_until_complete, producer())
self.assertRaises(sh.ErrorReturnCode_34, asyncio.run, producer())

def test_handle_both_out_and_err(self):
py = create_tmp_test(
Expand Down

0 comments on commit b658ce2

Please sign in to comment.