Skip to content

Commit

Permalink
Retry and timeout can be change by setting a class attribute (rytilah…
Browse files Browse the repository at this point in the history
…ti#884)

* Retry and timeout can be change by setting a class attribute

* Fix PR comments

* Add tests

* Improve test for device retry and timeout
  • Loading branch information
titilambert authored and xvlady committed May 9, 2021
1 parent 280619d commit 2b69d1b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
9 changes: 7 additions & 2 deletions miio/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,17 +106,21 @@ class Device(metaclass=DeviceGroupMeta):
This class should not be initialized directly but a device-specific class inheriting
it should be used instead of it."""

retry_count = 3
timeout = 5

def __init__(
self,
ip: str = None,
token: str = None,
start_id: int = 0,
debug: int = 0,
lazy_discover: bool = True,
timeout: int = 5,
timeout: int = None,
) -> None:
self.ip = ip
self.token = token
timeout = timeout if timeout is not None else self.timeout
self._protocol = MiIOProtocol(
ip, token, start_id, debug, lazy_discover, timeout
)
Expand All @@ -125,7 +129,7 @@ def send(
self,
command: str,
parameters: Any = None,
retry_count=3,
retry_count: int = None,
*,
extra_parameters=None,
) -> Any:
Expand All @@ -143,6 +147,7 @@ def send(
:param int retry_count: How many times to retry on error
:param dict extra_parameters: Extra top-level parameters
"""
retry_count = retry_count if retry_count is not None else self.retry_count
return self._protocol.send(
command, parameters, retry_count, extra_parameters=extra_parameters
)
Expand Down
27 changes: 27 additions & 0 deletions miio/tests/test_device.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,33 @@ def test_get_properties_splitting(mocker, max_properties):
assert send.call_count == math.ceil(len(properties) / max_properties)


def test_default_timeout_and_retry(mocker):
send = mocker.patch("miio.miioprotocol.MiIOProtocol.send")
d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff")
assert 5 == d._protocol._timeout
d.send(command="fake_command", parameters=[])
send.assert_called_with("fake_command", [], 3, extra_parameters=None)


def test_timeout_retry(mocker):
send = mocker.patch("miio.miioprotocol.MiIOProtocol.send")
d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff", timeout=4)
assert 4 == d._protocol._timeout
d.send("fake_command", [], 1)
send.assert_called_with("fake_command", [], 1, extra_parameters=None)
d.send("fake_command", [])
send.assert_called_with("fake_command", [], 3, extra_parameters=None)

class CustomDevice(Device):
retry_count = 5
timeout = 1

d2 = CustomDevice("127.0.0.1", "68ffffffffffffffffffffffffffffff")
assert 1 == d2._protocol._timeout
d2.send("fake_command", [])
send.assert_called_with("fake_command", [], 5, extra_parameters=None)


def test_unavailable_device_info_raises(mocker):
send = mocker.patch("miio.Device.send", side_effect=PayloadDecodeException)
d = Device("127.0.0.1", "68ffffffffffffffffffffffffffffff")
Expand Down

0 comments on commit 2b69d1b

Please sign in to comment.