@@ -29,47 +29,39 @@ class OSRMBaseLoader {
2929 const argsAsString = args . join ( ' ' ) ;
3030 log ( `running ${ cmd } ${ argsAsString } ` ) ;
3131
32- this . child = child_process . spawn (
33- cmd ,
34- args ,
35- { env : scenario . environment } ,
36- ) ;
32+ return new Promise ( ( resolve ) => {
33+ this . child = child_process . spawn (
34+ cmd ,
35+ args ,
36+ { env : scenario . environment } ,
37+ ) ;
38+
39+ this . child . stdout . on ( 'data' , ( data ) => {
40+ if ( data . includes ( 'running and waiting for requests' ) ) {
41+ log ( 'Routed running and waiting for requests' ) ;
42+ resolve ( ) ;
43+ }
44+ } ) ;
3745
38- this . child . on ( 'exit' , ( code ) => {
39- log ( `osrm-routed completed with exit code ${ code } ` ) ;
40- this . child = null ;
46+ this . child . on ( 'exit' , ( code ) => {
47+ log ( `osrm-routed completed with exit code ${ code } ` ) ;
48+ this . child = null ;
49+ } ) ;
4150 } ) ;
42-
43- return this . waitForConnection ( ) ;
4451 }
4552
46- // Terminates OSRM server process gracefully
53+ /**
54+ * Terminates the OSRM server process gracefully
55+ *
56+ * @returns A promise resolved when routed has terminated.
57+ */
4758 kill ( ) {
48- if ( this . osrmIsRunning ( ) ) {
49- const child = this . child ;
50- const p = new Promise ( ( resolve ) => {
51- child . on ( 'exit' , resolve ) ;
52- } ) ;
53- child . kill ( ) ;
54- return p ;
55- }
56- return Promise . resolve ( ) ;
57- }
58-
59- /** Returns a promise resolved when the server is up. */
60- waitForConnection ( ) {
61- const seconds = 10 ;
62- const waitOptions = {
63- resources : [ `tcp:${ env . wp . ip } :${ env . wp . port } ` ] ,
64- delay : 10 , // initial delay in ms
65- interval : 10 , // poll interval in ms
66- timeout : seconds * 1000 , // timeout in ms
67- } ;
68- const p = waitOn ( waitOptions ) ;
69- p . catch ( ( ) => { throw new Error (
70- `Could not connect to osrm-routed after ${ seconds } s.`
71- ) ; } ) ;
72- return p ;
59+ if ( ! this . osrmIsRunning ( ) )
60+ return Promise . resolve ( ) ;
61+ return new Promise ( ( resolve ) => {
62+ this . child . on ( 'close' , resolve ) ;
63+ this . child . kill ( ) ;
64+ } ) ;
7365 }
7466
7567 osrmIsRunning ( ) {
@@ -80,11 +72,19 @@ class OSRMBaseLoader {
8072
8173 /** Called at the init of the cucumber run */
8274 beforeAll ( ) { return Promise . resolve ( ) ; }
83- /** Called at the start of each scenario */
75+ /**
76+ * Called at the start of each scenario
77+ *
78+ * @returns A promise resolved when routed is ready for connections.
79+ */
8480 before ( _scenario ) {
8581 return Promise . resolve ( ) ;
8682 }
87- /** Called at the end of each scenario */
83+ /**
84+ * Called at the end of each scenario
85+ *
86+ * @returns A promise resolved when routed has terminated.
87+ */
8888 after ( _scenario ) {
8989 return this . kill ( ) ;
9090 }
@@ -187,38 +187,43 @@ export class OSRMDatastoreLoader extends OSRMBaseLoader {
187187 const argsAsString = args . join ( ' ' ) ;
188188 this . logSync ( `running ${ cmd } ${ argsAsString } ` ) ;
189189
190- this . child = child_process . spawn (
191- cmd ,
192- args ,
193- { env : scenario . environment } ,
194- ) ;
195-
196- this . child . stdout . on ( 'data' , ( data ) => {
197- if ( data . includes ( 'updated facade' ) ) {
198- this . logSync ( 'Facade updated and promise resolved' ) ;
199- this . resolve ( ) ;
200- }
201- } ) ;
190+ return new Promise ( ( resolve ) => {
191+ this . child = child_process . spawn (
192+ cmd ,
193+ args ,
194+ { env : scenario . environment } ,
195+ ) ;
196+
197+ this . child . stdout . on ( 'data' , ( data ) => {
198+ this . logSync ( `osrm-routed stdout:\n${ data } ` ) ;
199+ if ( data . includes ( 'updated facade' ) ) {
200+ this . logSync ( 'Facade updated and promise resolved' ) ;
201+ this . resolve ( ) ;
202+ }
203+ else if ( data . includes ( 'running and waiting for requests' ) ) {
204+ this . logSync ( 'Routed running and waiting for requests' ) ;
205+ resolve ( ) ;
206+ }
207+ } ) ;
202208
203- // we MUST consume these or the osrm-routed process will block eventually
204- this . child . stderr . on ( 'data' , ( data ) => this . logSync ( `osrm-routed stderr:\n${ data } ` ) ) ;
205- this . child . stdout . on ( 'data' , ( data ) => this . logSync ( `osrm-routed stdout:\n ${ data } ` ) ) ;
206-
207- this . child . on ( 'exit' , ( code , signal ) => {
208- this . child = null ;
209- if ( signal != null ) {
210- const msg = `osrm-routed aborted with signal ${ signal } ` ;
211- this . logSync ( msg ) ;
212- // throw new Error(msg);
213- }
214- if ( code != null ) {
215- const msg = `osrm-routed completed with exit code ${ code } ` ;
216- this . logSync ( msg ) ;
217- if ( code != 0 )
218- throw new Error ( msg ) ;
219- }
209+ // we MUST consume stdout and stderr or the osrm-routed process will block eventually
210+ this . child . stderr . on ( 'data' , ( data ) => this . logSync ( `osrm-routed stderr:\n${ data } ` ) ) ;
211+
212+ this . child . on ( 'exit' , ( code , signal ) => {
213+ this . child = null ;
214+ if ( signal ! = null ) {
215+ const msg = `osrm-routed aborted with signal ${ signal } ` ;
216+ this . logSync ( msg ) ;
217+ // throw new Error (msg);
218+ }
219+ if ( code != null ) {
220+ const msg = `osrm-routed completed with exit code ${ code } ` ;
221+ this . logSync ( msg ) ;
222+ if ( code != 0 )
223+ throw new Error ( msg ) ;
224+ }
225+ } ) ;
220226 } ) ;
221- return this . waitForConnection ( ) ;
222227 }
223228 after ( ) {
224229 this . current_scenario = null ;
0 commit comments