From cf37dd6b4ebe8bf514230f14f9db832f8e4f71be Mon Sep 17 00:00:00 2001 From: Shiz Date: Sat, 15 Mar 2014 00:24:20 +0100 Subject: [PATCH] tests: Add timedelta scheduling to MockEventLoop. --- tests/mocks.py | 19 +++++++++++++++++-- tests/test__mocks.py | 30 +++++++++++++++++++++++++++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/tests/mocks.py b/tests/mocks.py index 7c166f2..fb24866 100644 --- a/tests/mocks.py +++ b/tests/mocks.py @@ -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() diff --git a/tests/test__mocks.py b/tests/test__mocks.py index ddfcdd5..8ea069f 100644 --- a/tests/test__mocks.py +++ b/tests/test__mocks.py @@ -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()