Skip to content

Commit 6f94410

Browse files
authored
feat: add error empty fns (#2)
1 parent 773f9d0 commit 6f94410

File tree

8 files changed

+117
-8
lines changed

8 files changed

+117
-8
lines changed

.eslintrc.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,6 @@
22
"extends": "./node_modules/mwts/",
33
"ignorePatterns": ["node_modules", "dist", "fixtures"],
44
"env": {
5-
"mocha": true
5+
"jest": true
66
}
77
}

.github/workflows/nodejs.yml

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: Node.js CI
2+
3+
on: [push, pull_request]
4+
5+
jobs:
6+
build:
7+
8+
runs-on: ubuntu-latest
9+
10+
strategy:
11+
matrix:
12+
node-version: [10.x, 12.x, 14.x, 16.x]
13+
14+
steps:
15+
- name: Git checkout
16+
uses: actions/checkout@v2
17+
18+
- name: Use Node.js ${{ matrix.node-version }}
19+
uses: actions/setup-node@v2
20+
with:
21+
node-version: ${{ matrix.node-version }}
22+
23+
- run: npm install && npm install codecov
24+
- run: npm run build --if-present
25+
- run: npm run lint
26+
- run: npm run cov
27+
- name: Upload coverage to Codecov
28+
uses: codecov/codecov-action@v2
29+
30+
31+
# build-windows:
32+
# runs-on: windows-latest
33+
34+
# strategy:
35+
# matrix:
36+
# node-version: [12.x, 14.x]
37+
38+
# steps:
39+
# - uses: actions/checkout@v2
40+
# - name: Use Node.js ${{ matrix.node-version }}
41+
# uses: actions/setup-node@v1
42+
# with:
43+
# node-version: ${{ matrix.node-version }}
44+
# - run: npm install
45+
# - run: npm run build --if-present
46+
# - run: npm run cov

jest.config.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
module.exports = {
2+
preset: 'ts-jest',
3+
testEnvironment: 'node',
4+
testPathIgnorePatterns: ['<rootDir>/test/fixtures'],
5+
coveragePathIgnorePatterns: ['<rootDir>/test/'],
6+
setupFilesAfterEnv: ['./jest.setup.js'],
7+
};

jest.setup.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
process.env.MIDWAY_TS_MODE = 'true';
2+
jest.setTimeout(120000);

package.json

+11-7
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,25 @@
44
"main": "dist/index",
55
"typings": "dist/index.d.ts",
66
"files": [
7-
"dist"
7+
"dist/**/*.js",
8+
"dist/**/*.d.ts"
89
],
910
"devDependencies": {
10-
"@midwayjs/cli": "^1.0.0",
11+
"@types/jest": "^27.4.1",
12+
"@types/node": "^16.11.26",
13+
"ts-node": "^10.5.0",
1114
"mwts": "^1.0.5",
12-
"typescript": "^3.9.0"
15+
"typescript": "^4.0.0",
16+
"jest": "^27.5.1",
17+
"ts-jest": "^27.1.3"
1318
},
1419
"engines": {
1520
"node": ">= 10"
1621
},
1722
"scripts": {
18-
"build": "midway-bin build -c && rm -rf ./dist/.mwcc-cache",
19-
"test": "midway-bin test --ts --full-trace",
20-
"cov": "midway-bin cov --ts",
21-
"clean": "midway-bin clean",
23+
"build": "rm -rf dist && tsc",
24+
"test": "node --require=ts-node/register ./node_modules/.bin/jest",
25+
"cov": "node --require=ts-node/register ./node_modules/.bin/jest --coverage --forceExit",
2226
"lint": "mwts check",
2327
"lint:fix": "mwts fix"
2428
},

src/index.ts

+8
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export default class SimpleLock {
6464
}
6565
})
6666
.catch(err => {
67+
if (this.q.get(localKey).fns.length > 0) {
68+
this.q.get(localKey).fns = [];
69+
}
70+
6771
deferredReject(err);
6872
});
6973

@@ -113,6 +117,10 @@ export default class SimpleLock {
113117
this.q.get(key).resolved = true;
114118
})
115119
.catch(err => {
120+
if (this.q.get(key).fns.length > 0) {
121+
this.q.get(key).fns = [];
122+
}
123+
116124
deferredReject(err);
117125

118126
this.q.get(key).resolved = true;

test/index.test.ts

+41
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,29 @@ describe('simple lock', () => {
3030
assert(i === 1);
3131
});
3232

33+
it('sureOnce throw error should be ok', async () => {
34+
const lock = new SimpleLock();
35+
36+
let ee;
37+
try {
38+
await lock.sureOnce(async () => {
39+
(lock as any).q
40+
.get('_default_simple_lock_queue')
41+
.fns.push(() => console.log('test'));
42+
43+
throw new Error('11');
44+
});
45+
} catch (e) {
46+
ee = e.message;
47+
}
48+
49+
assert.deepEqual(ee, '11');
50+
assert.deepEqual(
51+
(lock as any).q.get('_default_simple_lock_queue').fns.length,
52+
0
53+
);
54+
});
55+
3356
it('acquire should be ok', async () => {
3457
const lock = new SimpleLock();
3558
let i = 0;
@@ -55,4 +78,22 @@ describe('simple lock', () => {
5578
assert(3 === i);
5679
assert.deepEqual([11, 2, 3], data);
5780
});
81+
82+
it('acquire throw error should be ok', async () => {
83+
const lock = new SimpleLock();
84+
85+
let ee;
86+
try {
87+
await lock.acquire('hello', async () => {
88+
(lock as any).q.get('hello').fns.push(() => console.log('test'));
89+
90+
throw new Error('11');
91+
});
92+
} catch (e) {
93+
ee = e.message;
94+
}
95+
96+
assert.deepEqual(ee, '11');
97+
assert.deepEqual((lock as any).q.get('hello').fns.length, 0);
98+
});
5899
});

tsconfig.json

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
// TODO: Emit test files to outDir
1515
"noEmitOnError": false,
1616
"skipLibCheck": true,
17+
"outDir": "dist"
1718
},
1819
"exclude": [
1920
"dist",

0 commit comments

Comments
 (0)