mirror of https://github.com/Shizmob/pydle.git
tests: Add timedelta scheduling to MockEventLoop.
This commit is contained in:
parent
6809c1a540
commit
cf37dd6b4e
|
@ -1,4 +1,5 @@
|
|||
import threading
|
||||
import datetime
|
||||
import json
|
||||
import pydle
|
||||
|
||||
|
@ -49,8 +50,17 @@ class MockClient(pydle.client.BasicClient):
|
|||
""" A client that subtitutes its own connection for a mock connection to MockServer. """
|
||||
|
||||
def __init__(self, *args, mock_server=None, **kwargs):
|
||||
super().__init__(*args, **kwargs)
|
||||
self._mock_server = mock_server
|
||||
self._mock_logger = Mock()
|
||||
super().__init__(*args, **kwargs)
|
||||
|
||||
@property
|
||||
def logger(self):
|
||||
return self._mock_logger
|
||||
|
||||
@logger.setter
|
||||
def logger(self, val):
|
||||
pass
|
||||
|
||||
def _connect(self, *args, **kwargs):
|
||||
self.connection = MockConnection(mock_client=self, mock_server=self._mock_server, eventloop=self.eventloop)
|
||||
|
@ -135,6 +145,9 @@ class MockEventLoop:
|
|||
f(*args, **kwargs)
|
||||
|
||||
def schedule_in(self, _delay, _f, *_args, **_kw):
|
||||
if isinstance(_delay, datetime.timedelta):
|
||||
_delay = _delay.total_seconds()
|
||||
|
||||
timer = threading.Timer(_delay, _f, _args, _kw)
|
||||
timer.start()
|
||||
|
||||
|
@ -144,8 +157,10 @@ class MockEventLoop:
|
|||
return id
|
||||
|
||||
def schedule_periodically(self, _delay, _f, *_args, **_kw):
|
||||
id = self._mock_periodical_id
|
||||
if isinstance(_delay, datetime.timedelta):
|
||||
_delay = _delay.total_seconds()
|
||||
|
||||
id = self._mock_periodical_id
|
||||
timer = threading.Timer(_delay, self._do_schedule_periodically, (_f, _delay, id, _args, _kw))
|
||||
timer.start()
|
||||
|
||||
|
|
|
@ -1,9 +1,10 @@
|
|||
import time
|
||||
import datetime
|
||||
import pydle
|
||||
|
||||
import pytest
|
||||
from .fixtures import with_client
|
||||
from .mocks import Mock, MockEventLoop
|
||||
from .mocks import Mock, MockEventLoop, MockConnection
|
||||
|
||||
|
||||
class Passed:
|
||||
|
@ -20,6 +21,8 @@ class Passed:
|
|||
self._passed = False
|
||||
|
||||
|
||||
## Client.
|
||||
|
||||
@with_client(connected=False)
|
||||
def test_mock_client_connect(server, client):
|
||||
assert not client.connected
|
||||
|
@ -48,6 +51,22 @@ def test_mock_client_receive(server, client):
|
|||
assert message.params == ('test',)
|
||||
|
||||
|
||||
## Connection.
|
||||
|
||||
def test_mock_connection_connect():
|
||||
serv = Mock()
|
||||
conn = MockConnection(mock_server=serv)
|
||||
|
||||
conn.connect()
|
||||
assert conn.connected
|
||||
assert serv.connection is conn
|
||||
|
||||
conn.disconnect()
|
||||
assert not conn.connected
|
||||
|
||||
|
||||
## Event loop.
|
||||
|
||||
def test_mock_eventloop_schedule():
|
||||
ev = MockEventLoop()
|
||||
passed = Passed()
|
||||
|
@ -68,6 +87,15 @@ def test_mock_eventloop_schedule_in():
|
|||
|
||||
ev.stop()
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_mock_eventloop_schedule_in_timedelta():
|
||||
ev = MockEventLoop()
|
||||
passed = Passed()
|
||||
|
||||
ev.schedule_in(datetime.timedelta(seconds=1), lambda: passed.set())
|
||||
time.sleep(1.1)
|
||||
assert passed
|
||||
|
||||
@pytest.mark.slow
|
||||
def test_mock_eventloop_schedule_periodically():
|
||||
ev = MockEventLoop()
|
||||
|
|
Loading…
Reference in New Issue