Permalink
Browse files

Merge pull request #151 from gridsync/149.tahoe-restart-fix

149.tahoe restart fix
  • Loading branch information...
crwood committed Feb 9, 2019
2 parents 32303ae + acab08e commit b629a1d2a613532b7c07507433fe773e666eac4e
Showing with 45 additions and 0 deletions.
  1. +22 −0 gridsync/tahoe.py
  2. +23 −0 tests/test_tahoe.py
@@ -80,6 +80,12 @@ def processExited(self, reason):


class Tahoe():

STOPPED = 0
STARTING = 1
STARTED = 2
STOPPING = 3

def __init__(self, nodedir=None, executable=None):
self.executable = executable
self.multi_folder_support = True
@@ -104,6 +110,7 @@ def __init__(self, nodedir=None, executable=None):
self.use_tor = False
self.monitor = Monitor(self)
self._monitor_started = False
self.state = Tahoe.STOPPED

def config_set(self, section, option, value):
self.config.set(section, option, value)
@@ -360,6 +367,8 @@ def kill(self):

@inlineCallbacks
def stop(self):
log.debug('Stopping "%s" tahoe client...', self.name)
self.state = Tahoe.STOPPING
if not os.path.isfile(self.pidfile):
log.error('No "twistd.pid" file found in %s', self.nodedir)
return
@@ -374,6 +383,8 @@ def stop(self):
os.remove(self.pidfile)
except EnvironmentError:
pass
self.state = Tahoe.STOPPED
log.debug('Finished stopping "%s" tahoe client', self.name)

@inlineCallbacks
def upgrade_legacy_config(self):
@@ -430,6 +441,8 @@ def upgrade_legacy_config(self):

@inlineCallbacks
def start(self):
log.debug('Starting "%s" tahoe client...', self.name)
self.state = Tahoe.STARTING
if not self._monitor_started:
self.monitor.start()
self._monitor_started = True
@@ -452,10 +465,19 @@ def start(self):
self.api_token = f.read().strip()
self.shares_happy = int(self.config_get('client', 'shares.happy'))
self.load_magic_folders()
self.state = Tahoe.STARTED
log.debug(
'Finished starting "%s" tahoe client (pid: %s)', self.name, pid)

@inlineCallbacks
def restart(self):
log.debug("Restarting %s client...", self.name)
if self.state in (Tahoe.STOPPING, Tahoe.STARTING):
log.warning(
'Aborting restart operation; '
'the "%s" client is already (re)starting',
self.name)
return
# Temporarily disable desktop notifications for (dis)connect events
pref = get_preference('notifications', 'connection')
set_preference('notifications', 'connection', 'false')
@@ -356,6 +356,29 @@ def return_args(_, args):
assert output == ['stop']


@pytest.mark.parametrize(
'tahoe_state,call_count',
[
(Tahoe.STOPPED, 1), # restart completed
(Tahoe.STARTING, 0), # restart aborted
(Tahoe.STARTED, 1), # restart completed
(Tahoe.STOPPING, 0), # restart aborted
]
)
@inlineCallbacks
def test_tahoe_restart(tahoe_state, call_count, tahoe, monkeypatch):
mocked_start = MagicMock()
monkeypatch.setattr('gridsync.tahoe.Tahoe.stop', MagicMock())
monkeypatch.setattr('gridsync.tahoe.Tahoe.start', mocked_start)
monkeypatch.setattr('gridsync.tahoe.Tahoe.await_ready', MagicMock())
monkeypatch.setattr('gridsync.tahoe.deferLater', MagicMock())
monkeypatch.setattr('gridsync.tahoe.set_preference', MagicMock())
monkeypatch.setattr('gridsync.tahoe.get_preference', MagicMock())
tahoe.state = tahoe_state
yield tahoe.restart()
assert mocked_start.call_count == call_count


@inlineCallbacks
def test_get_grid_status(tahoe, monkeypatch):
json_content = b'''{

0 comments on commit b629a1d

Please sign in to comment.