Skip to content

Commit

Permalink
fix: should not emit timeout when has cache (#58)
Browse files Browse the repository at this point in the history
  • Loading branch information
killagu committed Nov 23, 2022
1 parent 180ffbe commit 9d9b965
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 8 deletions.
8 changes: 4 additions & 4 deletions .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,25 +22,25 @@ jobs:
strategy:
fail-fast: false
matrix:
node-version: [8, 10, 12, 14, 16]
node-version: [14, 16, 18]
os: [ubuntu-latest, windows-latest, macos-latest]

steps:
- name: Checkout Git Source
uses: actions/checkout@v2

- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
uses: actions/setup-node@v3
with:
node-version: ${{ matrix.node-version }}

- name: Install Dependencies
run: npm i -g npminstall@5 && npminstall
run: npm i

- name: Continuous Integration
run: npm run ci

- name: Code Coverage
uses: codecov/codecov-action@v1
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
2 changes: 1 addition & 1 deletion lib/follower.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Follower extends Base {
subscribe(reg, listener) {
const key = this.formatKey(reg);
this.on(key, listener);
if (this.options.subscribeTimeout) {
if (this.options.subscribeTimeout && !this._subData.has(key)) {
this._listenSubscribeTimeout(key);
}

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
"contributors": "^0.5.1",
"detect-port": "^1.3.0",
"egg-bin": "^4.11.0",
"egg-ci": "^1.8.0",
"egg-ci": "^2.2.0",
"egg-mock": "^3.21.0",
"eslint": "^5.14.1",
"eslint-config-egg": "^7.1.0",
Expand All @@ -64,9 +64,9 @@
"webstorm-disable-index": "^1.2.0"
},
"engines": {
"node": ">=8.0.0"
"node": ">=14.0.0"
},
"ci": {
"version": "8, 10, 12, 14, 16"
"version": "14, 16, 18"
}
}
41 changes: 41 additions & 0 deletions test/subscribe.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,5 +44,46 @@ describe('test/subscrib.test.js', () => {
await follower.close();
await leader.close();
});

it('should not timeout when already have data', async () => {
const leader = new ApiClient({
singleMode: false,
isLeader: true,
});
const follower = new ApiClient({
clusterOptions: {
subscribeTimeout: 1000,
},
singleMode: false,
isLeader: false,
});
await follower.ready();
const errors = [];
const values = [];
follower.on('error', err => {
errors.push(err);
});
// ensure has data
follower.subscribe({
key: 'timeout:500',
}, value => {
values.push(value);
});
await sleep(1000);
// second subscribe
follower.subscribe({
key: 'timeout:600',
}, value => {
values.push(value);
});
await sleep(1000);
assert.deepStrictEqual(values, [
'hello:500',
'hello:600',
]);
assert(errors.length === 0);
await follower.close();
await leader.close();
});
});
});

0 comments on commit 9d9b965

Please sign in to comment.