You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-10 11:43:01 +03:00
fix client.quit and client.disconnect
This commit is contained in:
@@ -2,7 +2,7 @@ import { strict as assert, AssertionError } from 'assert';
|
|||||||
import { once } from 'events';
|
import { once } from 'events';
|
||||||
import { itWithClient, TEST_REDIS_SERVERS, TestRedisServers, waitTillBeenCalled, isRedisVersionGreaterThan } from '../test-utils';
|
import { itWithClient, TEST_REDIS_SERVERS, TestRedisServers, waitTillBeenCalled, isRedisVersionGreaterThan } from '../test-utils';
|
||||||
import RedisClient from '.';
|
import RedisClient from '.';
|
||||||
import { AbortError, ClientClosedError, ConnectionTimeoutError, WatchError } from '../errors';
|
import { AbortError, ClientClosedError, ConnectionTimeoutError, DisconnectsClientError, WatchError } from '../errors';
|
||||||
import { defineScript } from '../lua-script';
|
import { defineScript } from '../lua-script';
|
||||||
import { spy } from 'sinon';
|
import { spy } from 'sinon';
|
||||||
import { RedisNetSocketOptions } from '../client/socket';
|
import { RedisNetSocketOptions } from '../client/socket';
|
||||||
@@ -636,10 +636,36 @@ describe('Client', () => {
|
|||||||
await client.connect();
|
await client.connect();
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const quitPromise = client.quit();
|
const pingPromise = client.ping(),
|
||||||
|
quitPromise = client.quit();
|
||||||
|
assert.equal(client.isOpen, false);
|
||||||
|
|
||||||
|
const [ping] = await Promise.all([
|
||||||
|
pingPromise,
|
||||||
|
assert.doesNotReject(quitPromise),
|
||||||
|
assert.rejects(client.ping(), ClientClosedError)
|
||||||
|
]);
|
||||||
|
|
||||||
|
assert.equal(ping, 'PONG');
|
||||||
|
} finally {
|
||||||
|
if (client.isOpen) {
|
||||||
|
await client.disconnect();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
it('client.disconnect', async () => {
|
||||||
|
const client = RedisClient.create(TEST_REDIS_SERVERS[TestRedisServers.OPEN]);
|
||||||
|
|
||||||
|
await client.connect();
|
||||||
|
|
||||||
|
try {
|
||||||
|
const pingPromise = client.ping(),
|
||||||
|
disconnectPromise = client.disconnect();
|
||||||
assert.equal(client.isOpen, false);
|
assert.equal(client.isOpen, false);
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
quitPromise,
|
assert.rejects(pingPromise, DisconnectsClientError),
|
||||||
|
assert.doesNotReject(disconnectPromise),
|
||||||
assert.rejects(client.ping(), ClientClosedError)
|
assert.rejects(client.ping(), ClientClosedError)
|
||||||
]);
|
]);
|
||||||
} finally {
|
} finally {
|
||||||
|
@@ -11,7 +11,7 @@ import { ScanCommandOptions } from '../commands/SCAN';
|
|||||||
import { HScanTuple } from '../commands/HSCAN';
|
import { HScanTuple } from '../commands/HSCAN';
|
||||||
import { encodeCommand, extendWithCommands, extendWithModulesAndScripts, transformCommandArguments, transformCommandReply } from '../commander';
|
import { encodeCommand, extendWithCommands, extendWithModulesAndScripts, transformCommandArguments, transformCommandReply } from '../commander';
|
||||||
import { Pool, Options as PoolOptions, createPool } from 'generic-pool';
|
import { Pool, Options as PoolOptions, createPool } from 'generic-pool';
|
||||||
import { ClientClosedError } from '../errors';
|
import { ClientClosedError, DisconnectsClientError } from '../errors';
|
||||||
import { URL } from 'url';
|
import { URL } from 'url';
|
||||||
|
|
||||||
export interface RedisClientOptions<M extends RedisModules, S extends RedisScripts> extends RedisPlugins<M, S> {
|
export interface RedisClientOptions<M extends RedisModules, S extends RedisScripts> extends RedisPlugins<M, S> {
|
||||||
@@ -424,9 +424,12 @@ export default class RedisClient<M extends RedisModules, S extends RedisScripts>
|
|||||||
|
|
||||||
QUIT(): Promise<void> {
|
QUIT(): Promise<void> {
|
||||||
return this.#socket.quit(() => {
|
return this.#socket.quit(() => {
|
||||||
const promise = this.#queue.addCommand(['QUIT']);
|
const quitPromise = this.#queue.addCommand(['QUIT']);
|
||||||
this.#tick();
|
this.#tick();
|
||||||
return promise;
|
return Promise.all([
|
||||||
|
quitPromise,
|
||||||
|
this.#destroyIsolationPool()
|
||||||
|
]);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -519,7 +522,7 @@ export default class RedisClient<M extends RedisModules, S extends RedisScripts>
|
|||||||
}
|
}
|
||||||
|
|
||||||
async disconnect(): Promise<void> {
|
async disconnect(): Promise<void> {
|
||||||
this.#queue.flushAll(new Error('Disconnecting'));
|
this.#queue.flushAll(new DisconnectsClientError());
|
||||||
await Promise.all([
|
await Promise.all([
|
||||||
this.#socket.disconnect(),
|
this.#socket.disconnect(),
|
||||||
this.#destroyIsolationPool()
|
this.#destroyIsolationPool()
|
||||||
|
@@ -222,6 +222,7 @@ export default class RedisSocket extends EventEmitter {
|
|||||||
}
|
}
|
||||||
|
|
||||||
this.#socket.end();
|
this.#socket.end();
|
||||||
|
this.#socket.removeAllListeners('data');
|
||||||
await EventEmitter.once(this.#socket, 'end');
|
await EventEmitter.once(this.#socket, 'end');
|
||||||
this.#socket = undefined;
|
this.#socket = undefined;
|
||||||
this.emit('end');
|
this.emit('end');
|
||||||
|
@@ -21,3 +21,9 @@ export class ClientClosedError extends Error {
|
|||||||
super('The client is closed');
|
super('The client is closed');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export class DisconnectsClientError extends Error {
|
||||||
|
constructor() {
|
||||||
|
super('Disconnects client');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user