Skip to content

Commit

Permalink
Further develop API to more suitable for robot-framework (#7)
Browse files Browse the repository at this point in the history
* allow Appium object to instantiate
* split wait and allocate api from allocate_context API
  • Loading branch information
jupe authored Mar 22, 2021
1 parent 0325f3b commit 2d11cfc
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 14 deletions.
50 changes: 37 additions & 13 deletions stf_appium_client/StfClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,8 @@ def allocate(self, device: dict, timeout_seconds: int = DEFAULT_ALLOCATION_TIMEO
:param device: dictionary device object
:param timeout_seconds: Means the device will be automatically
removed from the user control if it is kept
idle for this period (in milliseconds);
default value is provided by the provider 'group timeout'
idle for this period. default value is provided
by the provider 'group timeout'
:return: None
"""
NotConnectedError.invariant(self._client, 'Not connected')
Expand Down Expand Up @@ -195,26 +195,50 @@ def try_allocate(device):
device['owner'] = "me"
return device

@contextmanager
def allocation_context(self, requirements: dict,
allocation_timeout=60,
timeout_seconds=DEFAULT_ALLOCATION_TIMEOUT_SECONDS):
def find_wait_and_allocate(self,
requirements: dict,
wait_timeout=60,
timeout_seconds=DEFAULT_ALLOCATION_TIMEOUT_SECONDS,
shuffle: bool = True):
"""
:param requirements:
:param allocation_timeout: how long time we try to allocate suitable device
:param timeout_seconds: allocation timeout
:return:
wait until suitable device is free and allocate it
:param requirements: dict of requirements for DUT
:param wait_timeout: wait timeout for suitable free device
:param timeout_seconds: allocation timeout. See more from allocate -API.
:param shuffle: allocate suitable device randomly.
:return: device dictionary
"""
self.logger.info(f"Trying to allocate device using requirements: {requirements}")
for i in range(allocation_timeout): # try to allocate for 1 minute..
device = None
for i in range(wait_timeout): # try to allocate for 1 minute..
try:
device = self.find_and_allocate(requirements=requirements,
timeout_seconds=timeout_seconds)
timeout_seconds=timeout_seconds,
shuffle=shuffle)
break
except DeviceNotFound:
# Wait a while
time.sleep(1)
pass
DeviceNotFound.invariant(device, 'Suitable device not found')
return device

@contextmanager
def allocation_context(self, requirements: dict,
wait_timeout=60,
timeout_seconds: int = DEFAULT_ALLOCATION_TIMEOUT_SECONDS,
shuffle: bool = True):
"""
:param requirements:
:param wait_timeout: how long time we try to allocate suitable device
:param timeout_seconds: allocation timeout
:param shuffle: allocate suitable device randomly
:return:
"""
self.logger.info(f"Trying to allocate device using requirements: {requirements}")
device = self.find_wait_and_allocate(requirements=requirements,
wait_timeout=wait_timeout,
timeout_seconds=timeout_seconds,
shuffle=shuffle)

@atexit.register
def exit():
Expand Down
11 changes: 10 additions & 1 deletion test/test_StfClient.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from pydash import get
from stf_appium_client.StfClient import StfClient
from stf_appium_client.exceptions import *

import types

@dataclass
class Response:
Expand All @@ -28,6 +28,15 @@ def test_construct(self):
client.release({})
with self.assertRaises(NotConnectedError):
client.find_and_allocate({})
# Check that all API's exists
self.assertIsInstance(client.allocate, types.MethodType)
self.assertIsInstance(client.release, types.MethodType)
self.assertIsInstance(client.remote_connect, types.MethodType)
self.assertIsInstance(client.remote_disconnect, types.MethodType)
self.assertIsInstance(client.get_devices, types.MethodType)
self.assertIsInstance(client.find_wait_and_allocate, types.MethodType)
self.assertIsInstance(client.find_and_allocate, types.MethodType)
self.assertIsInstance(client.allocation_context, types.MethodType)

@patch('stf_appium_client.StfClient.swagger_uri', new_callable=PropertyMock)
def test(self, mock_swagger_uri):
Expand Down

0 comments on commit 2d11cfc

Please sign in to comment.