@@ -26,12 +26,11 @@ module PhantomJS
26
26
#
27
27
28
28
class Service
29
- START_TIMEOUT = 20
30
- STOP_TIMEOUT = 5
31
- DEFAULT_PORT = 8910
32
- MISSING_TEXT = "Unable to find phantomjs executable."
33
-
34
- attr_reader :uri
29
+ START_TIMEOUT = 20
30
+ SOCKET_LOCK_TIMEOUT = 45
31
+ STOP_TIMEOUT = 5
32
+ DEFAULT_PORT = 8910
33
+ MISSING_TEXT = "Unable to find phantomjs executable."
35
34
36
35
def self . executable_path
37
36
@executable_path ||= (
@@ -44,35 +43,32 @@ def self.executable_path
44
43
end
45
44
46
45
def self . default_service ( port = nil )
47
- new executable_path , port || PortProber . above ( DEFAULT_PORT )
46
+ new executable_path , DEFAULT_PORT
48
47
end
49
48
50
49
def initialize ( executable_path , port )
51
- @uri = URI . parse "http:// #{ Platform . localhost } : #{ port } "
50
+ @host = Platform . localhost
52
51
@executable = executable_path
53
52
end
54
53
55
54
def start ( args = [ ] )
56
55
if @process && @process . alive?
57
- raise "already started: #{ @ uri. inspect } #{ @executable . inspect } "
56
+ raise "already started: #{ uri . inspect } #{ @executable . inspect } "
58
57
end
59
58
60
- @process = create_process ( args )
61
- @process . start
62
-
63
- socket_poller = SocketPoller . new Platform . localhost , @uri . port , START_TIMEOUT
59
+ Platform . exit_hook { stop } # make sure we don't leave the server running
64
60
65
- unless socket_poller . connected?
66
- raise Error ::WebDriverError , "unable to connect to phantomjs @ #{ @uri } after #{ START_TIMEOUT } seconds"
61
+ socket_lock . locked do
62
+ find_free_port
63
+ start_process ( args )
64
+ connect_until_stable
67
65
end
68
-
69
- Platform . exit_hook { stop } # make sure we don't leave the server running
70
66
end
71
67
72
68
def stop
73
69
return if @process . nil? || @process . exited?
74
70
75
- Net ::HTTP . start ( uri . host , uri . port ) do |http |
71
+ Net ::HTTP . start ( @ host, @ port) do |http |
76
72
http . open_timeout = STOP_TIMEOUT / 2
77
73
http . read_timeout = STOP_TIMEOUT / 2
78
74
@@ -89,9 +85,19 @@ def stop
89
85
end
90
86
end
91
87
92
- def create_process ( args )
93
- server_command = [ @executable , "--webdriver=#{ @uri . port } " , *args ]
94
- process = ChildProcess . build ( *server_command . compact )
88
+ def find_free_port
89
+ @port = PortProber . above @port
90
+ end
91
+
92
+ def uri
93
+ URI . parse "http://#{ @host } :#{ @port } "
94
+ end
95
+
96
+ private
97
+
98
+ def start_process ( args )
99
+ server_command = [ @executable , "--webdriver=#{ @port } " , *args ]
100
+ @process = ChildProcess . build ( *server_command . compact )
95
101
96
102
if $DEBUG == true
97
103
process . io . inherit!
@@ -100,10 +106,22 @@ def create_process(args)
100
106
process . io . stdout = process . io . stderr = File . new ( Platform . null_device , 'w' )
101
107
end
102
108
103
- process
109
+ @process . start
110
+ end
111
+
112
+ def connect_until_stable
113
+ socket_poller = SocketPoller . new @host , @port , START_TIMEOUT
114
+
115
+ unless socket_poller . connected?
116
+ raise Error ::WebDriverError , "unable to connect to phantomjs @ #{ uri } after #{ START_TIMEOUT } seconds"
117
+ end
118
+ end
119
+
120
+ def socket_lock
121
+ @socket_lock ||= SocketLock . new ( @port - 1 , SOCKET_LOCK_TIMEOUT )
104
122
end
105
123
106
124
end # Service
107
125
end # PhantomJS
108
126
end # WebDriver
109
- end # Service
127
+ end # Service
0 commit comments