Skip to content

Commit

Permalink
tests: get (almost) everything passing again by removing explicit "lo…
Browse files Browse the repository at this point in the history
…op" kwarg

In several parts of asyncio, explicit "loop" kwarg was deprecated in 3.8
and removed in 3.10. These interfaces have automatically used the
correct loop since 3.7, so by dropping 3.6 support, no problem remains.
  • Loading branch information
alanbriolat committed Feb 14, 2022
1 parent ac845dc commit 745781a
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 31 deletions.
7 changes: 2 additions & 5 deletions src/csbot/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def __init__(self, handle_event, loop=None):
self.loop = loop

self.pending = set()
self.pending_event = asyncio.Event(loop=self.loop)
self.pending_event = asyncio.Event()
self.pending_event.clear()
self.future = None

Expand Down Expand Up @@ -134,7 +134,6 @@ def _run(self):
break
# Run until 1 or more tasks complete (or more tasks are added)
done, not_done = yield from asyncio.wait(not_done,
loop=self.loop,
return_when=asyncio.FIRST_COMPLETED)
# Handle exceptions raised by tasks
for f in done:
Expand Down Expand Up @@ -181,7 +180,7 @@ def __init__(self, get_handlers, loop=None):
self.loop = loop

self.events = deque()
self.new_events = asyncio.Event(loop=self.loop)
self.new_events = asyncio.Event()
self.futures = set()
self.future = None

Expand Down Expand Up @@ -242,7 +241,6 @@ def _run_handler(self, handler, event):
future = maybe_future(
result,
log=LOG,
loop=self.loop,
)
if future:
future = asyncio.ensure_future(self._finish_async_handler(future, event), loop=self.loop)
Expand Down Expand Up @@ -278,7 +276,6 @@ async def _run(self):
new_events = self.loop.create_task(self.new_events.wait())
LOG.debug('waiting on %s futures', len(self.futures))
done, pending = await asyncio.wait(self.futures | {new_events},
loop=self.loop,
return_when=asyncio.FIRST_COMPLETED)
# Remove done futures from the set of futures being waited on
done_futures = done - {new_events}
Expand Down
4 changes: 2 additions & 2 deletions src/csbot/irc.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,9 +267,9 @@ def __init__(self, *, loop=None, **kwargs):

self.reader, self.writer = None, None
self._exiting = False
self.connected = asyncio.Event(loop=self.loop)
self.connected = asyncio.Event()
self.connected.clear()
self.disconnected = asyncio.Event(loop=self.loop)
self.disconnected = asyncio.Event()
self.disconnected.set()
self._last_message_received = self.loop.time()
self._client_ping = None
Expand Down
4 changes: 2 additions & 2 deletions tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ def close(self):
self._reader.feed_eof()


def paused(f, *, loop=None):
def paused(f):
"""Wrap a coroutine function so it waits until explicitly enabled.
"""
event = asyncio.Event(loop=loop)
event = asyncio.Event()
resume = event.set

async def _f(*args, **kwargs):
Expand Down
2 changes: 1 addition & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def fast_forward(event_loop):
async def f(n):
# The wait_for() prevents forward(n) from blocking if there isn't enough async work to do
try:
await asyncio.wait_for(forward(n), n, loop=event_loop)
await asyncio.wait_for(forward(n), n)
except asyncio.TimeoutError:
pass
yield f
Expand Down
30 changes: 15 additions & 15 deletions tests/test_events.py
Original file line number Diff line number Diff line change
Expand Up @@ -287,7 +287,7 @@ async def test_event_chain_asynchronous(self, event_loop, event_runner):
Any events that occur during an event handler should be processed before the initial
`post_event()` future has a result.
"""
events = [asyncio.Event(loop=event_loop) for _ in range(2)]
events = [asyncio.Event() for _ in range(2)]
complete = []

@event_runner.add_handler('a')
Expand Down Expand Up @@ -333,7 +333,7 @@ async def e(_):
# - should have a post_event('a') call
# - a1 should complete, a2 is blocked on events[0]
future = event_runner.runner.post_event('a')
await asyncio.wait({future}, loop=event_loop, timeout=0.1)
await asyncio.wait({future}, timeout=0.1)
assert not future.done()
assert event_runner.get_handlers.mock_calls == [
mock.call('a'),
Expand All @@ -349,7 +349,7 @@ async def e(_):
# - post_event('f') should be called (by c)
# - d should complete
events[0].set()
await asyncio.wait({future}, loop=event_loop, timeout=0.1)
await asyncio.wait({future}, timeout=0.1)
assert not future.done()
assert event_runner.get_handlers.mock_calls == [
mock.call('a'),
Expand All @@ -366,7 +366,7 @@ async def e(_):
# - e should complete
# - future should complete, because no events or tasks remain pending
events[1].set()
await asyncio.wait({future}, loop=event_loop, timeout=0.1)
await asyncio.wait({future}, timeout=0.1)
assert future.done()
assert event_runner.get_handlers.mock_calls == [
mock.call('a'),
Expand All @@ -385,7 +385,7 @@ async def test_event_chain_hybrid(self, event_loop, event_runner):
event all run before synchronous handlers for the next event, but asynchronous handers can
run out-of-order.
"""
events = [asyncio.Event(loop=event_loop) for _ in range(2)]
events = [asyncio.Event() for _ in range(2)]
complete = []

@event_runner.add_handler('a')
Expand Down Expand Up @@ -429,7 +429,7 @@ def d2(_):
# - post_event('a') should be called (initial)
# - a1 should complete, a2 is blocked on events[0]
future = event_runner.runner.post_event('a')
await asyncio.wait({future}, loop=event_loop, timeout=0.1)
await asyncio.wait({future}, timeout=0.1)
assert not future.done()
assert event_runner.get_handlers.mock_calls == [
mock.call('a'),
Expand All @@ -444,7 +444,7 @@ def d2(_):
# - d2 should complete (synchronous phase)
# - d1 should complete (asynchronous phase)
events[0].set()
await asyncio.wait({future}, loop=event_loop, timeout=0.1)
await asyncio.wait({future}, timeout=0.1)
assert not future.done()
assert event_runner.get_handlers.mock_calls == [
mock.call('a'),
Expand All @@ -460,7 +460,7 @@ def d2(_):
# - c2 should complete (asynchronous phase)
# - future should complete, because no events or tasks remain pending
events[1].set()
await asyncio.wait({future}, loop=event_loop, timeout=0.1)
await asyncio.wait({future}, timeout=0.1)
assert future.done()
assert event_runner.get_handlers.mock_calls == [
mock.call('a'),
Expand All @@ -472,7 +472,7 @@ def d2(_):

async def test_overlapping_root_events(self, event_loop, event_runner):
"""Check that overlapping events get the same future."""
events = [asyncio.Event(loop=event_loop) for _ in range(1)]
events = [asyncio.Event() for _ in range(1)]
complete = []

@event_runner.add_handler('a')
Expand All @@ -487,7 +487,7 @@ async def b(_):
# Post the first event and allow tasks to run:
# - a is blocked on events[0]
f1 = event_runner.runner.post_event('a')
await asyncio.wait({f1}, loop=event_loop, timeout=0.1)
await asyncio.wait({f1}, timeout=0.1)
assert not f1.done()
assert complete == []

Expand All @@ -496,7 +496,7 @@ async def b(_):
# - a is still blocked on events[0]
# - f1 and f2 are not done, because they're for the same run loop, and a is still blocked
f2 = event_runner.runner.post_event('b')
await asyncio.wait({f2}, loop=event_loop, timeout=0.1)
await asyncio.wait({f2}, timeout=0.1)
assert not f2.done()
assert not f1.done()
assert complete == ['b']
Expand All @@ -505,7 +505,7 @@ async def b(_):
# - a completes
# - f1 and f2 are both done, because the run loop has finished
events[0].set()
await asyncio.wait([f1, f2], loop=event_loop, timeout=0.1)
await asyncio.wait([f1, f2], timeout=0.1)
assert f1.done()
assert f2.done()
assert complete == ['b', 'a']
Expand All @@ -526,14 +526,14 @@ async def b(_):
complete.append('b')

f1 = event_runner.runner.post_event('a')
await asyncio.wait({f1}, loop=event_loop, timeout=0.1)
await asyncio.wait({f1}, timeout=0.1)
assert f1.done()
assert complete == ['a']

f2 = event_runner.runner.post_event('b')
assert not f2.done()
assert f2 is not f1
await asyncio.wait({f2}, loop=event_loop, timeout=0.1)
await asyncio.wait({f2}, timeout=0.1)
assert f2.done()
assert complete == ['a', 'b']

Expand Down Expand Up @@ -571,7 +571,7 @@ async def b2(_):

assert event_runner.exception_handler.call_count == 0
future = event_runner.runner.post_event('a')
await asyncio.wait({future}, loop=event_loop, timeout=0.1)
await asyncio.wait({future}, timeout=0.1)
assert future.done()
assert future.exception() is None
assert event_runner.runner.get_handlers.mock_calls == [
Expand Down
12 changes: 6 additions & 6 deletions tests/test_plugin_linkinfo.py
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ def privmsg(self, event):
async def test_non_blocking_privmsg(self, event_loop, bot_helper, aioresponses):
bot_helper.reset_mock()

event = asyncio.Event(loop=event_loop)
event = asyncio.Event()

async def handler(url, **kwargs):
await event.wait()
Expand All @@ -245,7 +245,7 @@ async def handler(url, **kwargs):
':nick!user@host PRIVMSG #channel :http://example.com/',
':nick!user@host PRIVMSG #channel :b',
])
await asyncio.wait(futures, loop=event_loop, timeout=0.1)
await asyncio.wait(futures, timeout=0.1)
assert bot_helper['mockplugin'].handler_mock.mock_calls == [
mock.call('a'),
mock.call('http://example.com/'),
Expand All @@ -254,7 +254,7 @@ async def handler(url, **kwargs):
bot_helper.client.send_line.assert_not_called()

event.set()
await asyncio.wait(futures, loop=event_loop, timeout=0.1)
await asyncio.wait(futures, timeout=0.1)
assert all(f.done() for f in futures)
bot_helper.client.send_line.assert_has_calls([
mock.call('NOTICE #channel :foo'),
Expand All @@ -264,7 +264,7 @@ async def handler(url, **kwargs):
async def test_non_blocking_command(self, event_loop, bot_helper, aioresponses):
bot_helper.reset_mock()

event = asyncio.Event(loop=event_loop)
event = asyncio.Event()

async def handler(url, **kwargs):
await event.wait()
Expand All @@ -278,7 +278,7 @@ async def handler(url, **kwargs):
':nick!user@host PRIVMSG #channel :!link http://example.com/',
':nick!user@host PRIVMSG #channel :b',
])
await asyncio.wait(futures, loop=event_loop, timeout=0.1)
await asyncio.wait(futures, timeout=0.1)
assert bot_helper['mockplugin'].handler_mock.mock_calls == [
mock.call('a'),
mock.call('!link http://example.com/'),
Expand All @@ -287,7 +287,7 @@ async def handler(url, **kwargs):
bot_helper.client.send_line.assert_not_called()

event.set()
await asyncio.wait(futures, loop=event_loop, timeout=0.1)
await asyncio.wait(futures, timeout=0.1)
assert all(f.done() for f in futures)
bot_helper.client.send_line.assert_has_calls([
mock.call('NOTICE #channel :Error: Content-Type not HTML-ish: '
Expand Down

0 comments on commit 745781a

Please sign in to comment.