Skip to content

Commit

Permalink
Revert "Lock for API calls (#26)"
Browse files Browse the repository at this point in the history
This reverts commit 5fca4ba.
  • Loading branch information
jupe authored Dec 16, 2024
1 parent 9ac6d16 commit a2a7780
Show file tree
Hide file tree
Showing 4 changed files with 5 additions and 49 deletions.
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,7 @@
"stf-client==0.1.0",
"pydash",
"easyprocess",
"requests",
"pid"
"requests"
],
entry_points={
'console_scripts': [
Expand Down
7 changes: 2 additions & 5 deletions stf_appium_client/StfClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

from stf_appium_client.Logger import Logger
from stf_appium_client.exceptions import DeviceNotFound, NotConnectedError
from stf_appium_client.tools import lock
from stf_client.api_client import ApiClient, Configuration
from stf_client.api.user_api import UserApi
from stf_client.api.devices_api import DevicesApi
Expand Down Expand Up @@ -81,8 +80,7 @@ def allocate(self, device: dict, timeout_seconds: int = DEFAULT_ALLOCATION_TIMEO
timeout = timeout_seconds * 1000

api_instance = UserApi(self._client)
with lock():
api_response = api_instance.add_user_device_v2(serial, timeout=timeout)
api_response = api_instance.add_user_device_v2(serial, timeout=timeout)
assert api_response.success, 'allocation fails'
self.logger.info(f'{serial}: Allocated (timeout: {timeout_seconds})')
device['owner'] = "me"
Expand Down Expand Up @@ -144,8 +142,7 @@ def release(self, device: dict) -> None:
self.logger.debug(f'{serial}: releasing..')

api_instance = UserApi(self._client)
with lock():
api_response = api_instance.delete_user_device_by_serial(serial)
api_response = api_instance.delete_user_device_by_serial(serial)
assert api_response.success, 'release fails'
device['owner'] = None
self.logger.info(f'{serial}: released')
Expand Down
18 changes: 1 addition & 17 deletions stf_appium_client/tools.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import socket
import json
import shutil
import tempfile
from contextlib import closing, contextmanager
from pid import PidFile, PidFileError
from contextlib import closing


def find_free_port() -> int:
Expand Down Expand Up @@ -55,17 +53,3 @@ def split(dest, subkey):
dest[subkey] = value
split(requirements, key)
return requirements

@contextmanager
def lock():
"""
Master lock
"""
try:
lockfile = PidFile(pidname='stf.pid', piddir=tempfile.gettempdir(), register_term_signal_handler=False)
lockfile.create()
yield lockfile
except PidFileError:
raise AssertionError('Lock in use')
finally:
lockfile.close()
26 changes: 1 addition & 25 deletions test/test_tools.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import json
from unittest.mock import patch, MagicMock
import pytest
from stf_appium_client.tools import find_free_port, parse_requirements, lock
from pid import PidFileAlreadyLockedError
from stf_appium_client.tools import find_free_port, parse_requirements


class TestTools:
Expand Down Expand Up @@ -30,25 +28,3 @@ def test_parse_requirements(self):
self.assertEqual(parse_requirements("key="), {})
with pytest.raises(ValueError):
self.assertEqual(parse_requirements("="), {})

@patch('stf_appium_client.tools.PidFile')
def test_lock(self, mock_pidfile):
with lock():
self.assertEqual(mock_pidfile.return_value.create.call_count, 1)
self.assertEqual(mock_pidfile.return_value.close.call_count, 0)
self.assertEqual(mock_pidfile.return_value.close.call_count, 1)

@patch('stf_appium_client.tools.PidFile')
def test_lock_is_released(self, mock_pidfile):
try:
with lock():
raise RuntimeError()
except RuntimeError:
self.assertEqual(mock_pidfile.return_value.close.call_count, 1)

@patch('stf_appium_client.tools.PidFile')
def test_lock_in_use(self, mock_pidfile):
mock_pidfile.return_value.create.side_effect = [PidFileAlreadyLockedError()]
with pytest.raises(AssertionError):
with lock():
pass

0 comments on commit a2a7780

Please sign in to comment.