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

TypeError: Worker is not a constructor #324

Closed
Xiryl opened this issue Jan 20, 2021 · 4 comments · Fixed by #325
Closed

TypeError: Worker is not a constructor #324

Xiryl opened this issue Jan 20, 2021 · 4 comments · Fixed by #325

Comments

@Xiryl
Copy link

Xiryl commented Jan 20, 2021

Hi,
I'm using the library with the worker options, using the onResponse as file module as said on the documentation:

const runWrkPOST = (connections, pipelining, duration, title) => {
  const instance = autocannon(
    {
      url: 'http://localhost:3000',
      duration: 2,
      title,
      workers: 2,
      headers: {
        'Content-type': 'application/json; charset=utf-8',
      },
      requests: [
        {
          method: 'POST',
          path: '/api/v1/k/push/example',
          onResponse: path.join(__dirname, 'helpers', 'on-response'),
        },
      ],
    },
    (err, result) => console.log(result),
  );
  autocannon.track(instance, {
    renderProgressBar: true,
    renderResultsTable: true,
    renderLatencyTable: true,
  });
};

where in ./helpers/on-response.js:

const fastJson = require('fast-json-stringify');

module.exports = (client, statusCode, resBytes, responseTime) => {
  const stringify = fastJson({
    title: 'Message Schema',
    type: 'object',
    properties: {
      message: {
        type: 'string',
      },
    },
  });
  client.setHeaders({ 'Content-type': 'application/json; charset=utf-8' });
  client.setBody(
    stringify({
      message: `RESTAPI,datatype=Double,unit=°C ATemp=19.19 ${new Date().getTime()}000000`,
    }),
  );
};

But I'm getting the following error:

/Users/fabio/projects/internal/test/test/node_modules/autocannon/lib/manager.js:69
      const worker = new Worker(path.resolve(__dirname, './worker.js'), { workerData: { opts: workerOpts } })
                     ^

TypeError: Worker is not a constructor
    at init (/Users/fabio/projects/internal/test/test/node_modules/autocannon/lib/manager.js:69:22)
    at initWorkers (/Users/fabio/projects/internal/test/test/node_modules/autocannon/lib/manager.js:107:3)
    at _init (/Users/fabio/projects/internal/test/test/node_modules/autocannon/lib/init.js:57:5)
    at init (/Users/fabio/projects/internal/test/test/node_modules/autocannon/lib/init.js:13:10)
    at Object.runWrkPOST (/Users/fabio/projects/internal/test/test/src/bapi.js:61:20)
    at Object.<anonymous> (/Users/fabio/projects/internal/test/test/index.js:22:6)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:789:10)
    at Module.load (internal/modules/cjs/loader.js:653:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:593:12)

I also tried to install the autocannon globally and run to check if the problem is in my code, but I'm getting the same error:

fabio@mbp-fabio test % autocannon -c 100 -d 5 -p 10 -w 2  http://localhost:3000/api/v1/status
worker_threads is not available
(node:25183) UnhandledPromiseRejectionWarning: TypeError: Worker is not a constructor
    at init (/usr/local/lib/node_modules/autocannon/lib/manager.js:69:22)
    at initWorkers (/usr/local/lib/node_modules/autocannon/lib/manager.js:107:3)
    at _init (/usr/local/lib/node_modules/autocannon/lib/init.js:57:5)
    at Promise (/usr/local/lib/node_modules/autocannon/lib/init.js:24:7)
    at new Promise (<anonymous>)
    at _init (/usr/local/lib/node_modules/autocannon/lib/init.js:23:21)
    at init (/usr/local/lib/node_modules/autocannon/lib/init.js:13:10)
    at start (/usr/local/lib/node_modules/autocannon/autocannon.js:232:5)
    at Object.<anonymous> (/usr/local/lib/node_modules/autocannon/autocannon.js:259:3)
    at Module._compile (internal/modules/cjs/loader.js:778:30)
(node:25183) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:25183) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

Am I forgetting something to use the workers options correctly?
Thanks


I'm running from MAC OS 11.0.1 (Big Sur) and node v10.16.3

@salmanm
Copy link
Collaborator

salmanm commented Jan 20, 2021

You'll need to use Node 11.7.0+ to be able to use workers.

But it should show the readable error message.

@Xiryl
Copy link
Author

Xiryl commented Jan 20, 2021

Oh good, I have to update node, my bad!
Thank you @salmanm

@Xiryl
Copy link
Author

Xiryl commented Jan 20, 2021

Updating to v14.15.4 work like a charm.
Thanks

@Xiryl Xiryl closed this as completed Jan 20, 2021
@salmanm
Copy link
Collaborator

salmanm commented Jan 20, 2021

Reopening as the library should print an error message

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants