Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(test): easy way to get cluster stuck #741

Open
wants to merge 2 commits into
base: v4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 34 additions & 2 deletions lib/cluster/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ const debug = require('../utils/debug')('ioredis:cluster')
const Commander = require('../commander')

type ClusterStatus = 'end' | 'close' | 'wait' | 'connecting' | 'connect' | 'ready' | 'reconnecting' | 'disconnecting'
type Deferred = Promise<void> & {
resolve?(): void
reject?(): void
}

/**
* Client for the official Redis Cluster
Expand All @@ -42,6 +46,7 @@ class Cluster extends EventEmitter {
private reconnectTimeout: NodeJS.Timer
private status: ClusterStatus
private isRefreshing: boolean = false
private connectionPromise?: Deferred

/**
* Every time Cluster#connect() is called, this value will be
Expand Down Expand Up @@ -122,9 +127,15 @@ class Cluster extends EventEmitter {
* @returns {Promise<void>}
* @memberof Cluster
*/
public connect(): Promise<void> {
public connect(): Deferred {
const Promise = PromiseContainer.get()
return new Promise((resolve, reject) => {

let deferredResolve;
let deferredReject;
const promise: Deferred = new Promise((resolve, reject) => {
deferredResolve = resolve
deferredReject = reject

if (this.status === 'connecting' || this.status === 'connect' || this.status === 'ready') {
reject(new Error('Redis is already connecting/connected'))
return
Expand Down Expand Up @@ -192,6 +203,27 @@ class Cluster extends EventEmitter {
this.subscriber.start()
}).catch(reject)
})

// populate them
promise.resolve = deferredResolve
promise.reject = deferredReject

// if connection promise already exists
if (this.connectionPromise) {
promise.then(this.connectionPromise.resolve, this.connectionPromise.reject)
this.connectionPromise = null
}

promise.catch(noop).then(() => {
promise.reject = null
promise.resolve = null
if (this.connectionPromise === promise) {
this.connectionPromise = undefined
}
})

this.connectionPromise = promise
return promise
}

/**
Expand Down
15 changes: 15 additions & 0 deletions test/functional/cluster/connect.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,14 +77,29 @@ describe('cluster:connect', function () {
var slotTable = [
[0, 16383, ['127.0.0.1', 30001]]
];

var slots = 0;
var state = 0;

var argvHandler = function (argv) {
if (argv[0] === 'info') {
// return 'role:master'
}

if (argv[0] === 'cluster' && argv[1] === 'slots') {
if (slots < 3) {
slots += 1;
return [];
}

return slotTable;
}

if (argv[0] === 'cluster' && argv[1] === 'info') {
if (state < 3) {
state += 1;
return 'cluster_state:fail';
}
return 'cluster_state:ok';
}
};
Expand Down