diff --git a/.travis.yml b/.travis.yml index 0cf673c..2ff243a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,8 +10,8 @@ install: - pip install -r test_requirements.txt before_install: - export DEVICE_HIVE_TRANSPORT_URLS='http://playground.dev.devicehive.com/api/rest,ws://playground.dev.devicehive.com/api/websocket' - - openssl aes-256-cbc -K $encrypted_1ea9ebbf5537_key -iv $encrypted_1ea9ebbf5537_iv -in admin_refresh_token.txt.enc -out admin_refresh_token.txt -d + - openssl aes-256-cbc -K $encrypted_94dc46d8330a_key -iv $encrypted_94dc46d8330a_iv -in refresh_tokens.tar.enc -out refresh_tokens.tar -d + - tar xvf refresh_tokens.tar - export DEVICE_HIVE_ADMIN_REFRESH_TOKEN=$(cat admin_refresh_token.txt) - - openssl aes-256-cbc -K $encrypted_94dc46d8330a_key -iv $encrypted_94dc46d8330a_iv -in user_refresh_token.txt.enc -out user_refresh_token.txt -d - export DEVICE_HIVE_USER_REFRESH_TOKEN=$(cat user_refresh_token.txt) script: pytest -xv tests --transport-urls=$DEVICE_HIVE_TRANSPORT_URLS --admin-refresh-token=$DEVICE_HIVE_ADMIN_REFRESH_TOKEN --user-refresh-token=$DEVICE_HIVE_USER_REFRESH_TOKEN diff --git a/admin_refresh_token.txt.enc b/admin_refresh_token.txt.enc deleted file mode 100644 index 2f7979a..0000000 Binary files a/admin_refresh_token.txt.enc and /dev/null differ diff --git a/devicehive/device_hive.py b/devicehive/device_hive.py index 862051d..d964639 100644 --- a/devicehive/device_hive.py +++ b/devicehive/device_hive.py @@ -45,7 +45,8 @@ def connect(self, transport_url, **options): self._transport_name = self.transport_name(transport_url) assert self._transport_name, 'Unexpected transport url scheme' transport_keep_alive = options.pop('transport_keep_alive', True) - transport_alive_timeout = options.pop('transport_alive_timeout', 0.01) + transport_alive_sleep_time = options.pop('transport_alive_sleep_time', + 1e-6) connect_timeout = options.pop('connect_timeout', 30) max_num_connect = options.pop('max_num_connect', 10) connect_interval = options.pop('connect_interval', 1) @@ -67,7 +68,7 @@ def connect(self, transport_url, **options): self._ensure_transport_disconnect() self._transport.connect(transport_url, **options) while self._transport.is_alive(): - time.sleep(transport_alive_timeout) + time.sleep(transport_alive_sleep_time) exception_info = self._transport.exception_info if exception_info and not isinstance(exception_info[1], self._transport.error): diff --git a/devicehive/device_hive_api.py b/devicehive/device_hive_api.py index 0aef261..72f5db8 100644 --- a/devicehive/device_hive_api.py +++ b/devicehive/device_hive_api.py @@ -37,8 +37,9 @@ class DeviceHiveApi(object): def __init__(self, transport_url, **options): self._transport_url = transport_url - self._transport_alive_timeout = options.pop('transport_alive_timeout', - 0.01) + transport_alive_sleep_time = options.pop('transport_alive_sleep_time', + 1e-6) + self._transport_alive_sleep_time = transport_alive_sleep_time options['transport_keep_alive'] = False options['api_init'] = False self._options = options @@ -71,7 +72,7 @@ def _call(self, call, *args, **kwargs): device_hive = DeviceHive(ApiCallHandler, call, *args, **kwargs) device_hive.connect(self._transport_url, **self._options) while not device_hive.handler.ready: - time.sleep(self._transport_alive_timeout) + time.sleep(self._transport_alive_sleep_time) if device_hive.transport.exception_info: six.reraise(*device_hive.transport.exception_info) return device_hive.handler.result diff --git a/devicehive/transports/http_transport.py b/devicehive/transports/http_transport.py index 39beb80..dd0fe25 100644 --- a/devicehive/transports/http_transport.py +++ b/devicehive/transports/http_transport.py @@ -17,7 +17,7 @@ def __init__(self, data_format_class, data_format_options, handler_class, handler_options) self._url = None self._options = None - self._events_queue_timeout = None + self._events_queue_sleep = None self._events_queue = [] self._subscription_ids = [] self._success_codes = [200, 201, 204] @@ -25,7 +25,8 @@ def __init__(self, data_format_class, data_format_options, handler_class, def _connect(self, url, **options): self._url = url self._options = options - self._events_queue_timeout = options.pop('events_queue_timeout', 0.01) + self._events_queue_sleep_time = options.pop('events_queue_sleep_time', + 1e-6) if not self._url.endswith('/'): self._url += '/' self._connected = True @@ -34,7 +35,7 @@ def _connect(self, url, **options): def _receive(self): while self._connected and not self._exception_info: if not self._events_queue: - time.sleep(self._events_queue_timeout) + time.sleep(self._events_queue_sleep_time) continue for event in self._events_queue.pop(0): self._handle_event(event) diff --git a/devicehive/transports/websocket_transport.py b/devicehive/transports/websocket_transport.py index c7bf561..d4a7e06 100644 --- a/devicehive/transports/websocket_transport.py +++ b/devicehive/transports/websocket_transport.py @@ -18,8 +18,8 @@ def __init__(self, data_format_class, data_format_options, handler_class, data_format_options, handler_class, handler_options) self._websocket = websocket.WebSocket() - self._event_queue_sleep = None - self._response_sleep = None + self._event_queue_sleep_time = None + self._response_sleep_time = None self._pong_received = False self._event_queue = [] self._responses = {} @@ -37,12 +37,12 @@ def _websocket_call(self, websocket_method, *args, **kwargs): def _connect(self, url, **options): timeout = options.pop('timeout', None) - event_queue_sleep = options.pop('event_queue_sleep', 0.01) - response_sleep = options.pop('response_sleep', 0.01) + event_queue_sleep_time = options.pop('event_queue_sleep_time', 1e-6) + response_sleep_time = options.pop('response_sleep_time', 1e-6) pong_timeout = options.pop('pong_timeout', None) self._websocket.timeout = timeout - self._event_queue_sleep = event_queue_sleep - self._response_sleep = response_sleep + self._event_queue_sleep_time = event_queue_sleep_time + self._response_sleep_time = response_sleep_time self._websocket_call(self._websocket.connect, url, **options) self._connected = True event_thread = threading.Thread(target=self._event) @@ -62,11 +62,16 @@ def _event(self): try: opcode, data = self._websocket_call(self._websocket.recv_data, True) - if opcode == websocket.ABNF.OPCODE_TEXT: - self._parse_event(self._decode(data.decode('utf-8'))) - continue - if opcode == websocket.ABNF.OPCODE_BINARY: - self._parse_event(self._decode(data)) + if opcode in (websocket.ABNF.OPCODE_TEXT, + websocket.ABNF.OPCODE_BINARY): + if opcode == websocket.ABNF.OPCODE_TEXT: + data = data.decode('utf-8') + event = self._decode(data) + request_id = event.get(self.REQUEST_ID_KEY) + if not request_id: + self._event_queue.append(event) + continue + self._responses[request_id] = event continue if opcode == websocket.ABNF.OPCODE_PONG: self._pong_received = True @@ -76,13 +81,6 @@ def _event(self): except: self._exception_info = sys.exc_info() - def _parse_event(self, event): - request_id = event.get(self.REQUEST_ID_KEY) - if not request_id: - self._event_queue.append(event) - return - self._responses[request_id] = event - def _ping(self, pong_timeout): while self._connected: try: @@ -99,7 +97,7 @@ def _ping(self, pong_timeout): def _receive(self): while self._connected and not self._exception_info: if not self._event_queue: - time.sleep(self._event_queue_sleep) + time.sleep(self._event_queue_sleep_time) continue for event in self._event_queue: self._handle_event(event) @@ -125,8 +123,9 @@ def _receive_response(self, request_id, timeout): while time.time() - timeout < start_time: response = self._responses.get(request_id) if response: + del self._responses[request_id] return response - time.sleep(self._response_sleep) + time.sleep(self._response_sleep_time) raise self._error('Response timeout.') def send_request(self, request_id, action, request, **params): diff --git a/refresh_tokens.tar.enc b/refresh_tokens.tar.enc new file mode 100644 index 0000000..b3ab8b0 Binary files /dev/null and b/refresh_tokens.tar.enc differ diff --git a/setup.py b/setup.py index e323d21..62acfe8 100644 --- a/setup.py +++ b/setup.py @@ -7,7 +7,7 @@ setup(name='devicehive', - version='2.1.0', + version='2.1.1', author='DataArt (http://dataart.com)', author_email='info@devicehive.com', url='https://devicehive.com', diff --git a/user_refresh_token.txt.enc b/user_refresh_token.txt.enc deleted file mode 100644 index 16bb085..0000000 Binary files a/user_refresh_token.txt.enc and /dev/null differ