Skip to content

Commit

Permalink
test: improve coverage to 100% (eggjs#333)
Browse files Browse the repository at this point in the history
* test: remove all skip tests
  • Loading branch information
fengmk2 authored Feb 8, 2017
1 parent 2c4919f commit 61f8907
Show file tree
Hide file tree
Showing 32 changed files with 367 additions and 264 deletions.
2 changes: 0 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ Please let us know what we can help, check [issues](https://github.com/eggjs/egg

If you are a contributor, follow [CONTRIBUTING](CONTRIBUTING.md).

If you are a member of egg, follow [MEMBER_GUIDE](MEMBER_GUIDE.md).

## License

[MIT](LICENSE)
2 changes: 0 additions & 2 deletions README.zh-CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ Please let us know what we can help, check [issues](https://github.com/eggjs/egg

If you are a contributor, follow [CONTRIBUTING](CONTRIBUTING.md).

If you are a member of egg, follow [MEMBER_GUIDE](MEMBER_GUIDE.md).

## 开源协议

[MIT](LICENSE)
3 changes: 2 additions & 1 deletion app/extend/context.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ const proto = module.exports = {
* @see Context#locals
*/
get state() {
return this.locals || {};
return this.locals;
},

set state(val) {
Expand All @@ -245,6 +245,7 @@ const proto = module.exports = {
runInBackground(scope) {
const ctx = this;
const start = Date.now();
/* istanbul ignore next */
const taskName = scope.name || '-';
co(function* () {
yield scope(ctx);
Expand Down
3 changes: 2 additions & 1 deletion app/extend/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ module.exports = {
* proxy is enabled.
* @member {String} Request#host
* @example
* ip + port
* ```js
* this.request.host
* => '127.0.0.1:7001'
* ```
* 如果是域名访问,会得到域名
* or domain
* ```js
* this.request.host
* => 'demo.eggjs.org'
Expand Down
2 changes: 1 addition & 1 deletion app/extend/response.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ module.exports = {
set type(type) {
// copy from koa
// change header name to lower case
type = getType(type) || false;
type = getType(type);
if (type) {
this.set('content-type', type);
} else {
Expand Down
1 change: 1 addition & 0 deletions app/middleware/site_file.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ const MAX_AGE = 'public, max-age=2592000'; // 30 days
module.exports = options => {
return function* siteFile(next) {
if (this.method !== 'HEAD' && this.method !== 'GET') return yield next;
/* istanbul ignore if */
if (this.path[0] !== '/') return yield next;

const content = options[this.path];
Expand Down
19 changes: 3 additions & 16 deletions lib/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ const path = require('path');
const EggApplication = require('./egg');
const AgentWorkerLoader = require('./loader').AgentWorkerLoader;

const AGENT_CLIENTS = Symbol('Agent#agentClients');
const EGG_LOADER = Symbol.for('egg#loader');
const EGG_PATH = Symbol.for('egg#eggPath');

Expand All @@ -13,42 +12,30 @@ const EGG_PATH = Symbol.for('egg#eggPath');
* @extends EggApplication
*/
class Agent extends EggApplication {

/**
* @constructor
* @param {Object} options - see {@link EggApplication}
*/
constructor(options) {
options = options || {};
constructor(options = {}) {
options.type = 'agent';
super(options);

this._wrapMessenger();

this.loader.load();

// 不让 agent 退出
// keep agent alive even it don't have any io tasks
setInterval(() => {}, 24 * 60 * 60 * 1000);

this._uncaughtExceptionHandler = this._uncaughtExceptionHandler.bind(this);
process.on('uncaughtException', this._uncaughtExceptionHandler);
}

/**
* 当前进程实例化的 AgentWorkerClient 集合,只在 mm.app 场景下才有用
* @private
*/
get agentWorkerClients() {
if (!this[AGENT_CLIENTS]) {
this[AGENT_CLIENTS] = new Map();
}
return this[AGENT_CLIENTS];
}

_uncaughtExceptionHandler(err) {
if (!(err instanceof Error)) {
err = new Error(String(err));
}
/* istanbul ignore else */
if (err.name === 'Error') {
err.name = 'unhandledExceptionError';
}
Expand Down
10 changes: 2 additions & 8 deletions lib/application.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,3 @@
/**
* 对 koa application 的所有扩展,都放在此文件统一维护。
*
* - koa application: https://github.com/koajs/koa/blob/master/lib/application.js
*/

'use strict';

const path = require('path');
Expand All @@ -30,8 +24,7 @@ class Application extends EggApplication {
* @constructor
* @param {Object} options - see {@link EggApplication}
*/
constructor(options) {
options = options || {};
constructor(options = {}) {
options.type = 'application';
super(options);
this.loader.load();
Expand All @@ -47,6 +40,7 @@ class Application extends EggApplication {
}

onServer(server) {
/* istanbul ignore next */
graceful({
server: [ server ],
error: (err, throwErrorCount) => {
Expand Down
4 changes: 2 additions & 2 deletions lib/cluster/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
const path = require('path');
const startCluster = require('egg-cluster').startCluster;

module.exports = (options, callback) => {
options = options || {};
module.exports = (options = {}, callback) => {
/* istanbul ignore next */
options.customEgg = options.customEgg || path.join(__dirname, '../..');
startCluster(options, callback);
};
1 change: 1 addition & 0 deletions lib/core/messenger.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ class Messenger extends EventEmitter {
* @return {Messenger} this
*/
sendRandom(action, data) {
/* istanbul ignore if */
if (!this.opids.length) return this;
const pid = random(this.opids);
this.sendTo(String(pid), action, data);
Expand Down
9 changes: 4 additions & 5 deletions lib/core/singleton.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
const assert = require('assert');

class Singleton {
constructor(options) {
options = options || {};
constructor(options = {}) {
assert(options.name, '[egg:singleton] Singleton#constructor options.name is required');
assert(options.app, '[egg:singleton] Singleton#constructor options.app is required');
assert(options.create, '[egg:singleton] Singleton#constructor options.create is required');
Expand All @@ -13,14 +12,14 @@ class Singleton {
this.app = options.app;
this.name = options.name;
this.create = options.create;
/* istanbul ignore next */
this.options = options.app.config[this.name] || {};
}

init() {
const options = this.options;
if (options.client && options.clients) {
throw new Error(`eggg:singleton ${this.name} can not set options.client and options.clients both`);
}
assert(!(options.client && options.clients),
`eggg:singleton ${this.name} can not set options.client and options.clients both`);

// alias app[name] as client, but still support createInstance method
if (options.client) {
Expand Down
8 changes: 6 additions & 2 deletions lib/egg.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ class EggApplication extends EggCore {

function delegate(res, app, keys) {
for (const key of keys) {
/* istanbul ignore else */
if (app[key]) {
res[key] = app[key];
}
Expand All @@ -97,6 +98,7 @@ class EggApplication extends EggCore {

function abbr(res, app, keys) {
for (const key of keys) {
/* istanbul ignore else */
if (app[key]) {
res[key] = `<egg ${key}>`;
}
Expand Down Expand Up @@ -217,6 +219,7 @@ class EggApplication extends EggCore {
if (!(err instanceof Error)) {
err = new Error(String(err));
}
/* istanbul ignore else */
if (err.name === 'Error') {
err.name = 'unhandledRejectionError';
}
Expand All @@ -231,13 +234,14 @@ class EggApplication extends EggCore {
const rundir = this.config.rundir;
const configdir = path.join(rundir, `${this.type}_config.json`);
try {
/* istanbul ignore if */
if (!fs.existsSync(rundir)) fs.mkdirSync(rundir);
fs.writeFileSync(configdir, JSON.stringify({
config: this.config,
plugins: this.plugins,
}, null, 2));
} catch (err) {
this.logger.warn(`dumpConfig error: ${err.message}`);
this.coreLogger.warn(`dumpConfig error: ${err.message}`);
}
}

Expand All @@ -247,7 +251,7 @@ class EggApplication extends EggCore {

_setupTimeoutTimer() {
const startTimeoutTimer = setTimeout(() => {
this.logger.error(`${this.type} still doesn't ready after ${this.config.workerStartTimeout} ms.`);
this.coreLogger.error(`${this.type} still doesn't ready after ${this.config.workerStartTimeout} ms.`);
this.emit('startTimeout');
}, this.config.workerStartTimeout);
this.ready(() => clearTimeout(startTimeoutTimer));
Expand Down
Loading

0 comments on commit 61f8907

Please sign in to comment.