@@ -61,38 +61,54 @@ exports.getStatus = getStatus;
61
61
* Waits for a WebDriver server to be healthy and accepting requests.
62
62
* @param {string } url Base URL of the server to query.
63
63
* @param {number } timeout How long to wait for the server.
64
- * @return {!promise.Promise } A promise that will resolve when the
65
- * server is ready.
64
+ * @param {Promise= } opt_cancelToken A promise used as a cancellation signal:
65
+ * if resolved before the server is ready, the wait will be terminated
66
+ * early with a {@link promise.CancellationError}.
67
+ * @return {!Promise } A promise that will resolve when the server is ready, or
68
+ * if the wait is cancelled.
66
69
*/
67
- exports . waitForServer = function ( url , timeout ) {
68
- var ready = promise . defer ( ) ,
69
- start = Date . now ( ) ;
70
- checkServerStatus ( ) ;
71
- return ready . promise ;
72
-
73
- function checkServerStatus ( ) {
74
- return getStatus ( url ) . then ( status => ready . fulfill ( status ) , onError ) ;
75
- }
76
-
77
- function onError ( e ) {
78
- // Some servers don't support the status command. If they are able to
79
- // response with an error, then can consider the server ready.
80
- if ( e instanceof error . UnsupportedOperationError ) {
81
- ready . fulfill ( ) ;
82
- return ;
70
+ exports . waitForServer = function ( url , timeout , opt_cancelToken ) {
71
+ return new Promise ( ( onResolve , onReject ) => {
72
+ let start = Date . now ( ) ;
73
+
74
+ let done = false ;
75
+ let resolve = ( status ) => {
76
+ done = true ;
77
+ onResolve ( status ) ;
78
+ } ;
79
+ let reject = ( err ) => {
80
+ done = true ;
81
+ onReject ( err ) ;
82
+ } ;
83
+
84
+ if ( opt_cancelToken ) {
85
+ opt_cancelToken . then ( _ => reject ( new promise . CancellationError ) ) ;
83
86
}
84
87
85
- if ( Date . now ( ) - start > timeout ) {
86
- ready . reject (
87
- Error ( 'Timed out waiting for the WebDriver server at ' + url ) ) ;
88
- } else {
89
- setTimeout ( function ( ) {
90
- if ( ready . promise . isPending ( ) ) {
91
- checkServerStatus ( ) ;
92
- }
93
- } , 50 ) ;
88
+ checkServerStatus ( ) ;
89
+ function checkServerStatus ( ) {
90
+ return getStatus ( url ) . then ( status => resolve ( status ) , onError ) ;
94
91
}
95
- }
92
+
93
+ function onError ( e ) {
94
+ // Some servers don't support the status command. If they are able to
95
+ // response with an error, then can consider the server ready.
96
+ if ( e instanceof error . UnsupportedOperationError ) {
97
+ resolve ( { } ) ;
98
+ return ;
99
+ }
100
+
101
+ if ( Date . now ( ) - start > timeout ) {
102
+ reject ( Error ( 'Timed out waiting for the WebDriver server at ' + url ) ) ;
103
+ } else {
104
+ setTimeout ( function ( ) {
105
+ if ( ! done ) {
106
+ checkServerStatus ( ) ;
107
+ }
108
+ } , 50 ) ;
109
+ }
110
+ }
111
+ } ) ;
96
112
} ;
97
113
98
114
@@ -101,39 +117,59 @@ exports.waitForServer = function(url, timeout) {
101
117
* timeout expires.
102
118
* @param {string } url The URL to poll.
103
119
* @param {number } timeout How long to wait, in milliseconds.
104
- * @return {!promise.Promise } A promise that will resolve when the
105
- * URL responds with 2xx.
120
+ * @param {Promise= } opt_cancelToken A promise used as a cancellation signal:
121
+ * if resolved before the a 2xx response is received, the wait will be
122
+ * terminated early with a {@link promise.CancellationError}.
123
+ * @return {!Promise } A promise that will resolve when a 2xx is received from
124
+ * the given URL, or if the wait is cancelled.
106
125
*/
107
- exports . waitForUrl = function ( url , timeout ) {
108
- var client = new HttpClient ( url ) ,
109
- request = new HttpRequest ( 'GET' , '' ) ,
110
- ready = promise . defer ( ) ,
111
- start = Date . now ( ) ;
112
- testUrl ( ) ;
113
- return ready . promise ;
114
-
115
- function testUrl ( ) {
116
- client . send ( request ) . then ( onResponse , onError ) ;
117
- }
118
-
119
- function onError ( ) {
120
- if ( Date . now ( ) - start > timeout ) {
121
- ready . reject ( Error (
122
- 'Timed out waiting for the URL to return 2xx: ' + url ) ) ;
123
- } else {
124
- setTimeout ( function ( ) {
125
- if ( ready . promise . isPending ( ) ) {
126
- testUrl ( ) ;
127
- }
128
- } , 50 ) ;
126
+ exports . waitForUrl = function ( url , timeout , opt_cancelToken ) {
127
+ return new Promise ( ( onResolve , onReject ) => {
128
+ let client = new HttpClient ( url ) ;
129
+ let request = new HttpRequest ( 'GET' , '' ) ;
130
+ let start = Date . now ( ) ;
131
+
132
+ let done = false ;
133
+ let resolve = ( ) => {
134
+ done = true ;
135
+ onResolve ( ) ;
136
+ } ;
137
+ let reject = ( err ) => {
138
+ done = true ;
139
+ onReject ( err ) ;
140
+ } ;
141
+
142
+ if ( opt_cancelToken ) {
143
+ opt_cancelToken . then ( _ => reject ( new promise . CancellationError ) ) ;
144
+ }
145
+
146
+ testUrl ( ) ;
147
+
148
+ function testUrl ( ) {
149
+ client . send ( request ) . then ( onResponse , onError ) ;
150
+ }
151
+
152
+ function onError ( ) {
153
+ if ( Date . now ( ) - start > timeout ) {
154
+ reject ( Error ( 'Timed out waiting for the URL to return 2xx: ' + url ) ) ;
155
+ } else {
156
+ setTimeout ( function ( ) {
157
+ if ( ! done ) {
158
+ testUrl ( ) ;
159
+ }
160
+ } , 50 ) ;
161
+ }
129
162
}
130
- }
131
163
132
- function onResponse ( response ) {
133
- if ( ! ready . promise . isPending ( ) ) return ;
134
- if ( response . status > 199 && response . status < 300 ) {
135
- return ready . fulfill ( ) ;
164
+ function onResponse ( response ) {
165
+ if ( done ) {
166
+ return ;
167
+ }
168
+ if ( response . status > 199 && response . status < 300 ) {
169
+ resolve ( ) ;
170
+ return ;
171
+ }
172
+ onError ( ) ;
136
173
}
137
- onError ( ) ;
138
- }
174
+ } ) ;
139
175
} ;
0 commit comments