@@ -9,6 +9,7 @@ import { DriverMaster } from './DriverMaster';
99import * as KeyEffects from './KeyEffects' ;
1010import * as MouseEffects from './MouseEffects' ;
1111import * as Routes from './Routes' ;
12+ import { REMOTE_IDLE_TIMEOUT_SECONDS } from '../auto/RemoteDriver' ;
1213
1314type Executor < D , T > = ( driver : Browser ) => ( data : D ) => Promise < T > ;
1415
@@ -75,7 +76,7 @@ export const create = (master: DriverMaster | null, maybeDriver: Attempt<any, Br
7576 } ;
7677 } ;
7778
78- const sendKeepAlive = ( driver : Browser ) => Promise . resolve ( void driver . execute ( ( ) => console . info ( 'keep-alive' , Date . now ( ) ) ) ) ;
79+ const sendKeepAlive = ( driver : Browser ) => Promise . resolve ( void driver . execute ( ( ) => console . info ( 'server keep-alive' , Date . now ( ) ) ) ) ;
7980
8081 const keepAliveAction = ( ) => Attempt . cata ( maybeDriver , ( ) => Promise . resolve ( ) ,
8182 ( driver ) => waitForDriverReady ( maxInvalidAttempts , ( ) => sendKeepAlive ( driver ) ) ) ;
@@ -123,6 +124,25 @@ export const create = (master: DriverMaster | null, maybeDriver: Attempt<any, Br
123124
124125 const c = Controller . create ( stickyFirstSession , overallTimeout , testfiles , loglevel ) ;
125126
127+ const maybeSendKeepAlive : ( ) => Promise < void > = ( ( ) => {
128+ let lastKeepAlive = Date . now ( ) ;
129+ /*
130+ * If we're within a minute of the remote driver timeout, send a keep-alive.
131+ *
132+ * In theory this is only required if no other action has been taken in the last x minutes,
133+ * but this happens so rarely it should be fine.
134+ */
135+ const keepAliveTimer = ( Math . max ( 120 , REMOTE_IDLE_TIMEOUT_SECONDS - 60 ) ) * 1000 ;
136+ return ( ) => {
137+ if ( Date . now ( ) - lastKeepAlive > keepAliveTimer ) {
138+ lastKeepAlive = Date . now ( ) ;
139+ return keepAliveAction ( ) ;
140+ } else {
141+ return Promise . resolve ( ) ;
142+ }
143+ } ;
144+ } ) ( ) ;
145+
126146 const routers = [
127147 driverRouter ( '/keys' , 'Keys' , KeyEffects . executor , false ) ,
128148 driverRouter ( '/mouse' , 'Mouse' , MouseEffects . executor , true ) ,
@@ -137,7 +157,7 @@ export const create = (master: DriverMaster | null, maybeDriver: Attempt<any, Br
137157 } ) ,
138158 Routes . effect ( 'POST' , '/tests/results' , ( data : ResultsData ) => {
139159 c . recordTestResults ( data . session , data . results ) ;
140- return Promise . resolve ( ) ;
160+ return maybeSendKeepAlive ( ) ;
141161 } ) ,
142162 Routes . effect ( 'POST' , '/tests/done' , ( data : DoneData ) => {
143163 Coverage . writeCoverageData ( data . coverage ) ;
0 commit comments