Skip to content

Properly acquire local port after calculating it.#208

Open
artikz wants to merge 4 commits intoxiaocong:masterfrom
artikz:fix-multi-process
Open

Properly acquire local port after calculating it.#208
artikz wants to merge 4 commits intoxiaocong:masterfrom
artikz:fix-multi-process

Conversation

@artikz
Copy link

@artikz artikz commented Mar 26, 2017

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.

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.

def test_start_success(self):
server = AutomatorServer()
server = AutomatorServer(local_port=1234)

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?

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():

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.

return None

@property
def local_port(self):

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.

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):

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.

@artikz
Copy link
Author

artikz commented May 21, 2017

@mcdaniel67 Addressed your comments, thanks!

Copy link

@mcdaniel67 mcdaniel67 left a 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

@CourageTrain
Copy link

What's the status on this repo?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants