forked from SeleniumHQ/selenium
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Generalize SocketLock to account for Chrome port race condition
When the Chrome browser boots up via the Ruby library, it attempts to find a free port and launch the bridge on that port. However, it's possible that, if you're running tests in parallel, two bridges will try to bind to the same port at the same time. This will cause a very obtuse "end of file" error as documented [here][1]. This patch is based on one initially proposed in the original [bug report][2]. However, it goes a step further, taking the technical comments into account, and generalizing the `SocketLock` class used in the Firefox launcher to assist. [1]: grosser/parallel_tests#322 [2]: https://code.google.com/p/selenium/issues/detail?id=8241 Signed-off-by: Alexei Barantsev <barancev@gmail.com>
- Loading branch information
1 parent
d6b0a91
commit 437725c
Showing
5 changed files
with
124 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
# Licensed to the Software Freedom Conservancy (SFC) under one | ||
# or more contributor license agreements. See the NOTICE file | ||
# distributed with this work for additional information | ||
# regarding copyright ownership. The SFC licenses this file | ||
# to you under the Apache License, Version 2.0 (the | ||
# "License"); you may not use this file except in compliance | ||
# with the License. You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, | ||
# software distributed under the License is distributed on an | ||
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
# KIND, either express or implied. See the License for the | ||
# specific language governing permissions and limitations | ||
# under the License. | ||
|
||
module Selenium | ||
module WebDriver | ||
class SocketLock | ||
|
||
def initialize(port, timeout) | ||
@port = port | ||
@timeout = timeout | ||
end | ||
|
||
# | ||
# Attempt to acquire a lock on the given port. Control is yielded to an | ||
# execution block if the lock could be successfully obtained. | ||
# | ||
|
||
def locked(&blk) | ||
lock | ||
|
||
begin | ||
yield | ||
ensure | ||
release | ||
end | ||
end | ||
|
||
private | ||
|
||
def lock | ||
max_time = Time.now + @timeout | ||
|
||
until can_lock? || Time.now >= max_time | ||
sleep 0.1 | ||
end | ||
|
||
unless did_lock? | ||
raise Error::WebDriverError, "unable to bind to locking port #{@port} within #{@timeout} seconds" | ||
end | ||
end | ||
|
||
def release | ||
@server && @server.close | ||
end | ||
|
||
def can_lock? | ||
@server = TCPServer.new(Platform.localhost, @port) | ||
ChildProcess.close_on_exec @server | ||
|
||
true | ||
rescue SocketError, Errno::EADDRINUSE, Errno::EBADF => ex | ||
$stderr.puts "#{self}: #{ex.message}" if $DEBUG | ||
false | ||
end | ||
|
||
def did_lock? | ||
!!@server | ||
end | ||
|
||
end # SocketLock | ||
end # WebDriver | ||
end # Selenium |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.