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 threading
|
||||||
|
import datetime
|
||||||
import json
|
import json
|
||||||
import pydle
|
import pydle
|
||||||
|
|
||||||
|
@ -49,8 +50,17 @@ class MockClient(pydle.client.BasicClient):
|
||||||
""" A client that subtitutes its own connection for a mock connection to MockServer. """
|
""" A client that subtitutes its own connection for a mock connection to MockServer. """
|
||||||
|
|
||||||
def __init__(self, *args, mock_server=None, **kwargs):
|
def __init__(self, *args, mock_server=None, **kwargs):
|
||||||
super().__init__(*args, **kwargs)
|
|
||||||
self._mock_server = mock_server
|
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):
|
def _connect(self, *args, **kwargs):
|
||||||
self.connection = MockConnection(mock_client=self, mock_server=self._mock_server, eventloop=self.eventloop)
|
self.connection = MockConnection(mock_client=self, mock_server=self._mock_server, eventloop=self.eventloop)
|
||||||
|
@ -135,6 +145,9 @@ class MockEventLoop:
|
||||||
f(*args, **kwargs)
|
f(*args, **kwargs)
|
||||||
|
|
||||||
def schedule_in(self, _delay, _f, *_args, **_kw):
|
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 = threading.Timer(_delay, _f, _args, _kw)
|
||||||
timer.start()
|
timer.start()
|
||||||
|
|
||||||
|
@ -144,8 +157,10 @@ class MockEventLoop:
|
||||||
return id
|
return id
|
||||||
|
|
||||||
def schedule_periodically(self, _delay, _f, *_args, **_kw):
|
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 = threading.Timer(_delay, self._do_schedule_periodically, (_f, _delay, id, _args, _kw))
|
||||||
timer.start()
|
timer.start()
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
import time
|
import time
|
||||||
|
import datetime
|
||||||
import pydle
|
import pydle
|
||||||
|
|
||||||
import pytest
|
import pytest
|
||||||
from .fixtures import with_client
|
from .fixtures import with_client
|
||||||
from .mocks import Mock, MockEventLoop
|
from .mocks import Mock, MockEventLoop, MockConnection
|
||||||
|
|
||||||
|
|
||||||
class Passed:
|
class Passed:
|
||||||
|
@ -20,6 +21,8 @@ class Passed:
|
||||||
self._passed = False
|
self._passed = False
|
||||||
|
|
||||||
|
|
||||||
|
## Client.
|
||||||
|
|
||||||
@with_client(connected=False)
|
@with_client(connected=False)
|
||||||
def test_mock_client_connect(server, client):
|
def test_mock_client_connect(server, client):
|
||||||
assert not client.connected
|
assert not client.connected
|
||||||
|
@ -48,6 +51,22 @@ def test_mock_client_receive(server, client):
|
||||||
assert message.params == ('test',)
|
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():
|
def test_mock_eventloop_schedule():
|
||||||
ev = MockEventLoop()
|
ev = MockEventLoop()
|
||||||
passed = Passed()
|
passed = Passed()
|
||||||
|
@ -68,6 +87,15 @@ def test_mock_eventloop_schedule_in():
|
||||||
|
|
||||||
ev.stop()
|
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
|
@pytest.mark.slow
|
||||||
def test_mock_eventloop_schedule_periodically():
|
def test_mock_eventloop_schedule_periodically():
|
||||||
ev = MockEventLoop()
|
ev = MockEventLoop()
|
||||||
|
|
Loading…
Reference in New Issue