Skip to content

Commit

Permalink
fix: add extra period to handle ETIMEDOUT error (#335)
Browse files Browse the repository at this point in the history
* add wait on test with ETIMEDOUT in the first 200 seconds

* add one more iteration before the timeout

* add extra timeout period
  • Loading branch information
bahmutov authored Mar 30, 2021
1 parent d66a709 commit f065379
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 11 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/example-wait-on.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,10 +78,27 @@ jobs:
- name: Cypress tests
uses: ./
with:
# in this situation the server does not respond
# for the first period causing ETIMEDOUT errors
working-directory: examples/wait-on
start: npm run start3
wait-on: 'http://localhost:3050'

wait3-for-200-seconds:
runs-on: ubuntu-20.04
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Cypress tests
uses: ./
with:
# in this situation the server does not respond
# for the first period causing ETIMEDOUT errors
working-directory: examples/wait-on
start: npm run start3-after-200-seconds
wait-on: 'http://localhost:3050'
wait-on-timeout: 210

wait4:
runs-on: ubuntu-20.04
steps:
Expand Down
11 changes: 9 additions & 2 deletions dist/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -66190,6 +66190,11 @@ const ping = (url, timeout) => {
// we expect the server to respond within a time limit
// and if it does not - retry up to total "timeout" duration
const individualPingTimeout = Math.min(timeout, 30000)

// add to the timeout max individual ping timeout
// to avoid long-waiting ping from "rolling" over the end
// and preventing pinging the last time
timeout += individualPingTimeout
const limit = Math.ceil(timeout / individualPingTimeout)

core.debug(`total ping timeout ${timeout}`)
Expand All @@ -66216,10 +66221,12 @@ const ping = (url, timeout) => {
)
if (elapsed > timeout) {
console.error(
'%s timed out on retry %d of %d',
'%s timed out on retry %d of %d, elapsed %dms, limit %dms',
url,
attemptCount,
limit
limit,
elapsed,
timeout
)
return 0
}
Expand Down
14 changes: 8 additions & 6 deletions examples/wait-on/index3.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,26 @@ const http = require('http')
const arg = require('arg')

const args = arg({
'--port': Number
'--port': Number,
'--delay': Number
})
const port = args['--port'] || 3050
const errorPeriodSeconds = args['--delay'] || 40

const errorPeriodSeconds = 40
const endErrorsAt = +new Date() + errorPeriodSeconds * 1000

log('creating the server on port %d', port)
log('will not respond for the first %d seconds', errorPeriodSeconds)

setTimeout(function() {
log('from now on will answer all good')
setTimeout(function () {
log('the server will now answer ok')
}, errorPeriodSeconds * 1000)

const server = http.createServer((req, res) => {
log('request %s %s', req.method, req.url)
if (new Date() < endErrorsAt) {
log('not responding')
const now = +new Date()
if (now < endErrorsAt) {
log('not responding, %d ms left', endErrorsAt - now)
return
}

Expand Down
1 change: 1 addition & 0 deletions examples/wait-on/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"start-after-50-seconds": "node ./index2 --delay 50",
"start-after-120-seconds": "node ./index2 --delay 120",
"start3": "node ./index3",
"start3-after-200-seconds": "node ./index3 --delay 200",
"start4": "node ./index4"
},
"author": "",
Expand Down
2 changes: 1 addition & 1 deletion src/ping-cli.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// node ./src/ping-cli <url>

const { ping } = require('./ping')
const timeoutSeconds = 60
const timeoutSeconds = 30
const url = process.argv[2]
console.log('pinging url %s for %d seconds', url, timeoutSeconds)
if (!url) {
Expand Down
11 changes: 9 additions & 2 deletions src/ping.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ const ping = (url, timeout) => {
// we expect the server to respond within a time limit
// and if it does not - retry up to total "timeout" duration
const individualPingTimeout = Math.min(timeout, 30000)

// add to the timeout max individual ping timeout
// to avoid long-waiting ping from "rolling" over the end
// and preventing pinging the last time
timeout += individualPingTimeout
const limit = Math.ceil(timeout / individualPingTimeout)

core.debug(`total ping timeout ${timeout}`)
Expand All @@ -44,10 +49,12 @@ const ping = (url, timeout) => {
)
if (elapsed > timeout) {
console.error(
'%s timed out on retry %d of %d',
'%s timed out on retry %d of %d, elapsed %dms, limit %dms',
url,
attemptCount,
limit
limit,
elapsed,
timeout
)
return 0
}
Expand Down

0 comments on commit f065379

Please sign in to comment.