Skip to content

Commit c28314b

Browse files
committed
test: add code coverage
1 parent b02cc58 commit c28314b

File tree

6 files changed

+2483
-101
lines changed

6 files changed

+2483
-101
lines changed

.github/workflows/main.yml

+37-7
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,47 @@ jobs:
2222
strategy:
2323
fail-fast: false
2424
matrix:
25-
include:
26-
- NODE_VERSION: 12-bullseye
27-
- NODE_VERSION: 14-bullseye
28-
- NODE_VERSION: 16-bullseye
25+
node: [12.x, 14.x, 16.x]
26+
redis: [2, 6]
2927
steps:
30-
- uses: actions/checkout@v2
31-
- name: Build and test
32-
run: bash test/docker/main.sh ${{ matrix.NODE_VERSION }}
28+
- name: Git checkout
29+
uses: actions/checkout@v2
30+
31+
- name: Use Node.js ${{ matrix.node }}
32+
uses: actions/setup-node@v1
33+
with:
34+
node-version: ${{ matrix.node }}
35+
36+
- name: Start Redis
37+
uses: supercharge/[email protected]
38+
with:
39+
redis-version: ${{ matrix.redis }}
40+
41+
- run: npm install
42+
- run: npm run lint
43+
- run: npm run build
44+
- run: npm run test:tsd
45+
- run: npm run test:cov || npm run test:cov || npm run test:cov
46+
- name: Coveralls
47+
uses: coverallsapp/github-action@master
48+
with:
49+
github-token: ${{ secrets.GITHUB_TOKEN }}
50+
flag-name: redis-${{matrix.redis}}-node-${{matrix.node}}
51+
parallel: true
52+
3353
test-cluster:
3454
runs-on: ubuntu-latest
3555
steps:
3656
- uses: actions/checkout@v2
3757
- name: Build and test cluster
3858
run: bash test-cluster/docker/main.sh
59+
60+
code-coverage:
61+
needs: test
62+
runs-on: ubuntu-latest
63+
steps:
64+
- name: Coveralls
65+
uses: coverallsapp/github-action@master
66+
with:
67+
github-token: ${{ secrets.GITHUB_TOKEN }}
68+
parallel-finished: true

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ node_modules
44
/.idea
55
built
66

7+
.nyc_output
8+
coverage
9+
710
.vscode
811
benchmarks/fixtures/*.txt
912

README.md

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
[![ioredis](https://cdn.jsdelivr.net/gh/luin/ioredis@b5e8c74/logo.svg)](https://github.com/luin/ioredis)
22

3-
[![Build Status](https://github.com/luin/ioredis/actions/workflows/main.yml/badge.svg?branch=master)](https://github.com/luin/ioredis/actions/workflows/main.yml?query=branch%3Amaster)
4-
[![code style: prettier](https://img.shields.io/badge/code_style-prettier-ff69b4.svg?style=flat-square)](https://github.com/prettier/prettier)
3+
[![Build Status](https://github.com/luin/ioredis/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/luin/ioredis/actions/workflows/main.yml?query=branch%3Amain)
4+
[![Coverage Status](https://coveralls.io/repos/github/luin/ioredis/badge.svg?branch=main)](https://coveralls.io/github/luin/ioredis?branch=main)
55
[![Join the chat at https://gitter.im/luin/ioredis](https://badges.gitter.im/Join%20Chat.svg)](https://gitter.im/luin/ioredis?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge)
66
[![Commitizen friendly](https://img.shields.io/badge/commitizen-friendly-brightgreen.svg)](http://commitizen.github.io/cz-cli/)
77
[![semantic-release](https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg)](https://github.com/semantic-release/semantic-release)
8-
[![npm latest version](https://img.shields.io/npm/v/ioredis/latest.svg)](https://www.npmjs.com/package/ioredis)
9-
[![npm next version](https://img.shields.io/npm/v/ioredis/next.svg)](https://www.npmjs.com/package/ioredis)
10-
<img alt="" src="">
118

129
A robust, performance-focused and full-featured [Redis](http://redis.io) client for [Node.js](https://nodejs.org).
1310

lib/cluster/index.ts

+37-34
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@ const debug = Debug("cluster");
4242

4343
const REJECT_OVERWRITTEN_COMMANDS = new WeakSet<Command>();
4444

45+
type OfflineQueueItem = {
46+
command: Command;
47+
stream: WriteableStream;
48+
node: unknown;
49+
};
50+
4551
type ClusterStatus =
4652
| "end"
4753
| "close"
@@ -80,7 +86,7 @@ class Cluster extends Commander {
8086
private manuallyClosing: boolean;
8187
private retryAttempts = 0;
8288
private delayQueue: DelayQueue = new DelayQueue();
83-
private offlineQueue = new Deque();
89+
private offlineQueue = new Deque<OfflineQueueItem>();
8490
private subscriber: ClusterSubscriber;
8591
private slotsTimer: NodeJS.Timer;
8692
private reconnectTimeout: NodeJS.Timer;
@@ -204,13 +210,13 @@ class Cluster extends Commander {
204210
}
205211
this.connectionPool.reset(nodes);
206212

207-
function readyHandler() {
213+
const readyHandler = () => {
208214
this.setStatus("ready");
209215
this.retryAttempts = 0;
210216
this.executeOfflineCommands();
211217
this.resetNodesRefreshInterval();
212218
resolve();
213-
}
219+
};
214220

215221
let closeListener: () => void = undefined;
216222
const refreshListener = () => {
@@ -229,15 +235,15 @@ class Cluster extends Commander {
229235
this.disconnect(true);
230236
}
231237
} else {
232-
readyHandler.call(this);
238+
readyHandler();
233239
}
234240
});
235241
} else {
236-
readyHandler.call(this);
242+
readyHandler();
237243
}
238244
};
239245

240-
closeListener = function () {
246+
closeListener = () => {
241247
const error = new Error("None of startup nodes is available");
242248

243249
this.removeListener("refresh", refreshListener);
@@ -411,8 +417,8 @@ class Cluster extends Commander {
411417
this.isRefreshing = true;
412418

413419
const _this = this;
414-
const wrapper = function (error?: Error) {
415-
_this.isRefreshing = false;
420+
const wrapper = (error?: Error) => {
421+
this.isRefreshing = false;
416422
if (callback) {
417423
callback(error);
418424
}
@@ -729,7 +735,7 @@ class Cluster extends Commander {
729735
debug("closed because %s", reason);
730736
}
731737

732-
let retryDelay;
738+
let retryDelay: unknown;
733739
if (
734740
!this.manuallyClosing &&
735741
typeof this.options.clusterRetryStrategy === "function"
@@ -743,13 +749,13 @@ class Cluster extends Commander {
743749
if (typeof retryDelay === "number") {
744750
this.setStatus("reconnecting");
745751
this.reconnectTimeout = setTimeout(
746-
function () {
752+
() => {
747753
this.reconnectTimeout = null;
748754
debug("Cluster is disconnected. Retrying after %dms", retryDelay);
749755
this.connect().catch(function (err) {
750756
debug("Got error %s when reconnecting. Ignoring...", err);
751757
});
752-
}.bind(this),
758+
},
753759
retryDelay
754760
);
755761
} else {
@@ -762,7 +768,7 @@ class Cluster extends Commander {
762768
* Flush offline queue with error.
763769
*/
764770
private flushQueue(error: Error) {
765-
let item;
771+
let item: OfflineQueueItem;
766772
while ((item = this.offlineQueue.shift())) {
767773
item.command.reject(error);
768774
}
@@ -773,7 +779,7 @@ class Cluster extends Commander {
773779
debug("send %d commands in offline queue", this.offlineQueue.length);
774780
const offlineQueue = this.offlineQueue;
775781
this.resetOfflineQueue();
776-
let item;
782+
let item: OfflineQueueItem;
777783
while ((item = offlineQueue.shift())) {
778784
this.sendCommand(item.command, item.stream, item.node);
779785
}
@@ -899,7 +905,7 @@ class Cluster extends Commander {
899905
);
900906
}
901907

902-
private invokeReadyDelayedCallbacks(err) {
908+
private invokeReadyDelayedCallbacks(err?: Error) {
903909
for (const c of this._readyDelayedCallbacks) {
904910
process.nextTick(c, err);
905911
}
@@ -911,7 +917,7 @@ class Cluster extends Commander {
911917
* Check whether Cluster is able to process commands
912918
*/
913919
private readyCheck(callback: Callback<void | "fail">): void {
914-
this.cluster("INFO", function (err, res) {
920+
this.cluster("INFO", (err, res) => {
915921
if (err) {
916922
return callback(err);
917923
}
@@ -1003,38 +1009,35 @@ class Cluster extends Commander {
10031009
* This process happens every time when #connect() is called since
10041010
* #startupNodes and DNS records may chanage.
10051011
*/
1006-
private resolveStartupNodeHostnames(): Promise<RedisOptions[]> {
1012+
private async resolveStartupNodeHostnames(): Promise<RedisOptions[]> {
10071013
if (!Array.isArray(this.startupNodes) || this.startupNodes.length === 0) {
1008-
return Promise.reject(
1009-
new Error("`startupNodes` should contain at least one node.")
1010-
);
1014+
throw new Error("`startupNodes` should contain at least one node.");
10111015
}
10121016
const startupNodes = normalizeNodeOptions(this.startupNodes);
10131017

10141018
const hostnames = getUniqueHostnamesFromOptions(startupNodes);
10151019
if (hostnames.length === 0) {
1016-
return Promise.resolve(startupNodes);
1020+
return startupNodes;
10171021
}
10181022

1019-
return Promise.all(
1023+
const configs = await Promise.all(
10201024
hostnames.map(
10211025
(this.options.useSRVRecords ? this.resolveSrv : this.dnsLookup).bind(
10221026
this
10231027
)
10241028
)
1025-
).then((configs) => {
1026-
const hostnameToConfig = zipMap(hostnames, configs);
1027-
1028-
return startupNodes.map((node) => {
1029-
const config = hostnameToConfig.get(node.host);
1030-
if (!config) {
1031-
return node;
1032-
} else if (this.options.useSRVRecords) {
1033-
return Object.assign({}, node, config);
1034-
} else {
1035-
return Object.assign({}, node, { host: config });
1036-
}
1037-
});
1029+
);
1030+
const hostnameToConfig = zipMap(hostnames, configs);
1031+
1032+
return startupNodes.map((node) => {
1033+
const config = hostnameToConfig.get(node.host);
1034+
if (!config) {
1035+
return node;
1036+
}
1037+
if (this.options.useSRVRecords) {
1038+
return Object.assign({}, node, config);
1039+
}
1040+
return Object.assign({}, node, { host: config });
10381041
});
10391042
}
10401043

0 commit comments

Comments
 (0)