Skip to content

Commit

Permalink
fix tests on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Jan 1, 2025
1 parent 6adebea commit 16553a1
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 43 deletions.
2 changes: 1 addition & 1 deletion test/lib/application.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ describe('test/lib/application.test.ts', () => {
new Application({
baseDir: getFilepath('custom-egg/index.js'),
});
}, /not a directory/);
}, /not a directory|no such file or directory/);
});
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
const assert = require('assert');
const mm = require('egg-mock');
const utils = require('../../../utils');
const Messenger = require('../../../../lib/core/messenger/ipc');
import { strict as assert } from 'node:assert';
import { scheduler } from 'node:timers/promises';
import { mm } from '@eggjs/mock';
import { cluster, MockApplication } from '../../../utils.js';
import { Messenger } from '../../../../src/lib/core/messenger/ipc.js';

describe('test/lib/core/messenger/ipc.test.js', () => {
let messenger;
describe('test/lib/core/messenger/ipc.test.ts', () => {
let messenger: Messenger;
const app: any = {};

before(() => {
messenger = new Messenger();
messenger = new Messenger(app);
});

afterEach(mm.restore);
Expand All @@ -21,20 +23,20 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
done();
});

process.emit('message', {});
process.emit('message', null);
process.emit('message', {}, null);
process.emit('message', null, null);
process.emit('message', {
action: 'messenger-test-on-event',
data: {
success: true,
},
});
}, null);
});
});

describe('close()', () => {
it('should remove all listeners', () => {
const messenger = new Messenger();
const messenger = new Messenger(app);
messenger.on('messenger-test-on-event-2', () => {
throw new Error('should never emitted');
});
Expand All @@ -46,17 +48,17 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
data: {
success: true,
},
});
}, null);
});
});

describe('cluster messenger', () => {
let app;
let app: MockApplication;
after(() => app.close());

// use it to record create coverage codes time
it('before: should start cluster app', async () => {
app = utils.cluster('apps/messenger');
app = cluster('apps/messenger');
app.coverage(true);
await app.ready();
await scheduler.wait(1000);
Expand All @@ -66,7 +68,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
app.expect('stdout', /\[app] agent-to-app agent msg/);
});

it('app should accept agent assgin pid message', () => {
it('app should accept agent assign pid message', () => {
app.expect('stdout', /\[app] agent-to-app agent msg \d+/);
});

Expand All @@ -84,14 +86,14 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
});

describe('broadcast()', () => {
let app;
let app: MockApplication;
before(() => {
mm.env('default');
app = utils.cluster('apps/messenger-broadcast', { workers: 2 });
app = cluster('apps/messenger-broadcast', { workers: 2 });
app.coverage(false);
return app.ready();
});
before(() => utils.sleep(1000));
before(() => scheduler.wait(1000));
after(() => app.close());

it('should broadcast each other', () => {
Expand All @@ -105,15 +107,15 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
// agent 26494 receive message from app pid 26496
// agent 26494 receive message from agent pid 26494
const m = app.stdout.match(/(app|agent) \d+ receive message from (app|agent) pid \d+/g);
assert(m.length, 9);
assert.equal(m.length, 9);
});
});

describe('sendRandom', () => {
let app;
let app: MockApplication;
before(() => {
mm.env('default');
app = utils.cluster('apps/messenger-random', { workers: 4 });
app = cluster('apps/messenger-random', { workers: 4 });
app.coverage(false);
return app.ready();
});
Expand All @@ -137,10 +139,10 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
});

describe('sendToApp and sentToAgent', () => {
let app;
let app: MockApplication;
before(() => {
mm.env('default');
app = utils.cluster('apps/messenger-app-agent', { workers: 2 });
app = cluster('apps/messenger-app-agent', { workers: 2 });
app.coverage(false);
return app.ready();
});
Expand All @@ -155,7 +157,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
done();
}, 500);

function count(data, key) {
function count(data: string, key: string) {
return data.split('\n').filter(line => {
return line.indexOf(key) >= 0;
}).length;
Expand All @@ -164,10 +166,10 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
});

describe('worker_threads mode', () => {
let app;
let app: MockApplication;
before(() => {
mm.env('default');
app = utils.cluster('apps/messenger-app-agent', { workers: 1, startMode: 'worker_threads' });
app = cluster('apps/messenger-app-agent', { workers: 1, startMode: 'worker_threads' });
app.coverage(false);
return app.ready();
});
Expand All @@ -182,7 +184,7 @@ describe('test/lib/core/messenger/ipc.test.js', () => {
done();
}, 500);

function count(data, key) {
function count(data: string, key: string) {
return data.split('\n').filter(line => {
return line.indexOf(key) >= 0;
}).length;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
'use strict';
import { strict as assert } from 'node:assert';
import { scheduler } from 'node:timers/promises';

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 20)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 18.19.0)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 18.19.0)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (macos-latest, 22)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 20)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (ubuntu-latest, 22)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 18.19.0)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 20)

'scheduler' is defined but never used

Check failure on line 2 in test/lib/core/messenger/local.test.ts

View workflow job for this annotation

GitHub Actions / Node.js / Test (windows-latest, 22)

'scheduler' is defined but never used
import { mm } from '@eggjs/mock';
import { pending } from 'pedding';
import { singleProcessApp, MockApplication } from '../../../utils.js';

const utils = require('../../../utils');
const pedding = require('pedding');
const assert = require('assert');
const mm = require('egg-mock');

describe('test/lib/core/messenger/local.test.js', () => {
let app;
describe('test/lib/core/messenger/local.test.ts', () => {
let app: MockApplication;

before(async () => {
app = await utils.singleProcessApp('apps/demo');
app = await singleProcessApp('apps/demo');
});

after(() => app.close());
Expand All @@ -22,7 +21,7 @@ describe('test/lib/core/messenger/local.test.js', () => {

describe('broadcast()', () => {
it('app.messenger.broadcast should work', done => {
done = pedding(done, 2);
done = pending(2, done);
app.messenger.once('broadcast-event', msg => {
assert.deepEqual(msg, { foo: 'bar' });
done();
Expand All @@ -36,7 +35,7 @@ describe('test/lib/core/messenger/local.test.js', () => {
});

it('agent.messenger.broadcast should work', done => {
done = pedding(done, 2);
done = pending(2, done);
app.messenger.once('broadcast-event', msg => {
assert.deepEqual(msg, { foo: 'bar' });
done();
Expand Down Expand Up @@ -136,7 +135,7 @@ describe('test/lib/core/messenger/local.test.js', () => {

describe('sendTo(pid)', () => {
it('app.messenger.sendTo should work', done => {
done = pedding(done, 2);
done = pending(2, done);
app.messenger.once('sendTo-event', msg => {
assert.deepEqual(msg, { foo: 'bar' });
done();
Expand Down
2 changes: 2 additions & 0 deletions test/lib/egg.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ describe('test/lib/egg.test.ts', () => {
});

it('should dumpTiming when timeout', async () => {
if (process.platform === 'win32') return;
const baseDir = getFilepath('apps/dumptiming-timeout');
fs.rmSync(path.join(baseDir, 'run'), { recursive: true, force: true });
fs.rmSync(path.join(baseDir, 'logs'), { recursive: true, force: true });
Expand All @@ -193,6 +194,7 @@ describe('test/lib/egg.test.ts', () => {
});

it('should dump slow-boot-action warnning log', async () => {
if (process.platform === 'win32') return;
const baseDir = getFilepath('apps/dumptiming-slowBootActionMinDuration');
fs.rmSync(path.join(baseDir, 'run'), { recursive: true, force: true });
fs.rmSync(path.join(baseDir, 'logs'), { recursive: true, force: true });
Expand Down
6 changes: 3 additions & 3 deletions test/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { fileURLToPath } from 'node:url';
import { AddressInfo } from 'node:net';
import { scheduler } from 'node:timers/promises';
import {
mm, MockOptions, MockApplication,
mm, MockOptions, MockClusterOptions, MockApplication,
} from '@eggjs/mock';
import { Application as Koa } from '@eggjs/koa';
import { request } from '@eggjs/supertest';
Expand All @@ -23,7 +23,7 @@ export async function rimraf(target: string) {
await rm(target, { force: true, recursive: true });
}

export { MockApplication, MockOptions, mm } from '@eggjs/mock';
export { MockApplication, MockOptions, MockClusterOptions, mm };
export const restore = () => mm.restore();

export function app(name: string | MockOptions, options?: MockOptions) {
Expand All @@ -42,7 +42,7 @@ export const createApp = app;
* @param {Object} [options] - optional
* @return {App} app - Application object.
*/
export function cluster(name: string | MockOptions, options?: MockOptions): MockApplication {
export function cluster(name: string | MockClusterOptions, options?: MockClusterOptions): MockApplication {
options = formatOptions(name, options);
return mm.cluster(options) as unknown as MockApplication;
}
Expand Down

0 comments on commit 16553a1

Please sign in to comment.