Skip to content
This repository has been archived by the owner on Oct 31, 2023. It is now read-only.

Commit

Permalink
Node.quit RPC call (#2589)
Browse files Browse the repository at this point in the history
  • Loading branch information
mfranciszkiewicz authored Apr 7, 2018
1 parent 1dda28b commit 64df20c
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 6 deletions.
13 changes: 13 additions & 0 deletions golem/node.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,19 @@ def on_rpc_ready() -> Deferred:
except Exception as exc: # pylint: disable=broad-except
logger.exception("Application error: %r", exc)

def quit(self) -> None:

def _quit():
reactor = self._reactor
if self.client:
self.client.quit()
if reactor.running:
reactor.callFromThread(reactor.stop)

# Call in a separate thread and return early
from threading import Thread
Thread(target=_quit).start()

def set_password(self, password: str) -> bool:
logger.info("Got password")

Expand Down
5 changes: 3 additions & 2 deletions golem/rpc/mapping/rpcmethodnames.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,8 @@
withdraw= 'pay.withdraw',
get_withdraw_gas_cost= 'pay.withdraw.gas_cost',

quit= 'ui.quit',
resume= 'ui.start',
pause= 'ui.stop'
pause= 'ui.stop',
)

NODE_METHOD_MAP = dict(
Expand All @@ -100,4 +99,6 @@
are_terms_accepted= 'golem.terms',
accept_terms= 'golem.terms.accept',
show_terms= 'golem.terms.show',

quit= 'ui.quit',
)
60 changes: 56 additions & 4 deletions tests/golem/test_opt_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -485,6 +485,23 @@ def set_keys_auth(obj):
obj._keys_auth = Mock()


def call_now(fn, *args, **kwargs):
fn(*args, **kwargs)


class MockThread:

def __init__(self, target=None) -> None:
self._target = target

def start(self):
self._target()

@property
def target(self):
return self._target


@patch('golem.node.Node._start_keys_auth', set_keys_auth)
@patch('golem.node.Node._start_docker')
@patch('golem.node.async_run', mock_async_run)
Expand All @@ -501,10 +518,11 @@ def setUp(self):
self.node = None

def tearDown(self):
if self.node.client:
self.node.client.quit()
if self.node._db:
self.node._db.close()
if self.node:
if self.node.client:
self.node.client.quit()
if self.node._db:
self.node._db.close()
super().tearDown()

def test_start_rpc_router(self, reactor, *_):
Expand Down Expand Up @@ -674,3 +692,37 @@ def test_error(self, reactor, *_):
error_result = error('error message')
assert reactor.callFromThread.called
assert error_result is None

@patch('golem.node.Database')
@patch('threading.Thread', MockThread)
@patch('twisted.internet.reactor', create=True)
def test_quit_mock(self, reactor, *_):
reactor.running = False
reactor.callFromThread = call_now

node = Node.__new__(Node)

setattr(node, '_reactor', reactor)
setattr(node, 'client', None)

node.quit()

assert not node._reactor.stop.called

@patch('golem.node.Database')
@patch('threading.Thread', MockThread)
@patch('twisted.internet.reactor', create=True)
def test_quit(self, reactor, *_):
reactor.running = True

self.node = Node(datadir=self.path,
app_config=Mock(),
config_desc=ClientConfigDescriptor(),
use_docker_manager=False)

self.node.client = Mock()
self.node._reactor.callFromThread = call_now

self.node.quit()
assert self.node.client.quit.called
assert self.node._reactor.stop.called

0 comments on commit 64df20c

Please sign in to comment.