diff --git a/lib/utils/options.js b/lib/utils/options.js index 44d57e6..22444b8 100644 --- a/lib/utils/options.js +++ b/lib/utils/options.js @@ -65,10 +65,18 @@ module.exports = function(options) { const isDebug = process.execArgv.some(argv => argv.includes('--debug') || argv.includes('--inspect')); if (isDebug) options.isDebug = isDebug; - /* istanbul ignore next */ - if (options.serialization && !semver.gte(process.version, '12.16.0')) { - const err = new Error('[master] agent_worker options.serialization requires Node.js >= v12.16.0'); - this.logger.error(err); + if (semver.gte(process.version, '12.16.0')) { + /* istanbul ignore else */ + if (typeof options.serialization !== 'undefined' + && options.serialization !== 'json' + && options.serialization !== 'advanced') { + throw new Error(`[egg-cluster] options.serialization must be "json" or "advanced", value is ${options.serialization}`); + } + } else { + /* istanbul ignore else */ + if (options.serialization) { + throw new Error('[egg-cluster] options.serialization requires Node.js >= v12.16.0'); + } } return options; diff --git a/test/options.test.js b/test/options.test.js index 9b6f578..ce842d8 100644 --- a/test/options.test.js +++ b/test/options.test.js @@ -4,6 +4,7 @@ const path = require('path'); const assert = require('assert'); const os = require('os'); const mm = require('egg-mock'); +const semver = require('semver'); const parseOptions = require('../lib/utils/options'); const utils = require('./utils'); @@ -202,4 +203,64 @@ describe('test/options.test.js', () => { } }); }); + + describe('should options.serialization', () => { + it('should with "json" value when Node.js >= v12.16.0', () => { + if (semver.gte(process.version, '12.16.0')) { + const options = parseOptions({ + serialization: 'json', + }); + assert(options.serialization === 'json'); + } + }); + + it('should with "advanced" value when Node.js >= v12.16.0', () => { + if (semver.gte(process.version, '12.16.0')) { + const options = parseOptions({ + serialization: 'advanced', + }); + assert(options.serialization === 'advanced'); + } + }); + + it('should error with invalid value when Node.js >= v12.16.0', () => { + if (semver.gte(process.version, '12.16.0')) { + try { + parseOptions({ + serialization: 'fake', + }); + assert(false); + } catch (ex) { + assert(ex.message.includes('must be "json" or "advanced"')); + } + } + }); + + it('should error with empty value when Node.js >= v12.16.0', () => { + if (semver.gte(process.version, '12.16.0')) { + try { + parseOptions({ + serialization: '', + }); + assert(false); + } catch (ex) { + assert(ex.message.includes('must be "json" or "advanced"')); + } + } + }); + + it('should error with value when Node.js < v12.16.0', () => { + if (!semver.gte(process.version, '12.16.0')) { + try { + parseOptions({ + serialization: 'json', + }); + assert(false); + } catch (ex) { + assert(ex.message.includes('requires Node.js >= v12.16.0')); + } + } + }); + }); + });