Skip to content

Commit

Permalink
feat: throw error when create server port is missing (#62)
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 11, 2023
1 parent 2f5e91a commit eb890c3
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
9 changes: 2 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,15 @@
Sharing Connection among Multi-Process Nodejs

[![NPM version][npm-image]][npm-url]
[![build status][travis-image]][travis-url]
[![CI](https://github.com/node-modules/cluster-client/actions/workflows/nodejs.yml/badge.svg)](https://github.com/node-modules/cluster-client/actions/workflows/nodejs.yml)
[![Test coverage][codecov-image]][codecov-url]
[![David deps][david-image]][david-url]
[![Known Vulnerabilities][snyk-image]][snyk-url]
[![npm download][download-image]][download-url]

[npm-image]: https://img.shields.io/npm/v/cluster-client.svg?style=flat-square
[npm-url]: https://npmjs.org/package/cluster-client
[travis-image]: https://img.shields.io/travis/node-modules/cluster-client.svg?style=flat-square
[travis-url]: https://travis-ci.org/node-modules/cluster-client
[codecov-image]: https://codecov.io/gh/node-modules/cluster-client/branch/master/graph/badge.svg
[codecov-url]: https://codecov.io/gh/node-modules/cluster-client
[david-image]: https://img.shields.io/david/node-modules/cluster-client.svg?style=flat-square
[david-url]: https://david-dm.org/node-modules/cluster-client
[snyk-image]: https://snyk.io/test/npm/cluster-client/badge.svg?style=flat-square
[snyk-url]: https://snyk.io/test/npm/cluster-client
[download-image]: https://img.shields.io/npm/dm/cluster-client.svg?style=flat-square
Expand Down Expand Up @@ -299,4 +294,4 @@ class APIClient extends APIClientBase {

For more information, you can refer to the [discussion](https://github.com/eggjs/egg/issues/322)

[MIT](LICENSE)
[MIT](LICENSE)
5 changes: 3 additions & 2 deletions lib/server.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const debug = require('debug')('cluster-client:lib:server');
const net = require('net');
const Base = require('sdk-base');
Expand Down Expand Up @@ -170,6 +168,9 @@ class ClusterServer extends Base {
*/
static async create(name, port) {
const key = `${name}@${port}`;
if (typeof port !== 'number') {
throw new Error(`[ClusterClient.server.create:${name}] port should be a number, but got ${JSON.stringify(port)}`);
}
let instance = serverMap.get(port);
if (instance && !instance.isClosed) {
debug('create %j instance exists', key);
Expand Down
1 change: 0 additions & 1 deletion lib/wrapper/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ class WrapperBase extends Base {
* @class
*/
constructor(options) {
options = options || {};
super(options);
debug('new WrapperBase({ port: %j, isLeader: %j })', options.port, options.isLeader);
this[subInfo] = new Map();
Expand Down
20 changes: 18 additions & 2 deletions test/server.test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
'use strict';

const mm = require('mm');
const path = require('path');
const coffee = require('coffee');
Expand Down Expand Up @@ -31,4 +29,22 @@ describe('test/server.test.js', () => {
assert(server2);
await ClusterServer.close('previous-closed', server1);
});

it('should throw error when port is not a number', async function() {
await assert.rejects(async () => {
await ClusterServer.create('same-name', undefined);
}, /port should be a number, but got undefined/);
await assert.rejects(async () => {
await ClusterServer.create('same-name', null);
}, /port should be a number, but got null/);
await assert.rejects(async () => {
await ClusterServer.create('same-name');
}, /port should be a number, but got undefined/);
await assert.rejects(async () => {
await ClusterServer.create('same-name', 'foo');
}, /port should be a number, but got "foo"/);
await assert.rejects(async () => {
await ClusterServer.create('same-name', '0');
}, /port should be a number, but got "0"/);
});
});

0 comments on commit eb890c3

Please sign in to comment.