Skip to content

Commit

Permalink
add hack to make asyncio behave similar in python 3.5
Browse files Browse the repository at this point in the history
  • Loading branch information
jabdoa2 committed Jul 10, 2016
1 parent 67f8691 commit 072ee26
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 13 deletions.
6 changes: 5 additions & 1 deletion mpf/tests/MpfTestCase.py
Expand Up @@ -142,11 +142,15 @@ def getOptions(self):

def advance_time_and_run(self, delta=1.0):
self.machine.log.info("Advancing time by %s", delta)
start = self.loop.time()
task_with_timeout = asyncio.wait_for(asyncio.sleep(delay=delta, loop=self.loop),
timeout=delta+1,
timeout=delta+0.00000001,
loop=self.loop)
self.machine.clock.loop.run_until_complete(task_with_timeout)

# hack to python3.5
self.loop.set_time(start + delta)

def machine_run(self):
#self.machine.events.process_event_queue()
self.advance_time_and_run(0)
Expand Down
17 changes: 5 additions & 12 deletions mpf/tests/loop.py
@@ -1,5 +1,5 @@
import selectors
from asyncio import base_events, futures, coroutine, events
from asyncio import base_events, futures, coroutine, events, test_utils
import collections
import heapq

Expand Down Expand Up @@ -128,6 +128,10 @@ def __init__(self):
def time(self):
return self._time

def set_time(self, time):
"""Set time in loop."""
self._time = time

def advance_time(self, advance):
"""Move test time forward."""
if advance:
Expand Down Expand Up @@ -240,23 +244,12 @@ def create_connection(self, protocol_factory, host=None, port=None, *,
# return

def _run_once(self):
# check if a mocked reader is ready
for fd, reader in self.readers.items():
if hasattr(fd, "ready") and fd.ready() and not isinstance(fd, MagicMock):
self.call_soon(reader[0], *reader[1])

# check if a mocked writer is ready
for fd, writer in self.writers.items():
if hasattr(fd, "ready") and fd.ready() and not isinstance(fd, MagicMock):
self.call_soon(writer[0], *writer[1])

super()._run_once()

# Advance time only when we finished everything at the present:
if len(self._ready) == 0:
if not self._timers.is_empty():
self._time = self._timers.pop_closest()
# print('time:',self._time,'timers:',self._timers._timers_set)

def call_at(self, when, callback, *args):
self._timers.add(when)
Expand Down

0 comments on commit 072ee26

Please sign in to comment.