Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(324): Show error when no worker support is found #325

Merged
merged 4 commits into from
Jan 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion autocannon.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,9 @@ function start (argv) {
})
})
} else {
initJob(argv)
initJob(argv).catch((err) => {
console.error(err.message)
})
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
'use strict'

const semver = require('semver')
const hasWorkerSupport = semver.gte(process.versions.node, '11.7.0')

module.exports = { hasWorkerSupport }
2 changes: 2 additions & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const timestring = require('timestring')
const { checkURL } = require('./url')
const multipart = require('./multipart')
const { parseHAR } = require('./parseHAR')
const { hasWorkerSupport } = require('./util')

const isValidFn = (opt) => (!opt || typeof opt === 'function' || typeof opt === 'string')

Expand Down Expand Up @@ -47,6 +48,7 @@ function defaultOpts (opts) {
}

module.exports = function validateOpts (opts, cbPassedIn) {
if (opts.workers && !hasWorkerSupport) return new Error('Please use node >= 11.7.0 for workers support')
// these need to be validated before defaulting
if (minIfPresent(opts.bailout, 1)) return lessThanOneError('bailout threshold')
if (minIfPresent(opts.connectionRate, 1)) return lessThanOneError('connectionRate')
Expand Down
7 changes: 4 additions & 3 deletions lib/worker_threads.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ let workerThreads = {}
try {
workerThreads = require('worker_threads')
} catch (err) {
// we don't need the error but can't have catch block
// without err as node 8 doesn't support that
if (err) console.log('worker_threads is not available')
if (err) {
// we don't need the error but can't have catch block
// without err as node 8 doesn't support that
}

workerThreads = {
isMainThread: true
Expand Down
2 changes: 1 addition & 1 deletion test/cli.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const fs = require('fs')
const os = require('os')
const childProcess = require('child_process')
const helper = require('./helper')
const hasWorkerSupport = require('./utils/has-worker-support')
const { hasWorkerSupport } = require('../lib/util')

test('should run benchmark against server', (t) => {
const lines = [
Expand Down
5 changes: 4 additions & 1 deletion test/run.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
const os = require('os')
const path = require('path')
const test = require('tap').test
const semver = require('semver')
const initJob = require('../lib/init')
const defaultOptions = require('../lib/defaultOptions')
const helper = require('./helper')
const server = helper.startServer()
const isNode15 = semver.gte(process.versions.node, '15.0.0')

test('init', (t) => {
initJob({
Expand Down Expand Up @@ -366,7 +368,8 @@ test('run should accept a unix socket/windows pipe', (t) => {
})

for (let i = 1; i <= 5; i++) {
test(`run should count all ${i}xx status codes`, (t) => {
// TODO we should not skip those tests
test(`run should count all ${i}xx status codes`, { skip: isNode15 }, (t) => {
salmanm marked this conversation as resolved.
Show resolved Hide resolved
const server = helper.startServer({ statusCode: i * 100 + 2 })

initJob({
Expand Down
2 changes: 1 addition & 1 deletion test/runMultipart.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const { promisify } = require('util')
const initJob = require('../lib/init')
const helper = require('./helper')
const writef = promisify(writeFile)
const hasWorkerSupport = require('./utils/has-worker-support')
const { hasWorkerSupport } = require('../lib/util')

test('run should return an error with invalid form options', async t => {
const cases = [
Expand Down
20 changes: 19 additions & 1 deletion test/workers.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,28 @@ const path = require('path')
const test = require('tap').test
const http = require('http')
const initJob = require('../lib/init')
const hasWorkerSupport = require('./utils/has-worker-support')
const { hasWorkerSupport } = require('../lib/util')
const helper = require('./helper')
const server = helper.startServer()

test('returns error when no worker support was found', (t) => {
initJob({
url: 'http://localhost:' + server.address().port,
connections: 3,
workers: 3,
amount: 6,
title: 'with-workers'
}, function (err, result) {
if (hasWorkerSupport) {
t.error(err)
} else {
t.equal(err.message, 'Please use node >= 11.7.0 for workers support')
}

t.end()
})
})

test('init with workers', { skip: !hasWorkerSupport }, (t) => {
initJob({
url: 'http://localhost:' + server.address().port,
Expand Down