You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-06 02:15:48 +03:00
v4.0.0-rc.4 (#1723)
* update workflows & README * add .deepsource.toml * fix client.quit, add error events on cluster, fix some "deepsource.io" warnings * Release 4.0.0-rc.1 * add cluster.duplicate, add some tests * fix #1650 - add support for Buffer in some commands, add GET_BUFFER command * fix GET and GET_BUFFER return type * update FAQ * Update invalid code example in README.md (#1654) * Update invalid code example in README.md * Update README.md Co-authored-by: Leibale Eidelman <leibale1998@gmail.com> * fix #1652 * ref #1653 - better types * better types * fix54124793ad
* Update GEOSEARCHSTORE.spec.ts * fix #1660 - add support for client.HSET('key', 'field', 'value') * upgrade dependencies, update README * fix #1659 - add support for db-number in client options url * fix README, remove unused import, downgrade typedoc & typedoc-plugin-markdown * update client-configurations.md * fix README * add CLUSTER_SLOTS, add some tests * fix "createClient with url" test with redis 5 * remove unused imports * Release 4.0.0-rc.2 * add missing semicolon * replace empty "transformReply" functions with typescript "declare" * fix EVAL & EVALSHA, add some tests, npm update * fix #1665 - add ZRANGEBYLEX, ZRANGEBYSCORE, ZRANGEBYSCORE_WITHSCORES * new issue templates * add all COMMAND commands * run COMMAND & COMMAND INFO tests only on redis >6 * Create SECURITY.md * fix #1671 - add support for all client configurations in cluster * ref #1671 - add support for defaults * remove some commands from cluster, npm update, clean code, * lock benny version * fix #1674 - remove `isolationPoolOptions` when creating isolated connection * increase test coverage * update .npmignore * Release 4.0.0-rc.3 * fix README * remove whitespace from LICENSE * use "export { x as y }" instead of import & const * move from "NodeRedis" to "Redis" * fix #1676 * update comments * Auth before select database (#1679) * Auth before select database * fix #1681 Co-authored-by: leibale <leibale1998@gmail.com> * Adds connect-as-acl-user example. (#1684) * Adds connect-as-acl-user example. * Adds blank line at end. * Set to private. * Adds examples folder to npmignore. * Adds Apple .DS_Store file to .gitignore (#1685) * Adds Apple .DS_Store file. * Add .DS_Store to .npmignore too Co-authored-by: Leibale Eidelman <leibale1998@gmail.com> * move examples * clean some tests * clean code * Adds examples table of contents and contribution guidelines. (#1686) * Updated examples to use named functions. (#1687) * Updated examples to user named functions. * Update README.md Co-authored-by: Leibale Eidelman <leibale1998@gmail.com> * update docs, add 6.0.x to the tests matrix, add eslint, npm update, fix some commands, fix some types Co-authored-by: Simon Prickett <simon@crudworks.org> * fix tests with redis 6.0.x * fix ACL GETUSER test * fix client.quit and client.disconnect * fix ACL GETUSER * Adds TypeScript note and corrects a typo. * Fixes a bug in the Scan Iterator section. (#1694) * Made examples use local version. * Add `lua-multi-incr.js` example (#1692) Also fix syntax error in the lua example in the README Closes #1689. * Add(examples): Create an example for blPop & lPush (#1696) * Add(examples): Create an example for blPop & lPush Signed-off-by: Aditya Rastogi <adit.rastogi2014@gmail.com> * Update(examples): fix case, add timeout, update readme Signed-off-by: Aditya Rastogi <adit.rastogi2014@gmail.com> Closes #1693. * Add command-with-modifiers.js example (#1695) * Adds TypeScript note and corrects a typo. * Adds command-with-modifiers example. (redis#1688) * Adds command-with-modifiers example. (redis#1688) * Adds command-with-modifiers example. (redis#1688) * Removed callbacks. Co-authored-by: Simon Prickett <simon@redislabs.com> Closes #1688. * Issue # 1697 FIX - creates an example script that shows how to use the SSCAN iterator (#1699) * #1697 fix for set scan example * adds the js file * adds comment * Minor layout and comment adjustment. Co-authored-by: srawat2 <shashank19aug> Co-authored-by: Simon Prickett <simon@redislabs.com> Closes #1697. * fix #1706 - HSET return type should be number * use dockers for tests, fix some bugs * increase dockers timeout to 30s * release drafter (#1683) * release drafter * fixing contributors * use dockers for tests, use npm workspaces, add rejson & redisearch modules, fix some bugs * fix #1712 - fix LINDEX return type * uncomment TIME tests * use codecov * fix tests.yml * uncomment "should handle live resharding" test * fix #1714 - update README(s) * add package-lock.json * update CONTRIBUTING.md * update examples * uncomment some tests * fix test-utils * move "all-in-one" to root folder * fix tests workflow * fix bug in cluster slots, enhance live resharding test * fix live resharding test * fix #1707 - handle number arguments in legacy mode * Add rejectedUnauthorized and other TLS options (#1708) * Update socket.ts * fix #1716 - decode username and password from url * fix some Z (sorted list) commands, increase commands test coverage * remove empty lines * fix 'Scenario' typo (#1720) * update readmes, add createCluster to the `redis` package * add .release-it.json files, update some md files * run tests on pull requests too * Support esModuleInterop set to false. (#1717) * Support esModuleInterop set to false. When testing the upcoming 4.x release, we got a bunch of typescript errors emitted from this project. We quickly realized this is because the library uses the esModuleInterop flag. This makes some imports _slightly_ easier to write, but it comes at a cost: it forces any application or library using this library to *also* have esModuleInterop on. The `esModuleInterop` flag is a bit of a holdover from an earlier time, and I would not recommend using it in libraries. The main issue is that if it's set to true, you are forcing any users of the library to also have `esModuleInterop`, where if you keep have it set to `false` (the default), you leave the decision to the user. This change should have no rammifications to users with `esModuleInterop` on, but it will enable support for those that have it off. This is especially good for library authors such as myself, because I would also like to keep this flag off to not force *my* users into this feature. * All tests now pass! * Move @types/redis-parser into client sub-package and removed a comma * npm update, remove html from readme * add tests and licence badges * update changelog.md * update .npmignore and .release-it.json * update .release-it.json * Release client@1.0.0-rc.0 * revertd32f1edf8a
* fix .npmignore * replace @redis with @node-redis * Release client@1.0.0-rc.0 * update json & search version * Release json@1.0.0-rc.0 * Release search@1.0.0-rc.0 * update dependencies * Release redis@4.0.0-rc.4 Co-authored-by: Richard Samuelsson <noobtoothfairy@gmail.com> Co-authored-by: mustard <mhqnwt@gmail.com> Co-authored-by: Simon Prickett <simon@redislabs.com> Co-authored-by: Simon Prickett <simon@crudworks.org> Co-authored-by: Suze Shardlow <SuzeShardlow@users.noreply.github.com> Co-authored-by: Joshua T <buildingsomethingfun@gmail.com> Co-authored-by: Aditya Rastogi <adit.rastogi2014@gmail.com> Co-authored-by: Rohan Kumar <rohan.kr20@gmail.com> Co-authored-by: Kalki <shashank.kviit@gmail.com> Co-authored-by: Chayim <chayim@users.noreply.github.com> Co-authored-by: Da-Jin Chu <dajinchu@gmail.com> Co-authored-by: Henrique Corrêa <75134774+HeCorr@users.noreply.github.com> Co-authored-by: Evert Pot <me@evertpot.com>
This commit is contained in:
681
packages/client/lib/client/index.spec.ts
Normal file
681
packages/client/lib/client/index.spec.ts
Normal file
@@ -0,0 +1,681 @@
|
||||
import { strict as assert } from 'assert';
|
||||
import testUtils, { GLOBAL, waitTillBeenCalled } from '../test-utils';
|
||||
import RedisClient, { ClientLegacyCommandArguments, RedisClientType } from '.';
|
||||
import { RedisClientMultiCommandType } from './multi-command';
|
||||
import { RedisCommandArguments, RedisCommandRawReply, RedisModules, RedisScripts } from '../commands';
|
||||
import { AbortError, ClientClosedError, ConnectionTimeoutError, DisconnectsClientError, SocketClosedUnexpectedlyError, WatchError } from '../errors';
|
||||
import { defineScript } from '../lua-script';
|
||||
import { spy } from 'sinon';
|
||||
import { once } from 'events';
|
||||
|
||||
export const SQUARE_SCRIPT = defineScript({
|
||||
NUMBER_OF_KEYS: 0,
|
||||
SCRIPT: 'return ARGV[1] * ARGV[1];',
|
||||
transformArguments(number: number): Array<string> {
|
||||
return [number.toString()];
|
||||
},
|
||||
transformReply(reply: number): number {
|
||||
return reply;
|
||||
}
|
||||
});
|
||||
|
||||
describe('Client', () => {
|
||||
describe('parseURL', () => {
|
||||
it('redis://user:secret@localhost:6379/0', () => {
|
||||
assert.deepEqual(
|
||||
RedisClient.parseURL('redis://user:secret@localhost:6379/0'),
|
||||
{
|
||||
socket: {
|
||||
host: 'localhost',
|
||||
port: 6379
|
||||
},
|
||||
username: 'user',
|
||||
password: 'secret',
|
||||
database: 0
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('rediss://user:secret@localhost:6379/0', () => {
|
||||
assert.deepEqual(
|
||||
RedisClient.parseURL('rediss://user:secret@localhost:6379/0'),
|
||||
{
|
||||
socket: {
|
||||
host: 'localhost',
|
||||
port: 6379,
|
||||
tls: true
|
||||
},
|
||||
username: 'user',
|
||||
password: 'secret',
|
||||
database: 0
|
||||
}
|
||||
);
|
||||
});
|
||||
|
||||
it('Invalid protocol', () => {
|
||||
assert.throws(
|
||||
() => RedisClient.parseURL('redi://user:secret@localhost:6379/0'),
|
||||
TypeError
|
||||
);
|
||||
});
|
||||
|
||||
it('Invalid pathname', () => {
|
||||
assert.throws(
|
||||
() => RedisClient.parseURL('redis://user:secret@localhost:6379/NaN'),
|
||||
TypeError
|
||||
);
|
||||
});
|
||||
|
||||
it('redis://localhost', () => {
|
||||
assert.deepEqual(
|
||||
RedisClient.parseURL('redis://localhost'),
|
||||
{
|
||||
socket: {
|
||||
host: 'localhost',
|
||||
}
|
||||
}
|
||||
);
|
||||
});
|
||||
});
|
||||
|
||||
describe('authentication', () => {
|
||||
testUtils.testWithClient('Client should be authenticated', async client => {
|
||||
assert.equal(
|
||||
await client.ping(),
|
||||
'PONG'
|
||||
);
|
||||
}, GLOBAL.SERVERS.PASSWORD);
|
||||
|
||||
testUtils.testWithClient('should not retry connecting if failed due to wrong auth', async client => {
|
||||
let message;
|
||||
if (testUtils.isVersionGreaterThan([6, 2])) {
|
||||
message = 'WRONGPASS invalid username-password pair or user is disabled.';
|
||||
} else if (testUtils.isVersionGreaterThan([6])) {
|
||||
message = 'WRONGPASS invalid username-password pair';
|
||||
} else {
|
||||
message = 'ERR invalid password';
|
||||
}
|
||||
|
||||
await assert.rejects(
|
||||
client.connect(),
|
||||
{ message }
|
||||
);
|
||||
|
||||
assert.equal(client.isOpen, false);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.PASSWORD,
|
||||
clientOptions: {
|
||||
password: 'wrongpassword'
|
||||
},
|
||||
disableClientSetup: true
|
||||
});
|
||||
|
||||
testUtils.testWithClient('should execute AUTH before SELECT', async client => {
|
||||
assert.equal(
|
||||
(await client.clientInfo()).db,
|
||||
2
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.PASSWORD,
|
||||
clientOptions: {
|
||||
...GLOBAL.SERVERS.PASSWORD.clientOptions,
|
||||
database: 2
|
||||
},
|
||||
minimumDockerVersion: [6, 2]
|
||||
});
|
||||
});
|
||||
|
||||
describe('legacyMode', () => {
|
||||
function sendCommandAsync<M extends RedisModules, S extends RedisScripts>(client: RedisClientType<M, S>, args: RedisCommandArguments): Promise<RedisCommandRawReply> {
|
||||
return new Promise((resolve, reject) => {
|
||||
(client as any).sendCommand(args, (err: Error | undefined, reply: RedisCommandRawReply) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
resolve(reply);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
testUtils.testWithClient('client.sendCommand should call the callback', async client => {
|
||||
assert.equal(
|
||||
await sendCommandAsync(client, ['PING']),
|
||||
'PONG'
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.sendCommand should work without callback', async client => {
|
||||
client.sendCommand(['PING']);
|
||||
await client.v4.ping(); // make sure the first command was replied
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.v4.sendCommand should return a promise', async client => {
|
||||
assert.equal(
|
||||
await client.v4.sendCommand(['PING']),
|
||||
'PONG'
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
function setAsync<M extends RedisModules, S extends RedisScripts>(client: RedisClientType<M, S>, ...args: ClientLegacyCommandArguments): Promise<RedisCommandRawReply> {
|
||||
return new Promise((resolve, reject) => {
|
||||
(client as any).set(...args, (err: Error | undefined, reply: RedisCommandRawReply) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
resolve(reply);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
testUtils.testWithClient('client.{command} should accept vardict arguments', async client => {
|
||||
assert.equal(
|
||||
await setAsync(client, 'a', 'b'),
|
||||
'OK'
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.{command} should accept arguments array', async client => {
|
||||
assert.equal(
|
||||
await setAsync(client, ['a', 'b']),
|
||||
'OK'
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.{command} should accept mix of arrays and arguments', async client => {
|
||||
assert.equal(
|
||||
await setAsync(client, ['a'], 'b', ['EX', 1]),
|
||||
'OK'
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
function multiExecAsync<M extends RedisModules, S extends RedisScripts>(multi: RedisClientMultiCommandType<M, S>): Promise<Array<RedisCommandRawReply>> {
|
||||
return new Promise((resolve, reject) => {
|
||||
(multi as any).exec((err: Error | undefined, replies: Array<RedisCommandRawReply>) => {
|
||||
if (err) return reject(err);
|
||||
|
||||
resolve(replies);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
testUtils.testWithClient('client.multi.ping.exec should call the callback', async client => {
|
||||
assert.deepEqual(
|
||||
await multiExecAsync(
|
||||
client.multi().ping()
|
||||
),
|
||||
['PONG']
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.multi.ping.exec should call the callback', async client => {
|
||||
client.multi()
|
||||
.ping()
|
||||
.exec();
|
||||
await client.v4.ping(); // make sure the first command was replied
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.multi.ping.v4.ping.v4.exec should return a promise', async client => {
|
||||
assert.deepEqual(
|
||||
await client.multi()
|
||||
.ping()
|
||||
.v4.ping()
|
||||
.v4.exec(),
|
||||
['PONG', 'PONG']
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.{script} should return a promise', async client => {
|
||||
assert.equal(
|
||||
await client.square(2),
|
||||
4
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
legacyMode: true,
|
||||
scripts: {
|
||||
square: SQUARE_SCRIPT
|
||||
}
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe('events', () => {
|
||||
testUtils.testWithClient('connect, ready, end', async client => {
|
||||
await Promise.all([
|
||||
once(client, 'connect'),
|
||||
once(client, 'ready'),
|
||||
client.connect()
|
||||
]);
|
||||
|
||||
await Promise.all([
|
||||
once(client, 'end'),
|
||||
client.disconnect()
|
||||
]);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
disableClientSetup: true
|
||||
});
|
||||
});
|
||||
|
||||
describe('sendCommand', () => {
|
||||
testUtils.testWithClient('PING', async client => {
|
||||
assert.equal(await client.sendCommand(['PING']), 'PONG');
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('bufferMode', async client => {
|
||||
assert.deepEqual(
|
||||
await client.sendCommand(['PING'], undefined, true),
|
||||
Buffer.from('PONG')
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
describe('AbortController', () => {
|
||||
before(function () {
|
||||
if (!global.AbortController) {
|
||||
this.skip();
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('success', async client => {
|
||||
await client.sendCommand(['PING'], {
|
||||
signal: new AbortController().signal
|
||||
});
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('AbortError', client => {
|
||||
const controller = new AbortController();
|
||||
controller.abort();
|
||||
|
||||
return assert.rejects(
|
||||
client.sendCommand(['PING'], {
|
||||
signal: controller.signal
|
||||
}),
|
||||
AbortError
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
});
|
||||
|
||||
describe('multi', () => {
|
||||
testUtils.testWithClient('simple', async client => {
|
||||
assert.deepEqual(
|
||||
await client.multi()
|
||||
.ping()
|
||||
.set('key', 'value')
|
||||
.get('key')
|
||||
.exec(),
|
||||
['PONG', 'OK', 'value']
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('should reject the whole chain on error', client => {
|
||||
return assert.rejects(
|
||||
client.multi()
|
||||
.ping()
|
||||
.addCommand(['INVALID COMMAND'])
|
||||
.ping()
|
||||
.exec()
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('with script', async client => {
|
||||
assert.deepEqual(
|
||||
await client.multi()
|
||||
.square(2)
|
||||
.exec(),
|
||||
[4]
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
scripts: {
|
||||
square: SQUARE_SCRIPT
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('WatchError', async client => {
|
||||
await client.watch('key');
|
||||
|
||||
await client.set(
|
||||
RedisClient.commandOptions({
|
||||
isolated: true
|
||||
}),
|
||||
'key',
|
||||
'1'
|
||||
);
|
||||
|
||||
await assert.rejects(
|
||||
client.multi()
|
||||
.decr('key')
|
||||
.exec(),
|
||||
WatchError
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('execAsPipeline', async client => {
|
||||
assert.deepEqual(
|
||||
await client.multi()
|
||||
.ping()
|
||||
.exec(true),
|
||||
['PONG']
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
});
|
||||
|
||||
testUtils.testWithClient('scripts', async client => {
|
||||
assert.equal(
|
||||
await client.square(2),
|
||||
4
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
scripts: {
|
||||
square: SQUARE_SCRIPT
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('modules', async client => {
|
||||
assert.equal(
|
||||
await client.module.echo('message'),
|
||||
'message'
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
modules: {
|
||||
module: {
|
||||
echo: {
|
||||
transformArguments(message: string): Array<string> {
|
||||
return ['ECHO', message];
|
||||
},
|
||||
transformReply(reply: string): string {
|
||||
return reply;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
testUtils.testWithClient('executeIsolated', async client => {
|
||||
await client.sendCommand(['CLIENT', 'SETNAME', 'client']);
|
||||
|
||||
assert.equal(
|
||||
await client.executeIsolated(isolatedClient =>
|
||||
isolatedClient.sendCommand(['CLIENT', 'GETNAME'])
|
||||
),
|
||||
null
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
async function killClient<M extends RedisModules, S extends RedisScripts>(client: RedisClientType<M, S>): Promise<void> {
|
||||
const onceErrorPromise = once(client, 'error');
|
||||
await client.sendCommand(['QUIT']);
|
||||
await Promise.all([
|
||||
onceErrorPromise,
|
||||
assert.rejects(client.ping(), SocketClosedUnexpectedlyError)
|
||||
]);
|
||||
}
|
||||
|
||||
testUtils.testWithClient('should reconnect when socket disconnects', async client => {
|
||||
await killClient(client);
|
||||
await assert.doesNotReject(client.ping());
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('should remember selected db', async client => {
|
||||
await client.select(1);
|
||||
await killClient(client);
|
||||
assert.equal(
|
||||
(await client.clientInfo()).db,
|
||||
1
|
||||
);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
minimumDockerVersion: [6, 2] // CLIENT INFO
|
||||
});
|
||||
|
||||
testUtils.testWithClient('scanIterator', async client => {
|
||||
const promises = [],
|
||||
keys = new Set();
|
||||
for (let i = 0; i < 100; i++) {
|
||||
const key = i.toString();
|
||||
keys.add(key);
|
||||
promises.push(client.set(key, ''));
|
||||
}
|
||||
|
||||
await Promise.all(promises);
|
||||
|
||||
const results = new Set();
|
||||
for await (const key of client.scanIterator()) {
|
||||
results.add(key);
|
||||
}
|
||||
|
||||
assert.deepEqual(keys, results);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('hScanIterator', async client => {
|
||||
const hash: Record<string, string> = {};
|
||||
for (let i = 0; i < 100; i++) {
|
||||
hash[i.toString()] = i.toString();
|
||||
}
|
||||
|
||||
await client.hSet('key', hash);
|
||||
|
||||
const results: Record<string, string> = {};
|
||||
for await (const { field, value } of client.hScanIterator('key')) {
|
||||
results[field] = value;
|
||||
}
|
||||
|
||||
assert.deepEqual(hash, results);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('sScanIterator', async client => {
|
||||
const members = new Set<string>();
|
||||
for (let i = 0; i < 100; i++) {
|
||||
members.add(i.toString());
|
||||
}
|
||||
|
||||
await client.sAdd('key', Array.from(members));
|
||||
|
||||
const results = new Set<string>();
|
||||
for await (const key of client.sScanIterator('key')) {
|
||||
results.add(key);
|
||||
}
|
||||
|
||||
assert.deepEqual(members, results);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('zScanIterator', async client => {
|
||||
const members = [];
|
||||
for (let i = 0; i < 100; i++) {
|
||||
members.push({
|
||||
score: 1,
|
||||
value: i.toString()
|
||||
});
|
||||
}
|
||||
|
||||
await client.zAdd('key', members);
|
||||
|
||||
const map = new Map();
|
||||
for await (const member of client.zScanIterator('key')) {
|
||||
map.set(member.value, member.score);
|
||||
}
|
||||
|
||||
type MemberTuple = [string, number];
|
||||
|
||||
function sort(a: MemberTuple, b: MemberTuple) {
|
||||
return Number(b[0]) - Number(a[0]);
|
||||
}
|
||||
|
||||
assert.deepEqual(
|
||||
[...map.entries()].sort(sort),
|
||||
members.map<MemberTuple>(member => [member.value, member.score]).sort(sort)
|
||||
);
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('PubSub', async publisher => {
|
||||
const subscriber = publisher.duplicate();
|
||||
|
||||
await subscriber.connect();
|
||||
|
||||
try {
|
||||
const channelListener1 = spy(),
|
||||
channelListener2 = spy(),
|
||||
patternListener = spy();
|
||||
|
||||
await Promise.all([
|
||||
subscriber.subscribe('channel', channelListener1),
|
||||
subscriber.subscribe('channel', channelListener2),
|
||||
subscriber.pSubscribe('channel*', patternListener)
|
||||
]);
|
||||
|
||||
await Promise.all([
|
||||
waitTillBeenCalled(channelListener1),
|
||||
waitTillBeenCalled(channelListener2),
|
||||
waitTillBeenCalled(patternListener),
|
||||
publisher.publish('channel', 'message')
|
||||
]);
|
||||
|
||||
assert.ok(channelListener1.calledOnceWithExactly('message', 'channel'));
|
||||
assert.ok(channelListener2.calledOnceWithExactly('message', 'channel'));
|
||||
assert.ok(patternListener.calledOnceWithExactly('message', 'channel'));
|
||||
|
||||
await subscriber.unsubscribe('channel', channelListener1);
|
||||
await Promise.all([
|
||||
waitTillBeenCalled(channelListener2),
|
||||
waitTillBeenCalled(patternListener),
|
||||
publisher.publish('channel', 'message')
|
||||
]);
|
||||
|
||||
assert.ok(channelListener1.calledOnce);
|
||||
assert.ok(channelListener2.calledTwice);
|
||||
assert.ok(channelListener2.secondCall.calledWithExactly('message', 'channel'));
|
||||
assert.ok(patternListener.calledTwice);
|
||||
assert.ok(patternListener.secondCall.calledWithExactly('message', 'channel'));
|
||||
|
||||
await subscriber.unsubscribe('channel');
|
||||
await Promise.all([
|
||||
waitTillBeenCalled(patternListener),
|
||||
publisher.publish('channel', 'message')
|
||||
]);
|
||||
|
||||
assert.ok(channelListener1.calledOnce);
|
||||
assert.ok(channelListener2.calledTwice);
|
||||
assert.ok(patternListener.calledThrice);
|
||||
assert.ok(patternListener.thirdCall.calledWithExactly('message', 'channel'));
|
||||
|
||||
await subscriber.pUnsubscribe();
|
||||
await publisher.publish('channel', 'message');
|
||||
|
||||
assert.ok(channelListener1.calledOnce);
|
||||
assert.ok(channelListener2.calledTwice);
|
||||
assert.ok(patternListener.calledThrice);
|
||||
|
||||
// should be able to send commands when unsubsribed from all channels (see #1652)
|
||||
await assert.doesNotReject(subscriber.ping());
|
||||
} finally {
|
||||
await subscriber.disconnect();
|
||||
}
|
||||
}, GLOBAL.SERVERS.OPEN);
|
||||
|
||||
testUtils.testWithClient('ConnectionTimeoutError', async client => {
|
||||
const promise = assert.rejects(client.connect(), ConnectionTimeoutError),
|
||||
start = process.hrtime.bigint();
|
||||
|
||||
while (process.hrtime.bigint() - start < 1_000_000) {
|
||||
// block the event loop for 1ms, to make sure the connection will timeout
|
||||
}
|
||||
|
||||
await promise;
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
clientOptions: {
|
||||
socket: {
|
||||
connectTimeout: 1
|
||||
}
|
||||
},
|
||||
disableClientSetup: true
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.quit', async client => {
|
||||
await client.connect();
|
||||
|
||||
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');
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
disableClientSetup: true
|
||||
});
|
||||
|
||||
testUtils.testWithClient('client.disconnect', async client => {
|
||||
await client.connect();
|
||||
|
||||
const pingPromise = client.ping(),
|
||||
disconnectPromise = client.disconnect();
|
||||
assert.equal(client.isOpen, false);
|
||||
await Promise.all([
|
||||
assert.rejects(pingPromise, DisconnectsClientError),
|
||||
assert.doesNotReject(disconnectPromise),
|
||||
assert.rejects(client.ping(), ClientClosedError)
|
||||
]);
|
||||
}, {
|
||||
...GLOBAL.SERVERS.OPEN,
|
||||
disableClientSetup: true
|
||||
});
|
||||
});
|
Reference in New Issue
Block a user