Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make start timeout work properly. #216

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 25 additions & 2 deletions test/test_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,32 @@ def test_start_error(self):
server.ping = MagicMock()
server.ping.return_value = None
server.adb = MagicMock()
with patch("time.sleep"):
with patch("time.sleep") as time_sleep_mock, patch("time.time") as time_time_mock:
self._current_time = 0
time_sleep_mock.side_effect = self._time_sleep
time_time_mock.side_effect = lambda: self._current_time
with self.assertRaises(IOError):
server.start()
server.start(timeout=1)

def _time_sleep(self, timeout):
self._current_time += timeout

# Tests that start() timeout works correctly even in case communication
# with device takes too long.
def test_start_timeout_works_properly(self):

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test could be named better, particularly the "_works_properly" part. Maybe call it test_start_timeout_slow_device?

Also, any reason to go with comments over a docstring?

server = AutomatorServer()
server.push = MagicMock()
server.push.return_value = ["bundle.jar", "uiautomator-stub.jar"]
server.ping = MagicMock()
server.ping.side_effect = lambda: self._time_sleep(100)
server.adb = MagicMock()
with patch("time.sleep") as time_sleep_mock, patch("time.time") as time_time_mock:
self._current_time = 0
time_sleep_mock.side_effect = self._time_sleep
time_time_mock.side_effect = lambda: self._current_time
with self.assertRaises(IOError):
server.start(timeout=1)
self.assertGreater(100.2, self._current_time)

def test_auto_start(self):
try:
Expand Down
11 changes: 7 additions & 4 deletions uiautomator/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -474,10 +474,13 @@ def start(self, timeout=5):
self.uiautomator_process = self.adb.cmd(*cmd)
self.adb.forward(self.local_port, self.device_port)

while not self.alive and timeout > 0:
time.sleep(0.1)
timeout -= 0.1
if not self.alive:
step_secs = 0.1

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What are your thoughts about making this a defaulted argument?

A doc string would be good on this method too.

expiration_time = time.time() + timeout
while time.time() < expiration_time:
if self.alive:
break
time.sleep(step_secs)
else:
raise IOError("RPC server not started!")

def ping(self):
Expand Down