[Tests] Fix failing SessionProxy tests
For some reason, the time.sleep calls in the tests in test_sessionproxy did not sleep for the expected amount of time causing the results to differ from the expected. Fixed by replacing time.time function with twisted's task.clock.seconds and advancing the clock manually. Also minor changes to test_client.py
This commit is contained in:
parent
cae8a18437
commit
9b18fb2b71
|
@ -94,7 +94,7 @@ class ClientTestCase(BaseTestCase):
|
|||
self.addCleanup(client.disconnect)
|
||||
return result
|
||||
|
||||
d.addCallback(on_connect)
|
||||
d.addCallbacks(on_connect, self.fail)
|
||||
return d
|
||||
|
||||
def test_connect_localclient(self):
|
||||
|
@ -108,7 +108,7 @@ class ClientTestCase(BaseTestCase):
|
|||
self.addCleanup(client.disconnect)
|
||||
return result
|
||||
|
||||
d.addCallback(on_connect)
|
||||
d.addCallbacks(on_connect, self.fail)
|
||||
return d
|
||||
|
||||
def test_connect_bad_password(self):
|
||||
|
@ -124,7 +124,7 @@ class ClientTestCase(BaseTestCase):
|
|||
)
|
||||
self.addCleanup(client.disconnect)
|
||||
|
||||
d.addErrback(on_failure)
|
||||
d.addCallbacks(self.fail, on_failure)
|
||||
return d
|
||||
|
||||
def test_connect_without_password(self):
|
||||
|
@ -141,7 +141,7 @@ class ClientTestCase(BaseTestCase):
|
|||
self.assertEqual(failure.value.username, username)
|
||||
self.addCleanup(client.disconnect)
|
||||
|
||||
d.addErrback(on_failure)
|
||||
d.addCallbacks(self.fail, on_failure)
|
||||
return d
|
||||
|
||||
@defer.inlineCallbacks
|
||||
|
@ -152,12 +152,9 @@ class ClientTestCase(BaseTestCase):
|
|||
d = client.core.invalid_method()
|
||||
|
||||
def on_failure(failure):
|
||||
self.assertEqual(
|
||||
failure.trap(error.WrappedException),
|
||||
error.WrappedException
|
||||
)
|
||||
self.assertEqual(failure.trap(error.WrappedException), error.WrappedException)
|
||||
self.addCleanup(client.disconnect)
|
||||
d.addErrback(on_failure)
|
||||
d.addCallbacks(self.fail, on_failure)
|
||||
yield d
|
||||
|
||||
def test_connect_without_sending_client_version_fails(self):
|
||||
|
@ -174,5 +171,5 @@ class ClientTestCase(BaseTestCase):
|
|||
)
|
||||
self.addCleanup(no_version_sending_client.disconnect)
|
||||
|
||||
d.addErrback(on_failure)
|
||||
d.addCallbacks(self.fail, on_failure)
|
||||
return d
|
||||
|
|
|
@ -1,6 +1,14 @@
|
|||
import time
|
||||
# -*- coding: utf-8 -*-
|
||||
#
|
||||
# Copyright (C) 2016 bendikro <bro.devel+deluge@gmail.com>
|
||||
#
|
||||
# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with
|
||||
# the additional special exception to link portions of this program with the OpenSSL library.
|
||||
# See LICENSE for more details.
|
||||
#
|
||||
|
||||
from twisted.internet.defer import maybeDeferred, succeed
|
||||
from twisted.internet.task import Clock
|
||||
|
||||
import deluge.component as component
|
||||
import deluge.ui.sessionproxy
|
||||
|
@ -9,6 +17,7 @@ from .basetest import BaseTestCase
|
|||
|
||||
|
||||
class Core(object):
|
||||
|
||||
def __init__(self):
|
||||
self.reset()
|
||||
|
||||
|
@ -87,19 +96,22 @@ class Client(object):
|
|||
|
||||
client = Client()
|
||||
|
||||
deluge.ui.sessionproxy.client = client
|
||||
|
||||
|
||||
class SessionProxyTestCase(BaseTestCase):
|
||||
|
||||
def set_up(self):
|
||||
self.clock = Clock()
|
||||
self.patch(deluge.ui.sessionproxy, "time", self.clock.seconds)
|
||||
self.patch(deluge.ui.sessionproxy, "client", client)
|
||||
self.sp = deluge.ui.sessionproxy.SessionProxy()
|
||||
client.core.reset()
|
||||
d = self.sp.start()
|
||||
|
||||
def do_get_torrents_status(torrent_ids):
|
||||
inital_keys = ['key1']
|
||||
self.sp.get_torrents_status({'id': torrent_ids}, inital_keys)
|
||||
# Advance clock to expire the cache times
|
||||
self.clock.advance(2)
|
||||
return self.sp.get_torrents_status({'id': torrent_ids}, inital_keys)
|
||||
d.addCallback(do_get_torrents_status)
|
||||
return d
|
||||
|
||||
|
@ -122,13 +134,13 @@ class SessionProxyTestCase(BaseTestCase):
|
|||
|
||||
def test_get_torrent_status_change_without_cache(self):
|
||||
client.core.torrents["a"]["key1"] = 2
|
||||
time.sleep(self.sp.cache_time + 0.1)
|
||||
self.clock.advance(self.sp.cache_time + 0.1)
|
||||
d = self.sp.get_torrent_status("a", [])
|
||||
d.addCallback(self.assertEquals, client.core.torrents["a"])
|
||||
return d
|
||||
|
||||
def test_get_torrent_status_key_not_updated(self):
|
||||
time.sleep(self.sp.cache_time + 0.1)
|
||||
self.clock.advance(self.sp.cache_time + 0.1)
|
||||
self.sp.get_torrent_status("a", ["key1"])
|
||||
client.core.torrents["a"]["key2"] = 99
|
||||
d = self.sp.get_torrent_status("a", ["key2"])
|
||||
|
@ -136,7 +148,7 @@ class SessionProxyTestCase(BaseTestCase):
|
|||
return d
|
||||
|
||||
def test_get_torrents_status_key_not_updated(self):
|
||||
time.sleep(self.sp.cache_time + 0.1)
|
||||
self.clock.advance(self.sp.cache_time + 0.1)
|
||||
self.sp.get_torrents_status({"id": ["a"]}, ["key1"])
|
||||
client.core.torrents["a"]["key2"] = 99
|
||||
d = self.sp.get_torrents_status({"id": ["a"]}, ["key2"])
|
||||
|
|
|
@ -158,11 +158,6 @@ class MenuBar(component.Component):
|
|||
"menuitem_addtorrent"
|
||||
]
|
||||
|
||||
client.register_event_handler("TorrentStateChangedEvent", self.on_torrentstatechanged_event)
|
||||
client.register_event_handler("TorrentResumedEvent", self.on_torrentresumed_event)
|
||||
client.register_event_handler("SessionPausedEvent", self.on_sessionpaused_event)
|
||||
client.register_event_handler("SessionResumedEvent", self.on_sessionresumed_event)
|
||||
|
||||
def start(self):
|
||||
for widget in self.change_sensitivity:
|
||||
self.main_builder.get_object(widget).set_sensitive(True)
|
||||
|
@ -193,9 +188,19 @@ class MenuBar(component.Component):
|
|||
client.core.get_known_accounts().addCallback(
|
||||
self._on_known_accounts).addErrback(self._on_known_accounts_fail)
|
||||
|
||||
client.register_event_handler("TorrentStateChangedEvent", self.on_torrentstatechanged_event)
|
||||
client.register_event_handler("TorrentResumedEvent", self.on_torrentresumed_event)
|
||||
client.register_event_handler("SessionPausedEvent", self.on_sessionpaused_event)
|
||||
client.register_event_handler("SessionResumedEvent", self.on_sessionresumed_event)
|
||||
|
||||
def stop(self):
|
||||
log.debug("MenuBar stopping")
|
||||
|
||||
client.deregister_event_handler("TorrentStateChangedEvent", self.on_torrentstatechanged_event)
|
||||
client.deregister_event_handler("TorrentResumedEvent", self.on_torrentresumed_event)
|
||||
client.deregister_event_handler("SessionPausedEvent", self.on_sessionpaused_event)
|
||||
client.deregister_event_handler("SessionResumedEvent", self.on_sessionresumed_event)
|
||||
|
||||
for widget in self.change_sensitivity:
|
||||
self.main_builder.get_object(widget).set_sensitive(False)
|
||||
|
||||
|
|
|
@ -6,10 +6,8 @@
|
|||
# the additional special exception to link portions of this program with the OpenSSL library.
|
||||
# See LICENSE for more details.
|
||||
#
|
||||
|
||||
|
||||
import logging
|
||||
import time
|
||||
from time import time
|
||||
|
||||
from twisted.internet.defer import maybeDeferred, succeed
|
||||
|
||||
|
@ -51,7 +49,7 @@ class SessionProxy(component.Component):
|
|||
for torrent_id in torrent_ids:
|
||||
# Let's at least store the torrent ids with empty statuses
|
||||
# so that upcoming queries or status updates don't throw errors.
|
||||
self.torrents.setdefault(torrent_id, [time.time(), {}])
|
||||
self.torrents.setdefault(torrent_id, [time(), {}])
|
||||
self.cache_times.setdefault(torrent_id, {})
|
||||
return torrent_ids
|
||||
return client.core.get_session_state().addCallback(on_get_session_state)
|
||||
|
@ -128,9 +126,8 @@ class SessionProxy(component.Component):
|
|||
keys = self.torrents[torrent_id][1].keys()
|
||||
|
||||
for key in keys:
|
||||
if time.time() - self.cache_times[torrent_id].get(key, 0.0) > self.cache_time:
|
||||
if time() - self.cache_times[torrent_id].get(key, 0.0) > self.cache_time:
|
||||
keys_to_get.append(key)
|
||||
|
||||
if not keys_to_get:
|
||||
return succeed(
|
||||
self.create_status_dict([torrent_id], keys)[torrent_id]
|
||||
|
@ -139,7 +136,7 @@ class SessionProxy(component.Component):
|
|||
d = client.core.get_torrent_status(torrent_id, keys_to_get, True)
|
||||
|
||||
def on_status(result, torrent_id):
|
||||
t = time.time()
|
||||
t = time()
|
||||
self.torrents[torrent_id][0] = t
|
||||
self.torrents[torrent_id][1].update(result)
|
||||
for key in keys_to_get:
|
||||
|
@ -151,7 +148,7 @@ class SessionProxy(component.Component):
|
|||
|
||||
def on_status(result):
|
||||
if result:
|
||||
t = time.time()
|
||||
t = time()
|
||||
self.torrents[torrent_id] = (t, result)
|
||||
self.cache_times[torrent_id] = {}
|
||||
for key in result:
|
||||
|
@ -180,7 +177,7 @@ class SessionProxy(component.Component):
|
|||
# Helper functions and callbacks ---------------------------------------
|
||||
def on_status(result, torrent_ids, keys):
|
||||
# Update the internal torrent status dict with the update values
|
||||
t = time.time()
|
||||
t = time()
|
||||
for key, value in result.iteritems():
|
||||
try:
|
||||
self.torrents[key][0] = t
|
||||
|
@ -199,7 +196,7 @@ class SessionProxy(component.Component):
|
|||
|
||||
def find_torrents_to_fetch(torrent_ids):
|
||||
to_fetch = []
|
||||
t = time.time()
|
||||
t = time()
|
||||
for torrent_id in torrent_ids:
|
||||
torrent = self.torrents[torrent_id]
|
||||
if t - torrent[0] > self.cache_time:
|
||||
|
@ -243,15 +240,15 @@ class SessionProxy(component.Component):
|
|||
def on_torrent_state_changed(self, torrent_id, state):
|
||||
if torrent_id in self.torrents:
|
||||
self.torrents[torrent_id][1].setdefault("state", state)
|
||||
self.cache_times.setdefault(torrent_id, {}).update(state=time.time())
|
||||
self.cache_times.setdefault(torrent_id, {}).update(state=time())
|
||||
|
||||
def on_torrent_added(self, torrent_id, from_state):
|
||||
self.torrents[torrent_id] = [time.time() - self.cache_time - 1, {}]
|
||||
self.torrents[torrent_id] = [time() - self.cache_time - 1, {}]
|
||||
self.cache_times[torrent_id] = {}
|
||||
|
||||
def on_status(status):
|
||||
self.torrents[torrent_id][1].update(status)
|
||||
t = time.time()
|
||||
t = time()
|
||||
for key in status:
|
||||
self.cache_times[torrent_id][key] = t
|
||||
client.core.get_torrent_status(torrent_id, []).addCallback(on_status)
|
||||
|
|
Loading…
Reference in New Issue