From f3c5a27db0234fbb9c01d3132cb2bc2535ca9f25 Mon Sep 17 00:00:00 2001 From: Jussi Vatjus-Anttila Date: Fri, 25 Nov 2022 08:19:39 +0200 Subject: [PATCH] accept 403 (ForbiddenException) when trying to allocate (#23) --- stf_appium_client/StfClient.py | 5 ++++- test/test_StfClient.py | 7 +++++-- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/stf_appium_client/StfClient.py b/stf_appium_client/StfClient.py index 8500606..b85a0d5 100644 --- a/stf_appium_client/StfClient.py +++ b/stf_appium_client/StfClient.py @@ -4,6 +4,9 @@ import json from pydash import filter_, map_, wrap, find, uniq import atexit + +from stf_client.exceptions import ForbiddenException + from stf_appium_client.Logger import Logger from stf_appium_client.exceptions import DeviceNotFound, NotConnectedError from stf_client.api_client import ApiClient, Configuration @@ -197,7 +200,7 @@ def allocate_first(): def try_allocate(device): try: return self.allocate(device, timeout_seconds=timeout_seconds) - except AssertionError as error: + except (AssertionError, ForbiddenException) as error: self.logger.warning(f"{device.get('serial')}Allocation fails: {error}") return None diff --git a/test/test_StfClient.py b/test/test_StfClient.py index aa94ebc..4ee640d 100644 --- a/test/test_StfClient.py +++ b/test/test_StfClient.py @@ -2,6 +2,9 @@ import logging import types from unittest.mock import patch, MagicMock + +from stf_client.exceptions import ForbiddenException + from stf_appium_client.StfClient import StfClient from stf_appium_client.exceptions import * @@ -134,12 +137,12 @@ def test_find_and_allocate_success(self): def test_find_and_allocate_second_success(self, mock_choise): invalid = {'serial': '12', 'present': False, 'ready': True, 'using': False, 'owner': None, 'status': 3} available = {'serial': '123', 'present': True, 'ready': True, 'using': False, 'owner': None, 'status': 3} - self.client.get_devices = MagicMock(return_value=[invalid, available]) + self.client.get_devices = MagicMock(return_value=[invalid, available, available]) def dummy_alloc(dev, timeout_seconds): return dev - self.client.allocate = MagicMock(side_effect=dummy_alloc) + self.client.allocate = MagicMock(side_effect=[ForbiddenException, dummy_alloc]) device = self.client.find_and_allocate({}) available['owner'] = 'me'