Skip to content

Commit 7f7a53a

Browse files
committed
uncomment some tests, fix bugs with _selectedDB
1 parent 3b28aa6 commit 7f7a53a

File tree

3 files changed

+82
-128
lines changed

3 files changed

+82
-128
lines changed

packages/client/lib/client/index.spec.ts

Lines changed: 64 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,13 @@
11
import { strict as assert } from 'node:assert';
22
import testUtils, { GLOBAL, waitTillBeenCalled } from '../test-utils';
33
import RedisClient, { RedisClientType } from '.';
4-
// import { RedisClientMultiCommandType } from './multi-command';
5-
// import { RedisCommandRawReply, RedisModules, RedisFunctions, RedisScripts } from '../commands';
64
import { AbortError, ClientClosedError, ClientOfflineError, ConnectionTimeoutError, DisconnectsClientError, ErrorReply, MultiErrorReply, SocketClosedUnexpectedlyError, WatchError } from '../errors';
75
import { defineScript } from '../lua-script';
86
import { spy } from 'sinon';
97
import { once } from 'node:events';
10-
// import { ClientKillFilters } from '../commands/CLIENT_KILL';
11-
// import { promisify } from 'node:util';
128
import { MATH_FUNCTION, loadMathFunction } from '../commands/FUNCTION_LOAD.spec';
139
import { RESP_TYPES } from '../RESP/decoder';
14-
import { NumberReply } from '../RESP/types';
10+
import { BlobStringReply, NumberReply } from '../RESP/types';
1511
import { SortedSetMember } from '../commands/generic-transformers';
1612

1713
export const SQUARE_SCRIPT = defineScript({
@@ -232,24 +228,26 @@ describe('Client', () => {
232228
}
233229
});
234230

235-
// testUtils.testWithClient('WatchError', async client => {
236-
// await client.watch('key');
237-
238-
// await client.set(
239-
// RedisClient.commandOptions({
240-
// isolated: true
241-
// }),
242-
// 'key',
243-
// '1'
244-
// );
245-
246-
// await assert.rejects(
247-
// client.multi()
248-
// .decr('key')
249-
// .exec(),
250-
// WatchError
251-
// );
252-
// }, GLOBAL.SERVERS.OPEN);
231+
testUtils.testWithClient('WatchError', async client => {
232+
await client.watch('key');
233+
234+
const duplicate = await client.duplicate().connect();
235+
try {
236+
await client.set(
237+
'key',
238+
'1'
239+
);
240+
} finally {
241+
duplicate.destroy();
242+
}
243+
244+
await assert.rejects(
245+
client.multi()
246+
.decr('key')
247+
.exec(),
248+
WatchError
249+
);
250+
}, GLOBAL.SERVERS.OPEN);
253251

254252
describe('execAsPipeline', () => {
255253
testUtils.testWithClient('exec(true)', async client => {
@@ -269,20 +267,19 @@ describe('Client', () => {
269267
}, GLOBAL.SERVERS.OPEN);
270268
});
271269

272-
// testUtils.testWithClient('should remember selected db', async client => {
273-
// await client.multi()
274-
// .select(1)
275-
// .exec();
276-
// await killClient(client);
277-
// assert.equal(
278-
// (await client.clientInfo()).db,
279-
// 1
280-
// );
281-
// }, {
282-
// ...GLOBAL.SERVERS.OPEN,
283-
// minimumDockerVersion: [6, 2] // CLIENT INFO
284-
// });
285-
270+
testUtils.testWithClient('should remember selected db', async client => {
271+
await client.multi()
272+
.select(1)
273+
.exec();
274+
await killClient(client);
275+
assert.equal(
276+
(await client.clientInfo()).db,
277+
1
278+
);
279+
}, {
280+
...GLOBAL.SERVERS.OPEN,
281+
minimumDockerVersion: [6, 2] // CLIENT INFO
282+
});
286283

287284
testUtils.testWithClient('should handle error replies (#2665)', async client => {
288285
await assert.rejects(
@@ -320,12 +317,10 @@ describe('Client', () => {
320317

321318
const module = {
322319
echo: {
323-
transformArguments(message: string): Array<string> {
320+
transformArguments(message: string) {
324321
return ['ECHO', message];
325322
},
326-
transformReply(reply: string): string {
327-
return reply;
328-
}
323+
transformReply: undefined as unknown as () => BlobStringReply
329324
}
330325
};
331326

@@ -386,81 +381,34 @@ describe('Client', () => {
386381
disableClientSetup: true,
387382
});
388383

389-
// describe('isolationPool', () => {
390-
// testUtils.testWithClient('executeIsolated', async client => {
391-
// const id = await client.clientId(),
392-
// isolatedId = await client.executeIsolated(isolatedClient => isolatedClient.clientId());
393-
// assert.ok(id !== isolatedId);
394-
// }, GLOBAL.SERVERS.OPEN);
395-
396-
// testUtils.testWithClient('should be able to use pool even before connect', async client => {
397-
// await client.executeIsolated(() => Promise.resolve());
398-
// // make sure to destroy isolation pool
399-
// await client.connect();
400-
// await client.disconnect();
401-
// }, {
402-
// ...GLOBAL.SERVERS.OPEN,
403-
// disableClientSetup: true
404-
// });
405-
406-
// testUtils.testWithClient('should work after reconnect (#2406)', async client => {
407-
// await client.disconnect();
408-
// await client.connect();
409-
// await client.executeIsolated(() => Promise.resolve());
410-
// }, GLOBAL.SERVERS.OPEN);
411-
412-
// testUtils.testWithClient('should throw ClientClosedError after disconnect', async client => {
413-
// await client.connect();
414-
// await client.disconnect();
415-
// await assert.rejects(
416-
// client.executeIsolated(() => Promise.resolve()),
417-
// ClientClosedError
418-
// );
419-
// }, {
420-
// ...GLOBAL.SERVERS.OPEN,
421-
// disableClientSetup: true
422-
// });
423-
// });
424-
425-
// async function killClient<
426-
// M extends RedisModules,
427-
// F extends RedisFunctions,
428-
// S extends RedisScripts
429-
// >(
430-
// client: RedisClientType<M, F, S>,
431-
// errorClient: RedisClientType<M, F, S> = client
432-
// ): Promise<void> {
433-
// const onceErrorPromise = once(errorClient, 'error');
434-
// await client.sendCommand(['QUIT']);
435-
// await Promise.all([
436-
// onceErrorPromise,
437-
// assert.rejects(client.ping(), SocketClosedUnexpectedlyError)
438-
// ]);
439-
// }
440-
441-
// testUtils.testWithClient('should reconnect when socket disconnects', async client => {
442-
// await killClient(client);
443-
// await assert.doesNotReject(client.ping());
444-
// }, GLOBAL.SERVERS.OPEN);
445-
446-
// testUtils.testWithClient('should remember selected db', async client => {
447-
// await client.select(1);
448-
// await killClient(client);
449-
// assert.equal(
450-
// (await client.clientInfo()).db,
451-
// 1
452-
// );
453-
// }, {
454-
// ...GLOBAL.SERVERS.OPEN,
455-
// minimumDockerVersion: [6, 2] // CLIENT INFO
456-
// });
457-
458-
// testUtils.testWithClient('should propagated errors from "isolated" clients', client => {
459-
// client.on('error', () => {
460-
// // ignore errors
461-
// });
462-
// return client.executeIsolated(isolated => killClient(isolated, client));
463-
// }, GLOBAL.SERVERS.OPEN);
384+
async function killClient(
385+
client: RedisClientType<any, any, any, any, any>,
386+
errorClient: RedisClientType<any, any, any, any, any> = client
387+
): Promise<void> {
388+
const onceErrorPromise = once(errorClient, 'error');
389+
await client.sendCommand(['QUIT']);
390+
await Promise.all([
391+
onceErrorPromise,
392+
assert.rejects(client.ping(), SocketClosedUnexpectedlyError)
393+
]);
394+
}
395+
396+
testUtils.testWithClient('should reconnect when socket disconnects', async client => {
397+
await killClient(client);
398+
await assert.doesNotReject(client.ping());
399+
}, GLOBAL.SERVERS.OPEN);
400+
401+
testUtils.testWithClient('should remember selected db', async client => {
402+
await client.select(1);
403+
await killClient(client);
404+
assert.equal(
405+
(await client.clientInfo()).db,
406+
1
407+
);
408+
}, {
409+
...GLOBAL.SERVERS.OPEN,
410+
minimumDockerVersion: [6, 2] // CLIENT INFO
411+
});
464412

465413
testUtils.testWithClient('scanIterator', async client => {
466414
const entries: Array<string> = [],

packages/client/lib/client/index.ts

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -315,7 +315,7 @@ export default class RedisClient<
315315
}
316316

317317
if (options?.database) {
318-
this._selectedDB = options.database;
318+
this.self._selectedDB = options.database;
319319
}
320320

321321
if (options?.commandOptions) {
@@ -584,7 +584,7 @@ export default class RedisClient<
584584

585585
async SELECT(db: number): Promise<void> {
586586
await this.sendCommand(['SELECT', db.toString()]);
587-
this._selectedDB = db;
587+
this.self._selectedDB = db;
588588
}
589589

590590
select = this.SELECT;
@@ -742,7 +742,10 @@ export default class RedisClient<
742742
/**
743743
* @internal
744744
*/
745-
_executePipeline(commands: Array<RedisMultiQueuedCommand>) {
745+
async _executePipeline(
746+
commands: Array<RedisMultiQueuedCommand>,
747+
selectedDB?: number
748+
) {
746749
if (!this._socket.isOpen) {
747750
return Promise.reject(new ClientClosedError());
748751
}
@@ -753,7 +756,13 @@ export default class RedisClient<
753756
}))
754757
);
755758
this._scheduleWrite();
756-
return promise;
759+
const result = await promise;
760+
761+
if (selectedDB !== undefined) {
762+
this.self._selectedDB = selectedDB;
763+
}
764+
765+
return result;
757766
}
758767

759768
/**
@@ -764,7 +773,7 @@ export default class RedisClient<
764773
selectedDB?: number
765774
) {
766775
if (!this._socket.isOpen) {
767-
return Promise.reject(new ClientClosedError());
776+
throw new ClientClosedError();
768777
}
769778

770779
const typeMapping = this._commandOptions?.typeMapping,
@@ -796,7 +805,7 @@ export default class RedisClient<
796805
}
797806

798807
if (selectedDB !== undefined) {
799-
this._selectedDB = selectedDB;
808+
this.self._selectedDB = selectedDB;
800809
}
801810

802811
return execResult as Array<unknown>;

packages/client/lib/client/multi-command.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,6 @@ export type RedisClientMultiCommandType<
8585

8686
type ExecuteMulti = (commands: Array<RedisMultiQueuedCommand>, selectedDB?: number) => Promise<Array<unknown>>;
8787

88-
type ExecutePipeline = (commands: Array<RedisMultiQueuedCommand>) => Promise<Array<unknown>>;
89-
9088
export default class RedisClientMultiCommand<REPLIES = []> {
9189
private static _createCommand(command: Command, resp: RespVersions) {
9290
const transformReply = getTransformReply(command, resp);
@@ -153,13 +151,12 @@ export default class RedisClientMultiCommand<REPLIES = []> {
153151

154152
private readonly _multi = new RedisMultiCommand();
155153
private readonly _executeMulti: ExecuteMulti;
156-
private readonly _executePipeline: ExecutePipeline;
154+
private readonly _executePipeline: ExecuteMulti;
157155
private _selectedDB?: number;
158156

159-
constructor(executeMulti: ExecuteMulti, executePipeline: ExecutePipeline) {
157+
constructor(executeMulti: ExecuteMulti, executePipeline: ExecuteMulti) {
160158
this._executeMulti = executeMulti;
161159
this._executePipeline = executePipeline;
162-
// this._client = client;
163160
}
164161

165162
SELECT(db: number, transformReply?: TransformReply): this {
@@ -193,7 +190,7 @@ export default class RedisClientMultiCommand<REPLIES = []> {
193190
if (this._multi.queue.length === 0) return [] as MultiReplyType<T, REPLIES>;
194191

195192
return this._multi.transformReplies(
196-
await this._executePipeline(this._multi.queue)
193+
await this._executePipeline(this._multi.queue, this._selectedDB)
197194
) as MultiReplyType<T, REPLIES>;
198195
}
199196

0 commit comments

Comments
 (0)