diff --git a/API.md b/API.md index 0a38541..9c5763c 100644 --- a/API.md +++ b/API.md @@ -31,7 +31,7 @@ Other supported Redis options: - `sentinelName` - the name of the sentinel master (when `sentinels` is specified). - `tls` - an object representing TLS config options for **ioredis**. -The plugin also accepts other `redis` options not mentioned above. +The plugin also accepts other `redis` options not mentioned above. It should work with `redis`, `dragonflydb`, and `valkey`— any Redis-compatible datastores should work with this plugin. ### Usage diff --git a/docker-compose.yml b/docker-compose.yml index 1c01da6..def9656 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,21 +1,20 @@ -version: '3.0' services: redis_basic: container_name: redis_basic - image: redis:6-alpine + image: redis:8-alpine ports: - 6379:6379 redis_with_password: container_name: redis_with_password - image: redis:6-alpine + image: redis:8-alpine command: redis-server --requirepass secret ports: - 6378:6379 redis_cluster: container_name: redis_cluster - image: grokzen/redis-cluster:6.2.8 + image: grokzen/redis-cluster:7.2.5 environment: IP: '0.0.0.0' CLUSTER_ONLY: 'true' @@ -26,3 +25,29 @@ services: - 7003:7003 - 7004:7004 - 7005:7005 + + dragonfly: + image: docker.dragonflydb.io/dragonflydb/dragonfly + container_name: dragonfly_db + ports: + - "6377:6379" + + dragonfly_with_password: + image: docker.dragonflydb.io/dragonflydb/dragonfly + container_name: dragonfly_db_with_password + command: dragonfly --requirepass secret + ports: + - "6376:6379" + + valkey: + image: valkey/valkey:8-alpine + container_name: valkey_db + ports: + - "6375:6379" + + valkey_with_password: + image: valkey/valkey:8-alpine + container_name: valkey_db_with_password + command: valkey-server --requirepass secret + ports: + - "6374:6379" diff --git a/test/index.js b/test/index.js index 27fc5ee..9b0f8c0 100755 --- a/test/index.js +++ b/test/index.js @@ -16,6 +16,42 @@ const internals = {}; const { it, describe } = exports.lab = Lab.script(); const expect = Code.expect; +const configs = [ + ['redis', { + nopass: { + host: '127.0.0.1', + port: 6379 + }, + withpass: { + host: '127.0.0.1', + port: 6378, + password: 'secret' + } + }], + ['dragonfly', { + nopass: { + host: '127.0.0.1', + port: 6377 + }, + withpass: { + host: '127.0.0.1', + port: 6376, + password: 'secret' + } + }], + ['valkey', { + nopass: { + host: '127.0.0.1', + port: 6375 + }, + withpass: { + host: '127.0.0.1', + port: 6374, + password: 'secret' + } + }] +]; + describe('Connection', { retry: true }, () => { it('creates a new connection', async () => { @@ -84,350 +120,357 @@ describe('Connection', { retry: true }, () => { await redisClient.quit(); }); - it('gets an item after setting it', async () => { + for (const [name, config] of configs) { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + it(`${name}: gets an item after setting it`, async () => { - const key = { id: 'x', segment: 'test' }; - await client.set(key, '123', 500); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - const result = await client.get(key); - expect(result.item).to.equal('123'); - }); + const key = { id: 'x', segment: 'test' }; + await client.set(key, '123', 500); - it('fails setting an item circular references', async () => { + const result = await client.get(key); + expect(result.item).to.equal('123'); + }); - const client = new Catbox.Client(CatboxRedis); - await client.start(); - const key = { id: 'x', segment: 'test' }; - const value = { a: 1 }; - value.b = value; + it(`${name}: fails setting an item circular references`, async () => { - await expect(client.set(key, value, 10)).to.reject(/Converting circular structure to JSON/); - }); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); + const key = { id: 'x', segment: 'test' }; + const value = { a: 1 }; + value.b = value; - it('ignored starting a connection twice on same event', () => { + await expect(client.set(key, value, 10)).to.reject(/Converting circular structure to JSON/); + }); - return new Promise((resolve, reject) => { + it(`${name}: ignored starting a connection twice on same event`, () => { - const client = new Catbox.Client(CatboxRedis); - let x = 2; - const start = async () => { + return new Promise((resolve, reject) => { - await client.start(); - expect(client.isReady()).to.equal(true); - --x; - if (!x) { - resolve(); - } - }; + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + let x = 2; + const start = async () => { - start(); - start(); + await client.start(); + expect(client.isReady()).to.equal(true); + --x; + if (!x) { + resolve(); + } + }; + + start(); + start(); + }); }); - }); - it('ignored starting a connection twice chained', async () => { + it(`${name}: ignored starting a connection twice chained`, async () => { - const client = new Catbox.Client(CatboxRedis); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); - await client.start(); - expect(client.isReady()).to.equal(true); + await client.start(); + expect(client.isReady()).to.equal(true); - await client.start(); - expect(client.isReady()).to.equal(true); - }); + await client.start(); + expect(client.isReady()).to.equal(true); + }); - it('returns not found on get when using null key', async () => { + it(`${name}: returns not found on get when using null key`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - const result = await client.get(null); + const result = await client.get(null); - expect(result).to.equal(null); - }); + expect(result).to.equal(null); + }); - it('returns not found on get when item expired', async () => { + it(`${name}: returns not found on get when item expired`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - const key = { id: 'x', segment: 'test' }; - await client.set(key, 'x', 1); + const key = { id: 'x', segment: 'test' }; + await client.set(key, 'x', 1); - await Hoek.wait(2); - const result = await client.get(key); - expect(result).to.equal(null); - }); + await Hoek.wait(2); + const result = await client.get(key); + expect(result).to.equal(null); + }); - it('errors on set when using null key', async () => { + it(`${name}: errors on set when using null key`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - await expect(client.set(null, {}, 1000)).to.reject(); - }); + await expect(client.set(null, {}, 1000)).to.reject(); + }); - it('errors on get when using invalid key', async () => { + it(`${name}: errors on get when using invalid key`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - await expect(client.get({})).to.reject(); - }); + await expect(client.get({})).to.reject(); + }); - it('errors on drop when using invalid key', async () => { + it(`${name}: errors on drop when using invalid key`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - await expect(client.drop({})).to.reject(); - }); + await expect(client.drop({})).to.reject(); + }); - it('errors on set when using invalid key', async () => { + it(`${name}: errors on set when using invalid key`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - await expect(client.set({}, {}, 1000)).to.reject(); - }); + await expect(client.set({}, {}, 1000)).to.reject(); + }); - it('ignores set when using non-positive ttl value', async () => { + it(`${name}: ignores set when using non-positive ttl value`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); - const key = { id: 'x', segment: 'test' }; - await client.set(key, 'y', 0); - }); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); + const key = { id: 'x', segment: 'test' }; + await client.set(key, 'y', 0); + }); - it('errors on drop when using null key', async () => { + it(`${name}: errors on drop when using null key`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.start(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.start(); - await expect(client.drop(null)).to.reject(); - }); + await expect(client.drop(null)).to.reject(); + }); - it('errors on get when stopped', async () => { + it(`${name}: errors on get when stopped`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.stop(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.stop(); - const key = { id: 'x', segment: 'test' }; - await expect(client.connection.get(key)).to.reject('Connection not started'); - }); + const key = { id: 'x', segment: 'test' }; + await expect(client.connection.get(key)).to.reject('Connection not started'); + }); - it('errors on set when stopped', async () => { + it(`${name}: errors on set when stopped`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.stop(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.stop(); - const key = { id: 'x', segment: 'test' }; - expect(() => client.connection.set(key, 'y', 1)).to.throw('Connection not started'); - }); + const key = { id: 'x', segment: 'test' }; + expect(() => client.connection.set(key, 'y', 1)).to.throw('Connection not started'); + }); - it('errors on drop when stopped', async () => { + it(`${name}: errors on drop when stopped`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.stop(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.stop(); - const key = { id: 'x', segment: 'test' }; + const key = { id: 'x', segment: 'test' }; - try { - await client.connection.drop(key); - } - catch (err) { - expect(err.message).to.equal('Connection not started'); - } - }); + try { + await client.connection.drop(key); + } + catch (err) { + expect(err.message).to.equal('Connection not started'); + } + }); - it('errors on missing segment name', () => { + it(`${name}: errors on missing segment name`, () => { - const config = { - expiresIn: 50000 - }; - const fn = () => { + const policyConfig = { + expiresIn: 50000 + }; + const fn = () => { - const client = new Catbox.Client(CatboxRedis); - new Catbox.Policy(config, client, ''); - }; + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + new Catbox.Policy(policyConfig, client, ''); + }; - expect(fn).to.throw(Error); - }); + expect(fn).to.throw(Error); + }); - it('errors on bad segment name', () => { + it(`${name}: errors on bad segment name`, () => { - const config = { - expiresIn: 50000 - }; - const fn = () => { + const policyConfig = { + expiresIn: 50000 + }; + const fn = () => { - const client = new Catbox.Client(CatboxRedis); - new Catbox.Policy(config, client, 'a\0b'); - }; + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + new Catbox.Policy(policyConfig, client, 'a\0b'); + }; - expect(fn).to.throw(Error); - }); + expect(fn).to.throw(Error); + }); - it('errors when cache item dropped while stopped', async () => { + it(`${name}: errors when cache item dropped while stopped`, async () => { - const client = new Catbox.Client(CatboxRedis); - await client.stop(); + const client = new Catbox.Client(new CatboxRedis(config.nopass)); + await client.stop(); - await expect(client.drop('a')).to.reject(); - }); + await expect(client.drop('a')).to.reject(); + }); - describe('start()', () => { + describe(`${name}: start()`, () => { - it('sets client to when the connection succeeds', async () => { + it('sets client to when the connection succeeds', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - await redis.start(); - expect(redis.client).to.exist(); - }); + await redis.start(); + expect(redis.client).to.exist(); + }); - it('reuses the client when a connection is already started', async () => { + it('reuses the client when a connection is already started', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - await redis.start(); - const client = redis.client; + await redis.start(); + const client = redis.client; - await redis.start(); - expect(client).to.equal(redis.client); - }); + await redis.start(); + expect(client).to.equal(redis.client); + }); - it('returns an error when connection fails', async () => { + it('returns an error when connection fails', async () => { - const options = { - host: '127.0.0.1', - port: 6380 - }; + const options = { + host: '127.0.0.1', + port: 6380 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - await expect(redis.start()).to.reject(); + await expect(redis.start()).to.reject(); - expect(redis.client).to.not.exist(); - }); + expect(redis.client).to.not.exist(); + }); - it('sends auth command when password is provided', async () => { + it('sends auth command when password is provided', async () => { - const options = { - host: '127.0.0.1', - port: 6379, - password: 'wrongpassword' - }; + if (name === 'dragonfly') { - const redis = new CatboxRedis(options); + // When testing: connect with password when none is required + // Dragonfly won't cause ioredis to trigger the console.warn message. + // This test is not critical for functionality + return true; + } - const warn = console.warn; - let consoleMessage = ''; - console.warn = function (message) { + const options = { + ...config.nopass, + password: 'wrongpassword' + }; - consoleMessage += message; - }; + const redis = new CatboxRedis(options); - await redis.start(); + const warn = console.warn; + let consoleMessage = ''; + console.warn = function (message) { - console.warn = warn; - expect(consoleMessage).to.contain('does not require a password, but a password was supplied'); - }); + consoleMessage += message; + }; - it('fails in error when auth is not correct', async () => { + await redis.start(); - const options = { - host: '127.0.0.1', - port: 6378, - password: 'foo' - }; + console.warn = warn; + expect(consoleMessage).to.contain('does not require a password, but a password was supplied'); + }); - const redis = new CatboxRedis(options); + it('fails in error when auth is not correct', async () => { - await expect(redis.start()).to.reject(); + const options = { + ...config.withpass, + password: 'foo' + }; - expect(redis.client).to.not.exist(); - }); + const redis = new CatboxRedis(options); - it('success when auth is correct', async () => { + const start = redis.start(); + console.log(start); - const options = { - host: '127.0.0.1', - port: 6378, - password: 'secret' - }; + await expect(start).to.reject(); - const redis = new CatboxRedis(options); + expect(redis.client).to.not.exist(); + }); - await redis.start(); - expect(redis.client).to.exist(); - }); + it('success when auth is correct', async () => { + + const options = { + ...config.withpass + }; - it('sends select command when database is provided', async () => { + const redis = new CatboxRedis(options); - const options = { - host: '127.0.0.1', - port: 6379, - database: 1 - }; + await redis.start(); + expect(redis.client).to.exist(); + }); - const redis = new CatboxRedis(options); + it('sends select command when database is provided', async () => { - await redis.start(); - expect(redis.client).to.exist(); - }); + const options = { + host: '127.0.0.1', + port: 6379, + database: 1 + }; - it('connects to a unix domain socket when one is provided', () => { + const redis = new CatboxRedis(options); - const socketPath = '/tmp/catbox-redis.sock'; - const promise = new Promise((resolve, reject) => { + await redis.start(); + expect(redis.client).to.exist(); + }); - let connected = false; - const server = new Net.createServer((socket) => { + it('connects to a unix domain socket when one is provided', () => { - connected = true; - socket.destroy(); - }); + const socketPath = '/tmp/catbox-redis.sock'; + const promise = new Promise((resolve, reject) => { - server.once('error', reject); - server.listen(socketPath, async () => { + let connected = false; + const server = new Net.createServer((socket) => { - const redis = new CatboxRedis({ socket: socketPath }); - await expect(redis.start()).to.reject('Connection is closed.'); - expect(connected).to.equal(true); - server.close(resolve); - }); - }); + connected = true; + socket.destroy(); + }); - return promise; - }); + server.once('error', reject); + server.listen(socketPath, async () => { - it('connects via a Redis URL when one is provided', async () => { + const redis = new CatboxRedis({ socket: socketPath }); + await expect(redis.start()).to.reject('Connection is closed.'); + expect(connected).to.equal(true); + server.close(resolve); + }); + }); - const options = { - url: 'redis://127.0.0.1:6379' - }; + return promise; + }); - const redis = new CatboxRedis(options); + it('connects via a Redis URL when one is provided', async () => { - await redis.start(); - expect(redis.client).to.exist(); - }); + const options = { + url: 'redis://127.0.0.1:6379' + }; - describe('', () => { + const redis = new CatboxRedis(options); + + await redis.start(); + expect(redis.client).to.exist(); + }); it('connects to a sentinel cluster', async () => { @@ -465,398 +508,400 @@ describe('Connection', { retry: true }, () => { expect(client.connector.options.sentinels).to.equal(options.sentinels); expect(client.connector.options.name).to.equal(options.sentinelName); }); - }); - it('does not stops the client on error post connection', async () => { + it('does not stops the client on error post connection', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - await redis.start(); - expect(redis.client).to.exist(); + await redis.start(); + expect(redis.client).to.exist(); - redis.client.emit('error', new Error('injected')); - expect(redis.client).to.exist(); + redis.client.emit('error', new Error('injected')); + expect(redis.client).to.exist(); + }); }); - }); - describe('isReady()', () => { + describe(`${name}: isReady()`, () => { - it('returns true when when connected', async () => { + it('returns true when when connected', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - await redis.start(); - expect(redis.client).to.exist(); - expect(redis.isReady()).to.equal(true); - await redis.stop(); - }); + await redis.start(); + expect(redis.client).to.exist(); + expect(redis.isReady()).to.equal(true); + await redis.stop(); + }); - it('returns false when stopped', async () => { + it('returns false when stopped', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - await redis.start(); - expect(redis.client).to.exist(); - expect(redis.isReady()).to.equal(true); - await redis.stop(); - expect(redis.isReady()).to.equal(false); + await redis.start(); + expect(redis.client).to.exist(); + expect(redis.isReady()).to.equal(true); + await redis.stop(); + expect(redis.isReady()).to.equal(false); + }); }); - }); - describe('validateSegmentName()', () => { + describe(`${name}: validateSegmentName()`, () => { - it('returns an error when the name is empty', () => { + it('returns an error when the name is empty', () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - const result = redis.validateSegmentName(''); + const result = redis.validateSegmentName(''); - expect(result).to.be.instanceOf(Error); - expect(result.message).to.equal('Empty string'); - }); + expect(result).to.be.instanceOf(Error); + expect(result.message).to.equal('Empty string'); + }); - it('returns an error when the name has a null character', () => { + it('returns an error when the name has a null character', () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - const result = redis.validateSegmentName('\0test'); + const result = redis.validateSegmentName('\0test'); - expect(result).to.be.instanceOf(Error); - }); + expect(result).to.be.instanceOf(Error); + }); - it('returns null when there aren\'t any errors', () => { + it('returns null when there aren\'t any errors', () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - const result = redis.validateSegmentName('valid'); + const result = redis.validateSegmentName('valid'); - expect(result).to.not.be.instanceOf(Error); - expect(result).to.equal(null); + expect(result).to.not.be.instanceOf(Error); + expect(result).to.equal(null); + }); }); - }); - describe('get()', () => { + describe(`${name}: get()`, () => { - it('returns a promise that rejects when the connection is closed', async () => { + it('returns a promise that rejects when the connection is closed', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - try { - await redis.get('test'); - } - catch (err) { - expect(err.message).to.equal('Connection not started'); - } - }); + try { + await redis.get('test'); + } + catch (err) { + expect(err.message).to.equal('Connection not started'); + } + }); - it('returns a promise that rejects when there is an error returned from getting an item', async () => { + it('returns a promise that rejects when there is an error returned from getting an item', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); - redis.client = { - get: function (item) { + const redis = new CatboxRedis(options); + redis.client = { + get: function (item) { - return Promise.reject(Error()); - } - }; + return Promise.reject(Error()); + } + }; - await expect(redis.get('test')).to.reject(); - }); + await expect(redis.get('test')).to.reject(); + }); - it('returns a promise that rejects when there is an error parsing the result', async () => { + it('returns a promise that rejects when there is an error parsing the result', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); - redis.client = { + const redis = new CatboxRedis(options); + redis.client = { - get: function (item) { + get: function (item) { - return Promise.resolve('test'); - } - }; + return Promise.resolve('test'); + } + }; - await expect(redis.get('test')).to.reject('Bad envelope content'); - }); + await expect(redis.get('test')).to.reject('Bad envelope content'); + }); - it('returns a promise that rejects when there is an error with the envelope structure (stored)', async () => { + it('returns a promise that rejects when there is an error with the envelope structure (stored)', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); - redis.client = { - get: function (item) { + const redis = new CatboxRedis(options); + redis.client = { + get: function (item) { - return Promise.resolve('{ "item": "false" }'); - } - }; + return Promise.resolve('{ "item": "false" }'); + } + }; - await expect(redis.get('test')).to.reject('Incorrect envelope structure'); - }); + await expect(redis.get('test')).to.reject('Incorrect envelope structure'); + }); - it('returns a promise that rejects when there is an error with the envelope structure (item)', async () => { + it('returns a promise that rejects when there is an error with the envelope structure (item)', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); - redis.client = { - get: function (item) { + const redis = new CatboxRedis(options); + redis.client = { + get: function (item) { - return Promise.resolve('{ "stored": "123" }'); - } - }; + return Promise.resolve('{ "stored": "123" }'); + } + }; - await expect(redis.get('test')).to.reject('Incorrect envelope structure'); - }); + await expect(redis.get('test')).to.reject('Incorrect envelope structure'); + }); - it('is able to retrieve an object thats stored when connection is started', async () => { + it('is able to retrieve an object thats stored when connection is started', async () => { - const options = { - host: '127.0.0.1', - port: 6379, - partition: 'wwwtest' - }; - const key = { - id: 'test', - segment: 'test' - }; + const options = { + host: '127.0.0.1', + port: 6379, + partition: 'wwwtest' + }; + const key = { + id: 'test', + segment: 'test' + }; - const redis = new CatboxRedis(options); - await redis.start(); - await redis.set(key, 'myvalue', 200); - const result = await redis.get(key); - expect(result.item).to.equal('myvalue'); - }); + const redis = new CatboxRedis(options); + await redis.start(); + await redis.set(key, 'myvalue', 200); + const result = await redis.get(key); + expect(result.item).to.equal('myvalue'); + }); - it('returns null when unable to find the item', async () => { + it('returns null when unable to find the item', async () => { - const options = { - host: '127.0.0.1', - port: 6379, - partition: 'wwwtest' - }; - const key = { - id: 'notfound', - segment: 'notfound' - }; + const options = { + host: '127.0.0.1', + port: 6379, + partition: 'wwwtest' + }; + const key = { + id: 'notfound', + segment: 'notfound' + }; - const redis = new CatboxRedis(options); - await redis.start(); - const result = await redis.get(key); - expect(result).to.not.exist(); - }); + const redis = new CatboxRedis(options); + await redis.start(); + const result = await redis.get(key); + expect(result).to.not.exist(); + }); - it('can store and retrieve falsy values such as int 0', async () => { + it('can store and retrieve falsy values such as int 0', async () => { - const options = { - host: '127.0.0.1', - port: 6379, - partition: 'wwwtest' - }; - const key = { - id: 'test', - segment: 'test' - }; + const options = { + host: '127.0.0.1', + port: 6379, + partition: 'wwwtest' + }; + const key = { + id: 'test', + segment: 'test' + }; - const redis = new CatboxRedis(options); - await redis.start(); - await redis.set(key, 0, 200); - const result = await redis.get(key); - expect(result.item).to.equal(0); - }); + const redis = new CatboxRedis(options); + await redis.start(); + await redis.set(key, 0, 200); + const result = await redis.get(key); + expect(result.item).to.equal(0); + }); - it('can store and retrieve falsy values such as boolean false', async () => { + it('can store and retrieve falsy values such as boolean false', async () => { - const options = { - host: '127.0.0.1', - port: 6379, - partition: 'wwwtest' - }; - const key = { - id: 'test', - segment: 'test' - }; + const options = { + host: '127.0.0.1', + port: 6379, + partition: 'wwwtest' + }; + const key = { + id: 'test', + segment: 'test' + }; - const redis = new CatboxRedis(options); - await redis.start(); - await redis.set(key, false, 200); - const result = await redis.get(key); - expect(result.item).to.equal(false); + const redis = new CatboxRedis(options); + await redis.start(); + await redis.set(key, false, 200); + const result = await redis.get(key); + expect(result.item).to.equal(false); + }); }); - }); - describe('set()', () => { + describe(`${name}: set()`, () => { - it('returns a promise that rejects when the connection is closed', async () => { + it('returns a promise that rejects when the connection is closed', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - try { - await redis.set('test1', 'test1', 3600); - } - catch (err) { - expect(err.message).to.equal('Connection not started'); - } - }); + try { + await redis.set('test1', 'test1', 3600); + } + catch (err) { + expect(err.message).to.equal('Connection not started'); + } + }); - it('returns a promise that rejects when there is an error returned from setting an item', async () => { + it('returns a promise that rejects when there is an error returned from setting an item', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); - redis.client = { - psetex: function (key, ttls, value) { + const redis = new CatboxRedis(options); + redis.client = { + psetex: function (key, ttls, value) { - return Promise.reject(Error()); - } - }; + return Promise.reject(Error()); + } + }; - await expect(redis.set('test', 'test', 3600)).to.reject(); + await expect(redis.set('test', 'test', 3600)).to.reject(); + }); }); - }); - describe('drop()', () => { + describe(`${name}: drop()`, () => { - it('returns a promise that rejects when the connection is closed', async () => { + it('returns a promise that rejects when the connection is closed', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - try { - await redis.drop('test2'); - } - catch (err) { - expect(err.message).to.equal('Connection not started'); - } - }); + try { + await redis.drop('test2'); + } + catch (err) { + expect(err.message).to.equal('Connection not started'); + } + }); - it('deletes the item from redis', async () => { + it('deletes the item from redis', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); - redis.client = { - del: function (key) { + const redis = new CatboxRedis(options); + redis.client = { + del: function (key) { - return Promise.resolve(null); - } - }; + return Promise.resolve(null); + } + }; - await redis.drop('test'); + await redis.drop('test'); + }); }); - }); - describe('generateKey()', () => { + describe(`${name}: generateKey()`, () => { - it('generates the storage key from a given catbox key', () => { + it('generates the storage key from a given catbox key', () => { - const options = { - partition: 'foo' - }; + const options = { + partition: 'foo' + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - const key = { - id: 'bar', - segment: 'baz' - }; + const key = { + id: 'bar', + segment: 'baz' + }; - expect(redis.generateKey(key)).to.equal('foo:baz:bar'); - }); + expect(redis.generateKey(key)).to.equal('foo:baz:bar'); + }); - it('generates the storage key from a given catbox key without partition', () => { + it('generates the storage key from a given catbox key without partition', () => { - const options = {}; + const options = {}; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - const key = { - id: 'bar', - segment: 'baz' - }; + const key = { + id: 'bar', + segment: 'baz' + }; - expect(redis.generateKey(key)).to.equal('baz:bar'); + expect(redis.generateKey(key)).to.equal('baz:bar'); + }); }); - }); - describe('stop()', () => { + describe(`${name}: stop()`, () => { - it('sets the client to null', async () => { + it('sets the client to null', async () => { - const options = { - host: '127.0.0.1', - port: 6379 - }; + const options = { + host: '127.0.0.1', + port: 6379 + }; - const redis = new CatboxRedis(options); + const redis = new CatboxRedis(options); - await redis.start(); - expect(redis.client).to.exist(); - await redis.stop(); - expect(redis.client).to.not.exist(); + await redis.start(); + expect(redis.client).to.exist(); + await redis.stop(); + expect(redis.client).to.not.exist(); + }); }); - }); + } }); + +