示例#1
0
def test_os_getpid_not_int(mocked_getpid, tmp):
    folder = tmp()
    folder.mkdir()

    mocked_getpid.return_value = "Boom !"

    lock = PidLockFile(folder, "qt")
    with pytest.raises(RuntimeError):
        lock.lock()
示例#2
0
def test_check_running_process_creation_time_too_high(mocked_create_time, tmp):
    folder = tmp()
    folder.mkdir()

    lock = PidLockFile(folder, "qt")
    lock.lock()

    # Test process creation time
    mocked_create_time.return_value = 999_999_999_999
    assert not lock.check_running()
示例#3
0
def test_already_locked_same_process(tmp):
    folder = tmp()
    folder.mkdir()
    lock_file = folder / "nxdrive_qt.pid"

    # Save the current pid
    lock_file.write_text(str(os.getpid()), encoding="utf-8")

    lock = PidLockFile(folder, "qt")
    pid = lock.lock()
    assert pid == os.getpid()
    assert not lock.locked
示例#4
0
def test_already_locked(tmp):
    folder = tmp()
    folder.mkdir()
    lock_file = folder / "nxdrive_qt.pid"

    # Inexistent pid
    lock_file.write_text("3857965", encoding="utf-8")

    lock = PidLockFile(folder, "qt")
    pid = lock.lock()
    assert pid is None
    assert lock.locked
示例#5
0
 def launch(self, options=None, console=False):
     """Launch the Qt app in the main thread and sync in another thread."""
     # TODO: use the start method as default once implemented
     from nxdrive.utils import PidLockFile
     lock = PidLockFile(self.manager.get_configuration_folder(), "qt")
     if lock.lock() is not None:
         self.log.warning("Qt application already running: exiting")
         return
     app = self._get_application(options, console=console)
     exit_code = app.exec_()
     lock.unlock()
     self.log.debug("Qt application exited with code %r", exit_code)
     return exit_code
示例#6
0
def test_bad_lock_file_content(tmp):
    folder = tmp()
    folder.mkdir()
    lock_file = folder / "nxdrive_qt.pid"

    # Craft a bad lock file
    lock_file.write_text("BOOM", encoding="utf-8")

    lock = PidLockFile(folder, "qt")
    pid = lock.lock()
    assert pid is None
    assert lock_file.is_file()
    assert lock.locked
示例#7
0
 def launch(self, options=None):
     """Launch the Qt app in the main thread and sync in another thread."""
     # TODO: use the start method as default once implemented
     from nxdrive.utils import PidLockFile
     lock = PidLockFile(self.controller.config_folder, "qt")
     if lock.lock() is not None:
         self.log.warning("Qt application already running: exiting")
         return
     from nxdrive.gui.application import Application
     app = Application(self.controller, options)
     exit_code = app.exec_()
     lock.unlock()
     self.log.debug("Qt application exited with code %r", exit_code)
示例#8
0
 def launch(self, options=None, console=False):
     """Launch the Qt app in the main thread and sync in another thread."""
     from nxdrive.utils import PidLockFile
     lock = PidLockFile(self.manager.get_configuration_folder(), "qt")
     if lock.lock() is not None:
         self.log.warning("Qt application already running: exiting")
         # Handle URL if needed
         self.manager.get_direct_edit().handle_url()
         return
     app = self._get_application(options, console=console)
     exit_code = app.exec_()
     lock.unlock()
     self.log.debug("Qt application exited with code %r", exit_code)
     return exit_code
示例#9
0
 def launch(self, options=None, console=False):
     """Launch the Qt app in the main thread and sync in another thread."""
     from nxdrive.utils import PidLockFile
     lock = PidLockFile(self.manager.nxdrive_home, 'qt')
     if lock.lock() is not None:
         self.log.warning('Qt application already running: exiting')
         # Handle URL if needed
         self.manager.direct_edit.handle_url()
         return
     app = self._get_application(console=console)
     exit_code = app.exec_()
     lock.unlock()
     self.log.debug('Qt application exited with code %r', exit_code)
     return exit_code
示例#10
0
 def launch(self, options=None, console=False):
     """Launch the Qt app in the main thread and sync in another thread."""
     from nxdrive.utils import PidLockFile
     lock = PidLockFile(self.manager.get_configuration_folder(), "qt")
     if lock.lock() is not None:
         self.log.warning("Qt application already running: exiting")
         # Handle URL if needed
         self.manager.get_drive_edit().handle_url()
         return
     app = self._get_application(options, console=console)
     exit_code = app.exec_()
     lock.unlock()
     self.log.debug("Qt application exited with code %r", exit_code)
     return exit_code
示例#11
0
def test_lock_file(tmp):
    folder = tmp()
    folder.mkdir()
    lock_file = folder / "nxdrive_qt.pid"

    assert not lock_file.is_file()

    lock = PidLockFile(folder, "qt")
    assert not lock.locked

    pid = lock.lock()
    assert pid is None
    assert lock.locked
    assert lock_file.is_file()

    lock.unlock()
    assert lock.locked
    assert not lock_file.is_file()
示例#12
0
def test_unlock(mocked_unlink, tmp):
    folder = tmp()
    folder.mkdir()
    lock_file = folder / "nxdrive_qt.pid"

    assert not lock_file.is_file()

    lock = PidLockFile(folder, "qt")

    # Not yet locked
    lock.unlock()

    # Now locked
    lock.lock()

    # Test file already removed
    mocked_unlink.side_effect = FileNotFoundError("Boom !")
    lock.unlock()

    # Test another OSerror
    mocked_unlink.side_effect = PermissionError("Boom !")
    lock.unlock()
示例#13
0
def test_check_running(mocked_unlink, tmp):
    folder = tmp()
    folder.mkdir()
    lock_file = folder / "nxdrive_qt.pid"

    lock = PidLockFile(folder, "qt")
    lock.lock()

    # Set false PID number
    lock_file.write_text("999999999")

    # Test another OSerror
    mocked_unlink.side_effect = PermissionError("Boom !")
    assert lock.check_running() == 999_999_999

    # Set PID data not int
    lock_file.write_text("999-999,999")
    assert lock.check_running() is None