Skip to content
This repository was archived by the owner on Oct 31, 2023. It is now read-only.
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 6594a8f

Browse files
committedJun 14, 2019
Move deferToThread import outside of 'execute' function
Use a wrapper function instead of lambda + bool cast
1 parent e74fb34 commit 6594a8f

File tree

3 files changed

+17
-9
lines changed

3 files changed

+17
-9
lines changed
 

‎golem/core/deferred.py

+5-3
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
from twisted.internet import defer
55
from twisted.internet.task import deferLater
6+
from twisted.internet.threads import deferToThread
67
from twisted.python.failure import Failure
78

89

@@ -11,9 +12,10 @@ def __init__(self, *fns) -> None:
1112
self._seq = list(fns)
1213

1314
def execute(self) -> defer.Deferred:
14-
from twisted.internet.threads import deferToThread
15-
# The executed function cannot return a Deferred, hence the bool cast
16-
return deferToThread(lambda *_: bool(self._execute()))
15+
# The executed function cannot return a Deferred object
16+
def wrapper():
17+
self._execute()
18+
return deferToThread(wrapper)
1719

1820
@defer.inlineCallbacks
1921
def _execute(self) -> Any:

‎tests/golem/core/test_deferred.py

+11-5
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from golem.core.deferred import chain_function, DeferredSeq
88

99

10-
@mock.patch('twisted.internet.threads.deferToThread', lambda x: succeed(x()))
10+
@mock.patch('golem.core.deferred.deferToThread', lambda x: succeed(x()))
1111
@mock.patch('twisted.internet.reactor', mock.Mock(), create=True)
1212
class TestDeferredSeq(unittest.TestCase):
1313

@@ -21,13 +21,19 @@ def test_init_with_functions(self):
2121
]
2222
assert DeferredSeq(*fns)._seq == fns
2323

24-
def test_execute_empty(self):
25-
assert DeferredSeq().execute().result
24+
@mock.patch('golem.core.deferred.DeferredSeq._execute')
25+
def test_execute_empty(self, execute):
26+
deferred_seq = DeferredSeq()
27+
with mock.patch('golem.core.deferred.DeferredSeq._execute',
28+
wraps=deferred_seq._execute):
29+
deferred_seq.execute()
30+
assert execute.called
2631

2732
def test_execute_functions(self):
2833
fn_1, fn_2 = mock.Mock(), mock.Mock()
2934
fns = [fn_1, fn_2]
30-
assert DeferredSeq(*fns).execute().result
35+
36+
DeferredSeq(*fns).execute()
3137
assert fn_1.called
3238
assert fn_2.called
3339

@@ -43,7 +49,7 @@ def def2t(f, *args, **kwargs) -> Deferred:
4349
except Exception as exc: # pylint: disable=broad-except
4450
return fail(exc)
4551

46-
with mock.patch('twisted.internet.threads.deferToThread', def2t):
52+
with mock.patch('golem.core.deferred.deferToThread', def2t):
4753
DeferredSeq(fn_1, fn_2, fn_3, fn_4).execute()
4854

4955
assert fn_1.called

‎tests/golem/task/test_rpc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ def test_create_task(self, *_):
124124
def execute(f, *args, **kwargs):
125125
return defer.succeed(f(*args, **kwargs))
126126

127-
with mock.patch('twisted.internet.threads.deferToThread', execute):
127+
with mock.patch('golem.core.deferred.deferToThread', execute):
128128
result = self.provider.create_task(t.to_dict())
129129
rpc.enqueue_new_task.assert_called()
130130
self.assertEqual(result, ('task_id', None))

0 commit comments

Comments
 (0)
This repository has been archived.