-
Notifications
You must be signed in to change notification settings - Fork 644
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
Properly acquire local port after calculating it. #208
base: master
Are you sure you want to change the base?
Conversation
This makes the AutomatorServer to properly acquire the local port chosen for forwarding traffic to the server running on the device, so that other instances of uiautomator-based scripts don't acquire it before the current instance.
test/test_server.py
Outdated
def test_device_port(self): | ||
self.assertEqual(AutomatorServer().device_port, 9008) | ||
|
||
def test_start_success(self): | ||
server = AutomatorServer() | ||
server = AutomatorServer(local_port=1234) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thoughts on using a helper to create the AutomatorServer so each test doesn't have to specify local_port?
uiautomator/__init__.py
Outdated
@@ -404,6 +402,31 @@ def install(self): | |||
for apk in self.__apk_files: | |||
self.adb.cmd("install", "-r -t", os.path.join(base_dir, apk)).wait() | |||
|
|||
def get_forwarded_port(self): | |||
for s, lp, rp in self.adb.forward_list(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
s, lp, rp could be named better. Also, I know this module doesn't have the best documentation, but I think a docstring would be good.
uiautomator/__init__.py
Outdated
return None | ||
|
||
@property | ||
def local_port(self): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Same here with the docstring.
@@ -312,9 +312,13 @@ def devices(self): | |||
raise EnvironmentError("adb is not working.") | |||
return dict([s.split("\t") for s in out[index + len(match):].strip().splitlines() if s.strip()]) | |||
|
|||
def forward(self, local_port, device_port): | |||
def forward(self, local_port, device_port, rebind=True): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be good to add some test coverage here.
@mcdaniel67 Addressed your comments, thanks! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me, other than needing a pre-merge commit squash. I rebased this on top of my fix for travis and pushed it up to my fork to get a travis run.
https://travis-ci.org/mcdaniel67/uiautomator/builds/234656251
What's the status on this repo? |
This makes the AutomatorServer to properly acquire the local port chosen for forwarding
traffic to the server running on the device, so that other instances of uiautomator-based
scripts don't acquire it before the current instance.
Before the port was selected on AutomatorService instantiation (which is happening on AutomatorDevice instantiation), but only allocated on the first real query, which could cause issues when running two processes on the same machine. I.e. the following sequence of events would lead to two tests talking to the same device:
Process 1: device = uiautomator.Device(serial="1") // selected local port 9008
Process 2: device = uiautomator.Device(serial="2") // selected local port 9008 (because it's still free)
Process 1: device.screen.on() // forwarded local port 9008
Process 2: device.screen.on() // re-forwarded local port 9008, because it doesn't now that it's already forwarded
// now both processes talk to device with serial="2"
Now we do not select port on instantiation, but select and forward it when we actually need it first time. After trying to forward it it also checks whether the forwarding was successful, because some other process could do the forwarding in the meantime.
Also this changed port forwarding to use "no-rebind" parameter, which doesn't allow port to be silently re-forwarded, so if some other process forwards it before us we don't mess it.