Skip to content

Commit

Permalink
Release 1.0.0 of rcc-ioredis-adapter - Initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron.Dupreez committed Mar 19, 2018
1 parent ed395b9 commit e7199fc
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 33 deletions.
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
"deep-equal": "1.0.1",
"logging-utils": "4.0.22",
"ioredis": "3.2.2",
"rcc-core": "1.0.1"
"rcc-core": "1.0.2"
},
"devDependencies": {
"tape": "^4.9.0",
Expand Down
22 changes: 13 additions & 9 deletions rcc-ioredis-adapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

const isInstanceOf = require('core-functions/objects').isInstanceOf;

const rccAdapters = require('rcc-core');
const rccCore = require('rcc-core');

const IoRedisClient = require('ioredis');
// const Ioredis = IoredisClient.Cluster;
// const ReplyError = Ioredis.ReplyError;
const ReplyError = IoRedisClient.ReplyError;

const DEFAULT_REDIS_HOST = rccAdapters.DEFAULT_REDIS_HOST;
const DEFAULT_REDIS_PORT = rccAdapters.DEFAULT_REDIS_PORT;
const DEFAULT_REDIS_HOST = 'localhost';
const DEFAULT_REDIS_PORT = rccCore.DEFAULT_REDIS_PORT;

exports.getDefaultHost = () => DEFAULT_REDIS_HOST;
exports.getDefaultPort = () => DEFAULT_REDIS_PORT;

exports.createClient = createClient;

Expand All @@ -22,7 +24,8 @@ exports.resolveHostAndPortFromMovedError = resolveHostAndPortFromMovedError;

/**
* Creates a new RedisClient instance.
* @param {RedisClientOptions|} redisClientOptions - the options to use to construct the new RedisClient instance
* @param {RedisClientOptions|undefined} [redisClientOptions] - the options to use to construct the new Redis client
* instance
* @return {RedisClient} returns the new RedisClient instance
*/
function createClient(redisClientOptions) {
Expand Down Expand Up @@ -71,13 +74,14 @@ function adaptClient(client) {
}

if (!client.addEventListeners) {
client.addEventListeners = function (onConnect, onReady, onReconnecting, onError, onClientError, onEnd) {
client.addEventListeners = function (onConnect, onReady, onReconnecting, onError, onClientError, onEnd, onClose) {
if (typeof onConnect === 'function') this.on('connect', onConnect);
if (typeof onReady === 'function') this.on('ready', onReady);
if (typeof onReconnecting === 'function') this.on('reconnecting', onReconnecting);
if (typeof onError === 'function') this.on('error', onError);
if (typeof onClientError === 'function') this.on('clientError', onClientError);
if (typeof onEnd === 'function') this.on('end', onEnd);
if (typeof onClose === 'function') this.on('close', onClose);
}
}

Expand Down Expand Up @@ -114,7 +118,7 @@ function deleteClientFunction(fnName) {
*/
function isMovedError(error) {
// Check if error message contains something like: "MOVED 14190 127.0.0.1:6379"
return /*!!isInstanceOf(error, ReplyError) &&*/ error.code === 'MOVED';
return !!isInstanceOf(error, ReplyError) && error.message && error.message.startsWith('MOVED ');
}

/**
Expand All @@ -128,5 +132,5 @@ function resolveHostAndPortFromMovedError(movedError) {
if (isMovedError(movedError)) {
return movedError.message.substring(movedError.message.lastIndexOf(' ') + 1).split(':');
}
throw new Error(`Unexpected ioredis client "moved" error - ${movedError}`);
throw new Error(`Unexpected ioredis client "moved" ReplyError - ${movedError}`);
}
3 changes: 2 additions & 1 deletion release_notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
## Changes

### 1.0.0
- Initial version
- Initial version
- Added `getDefaultHost` and `getDefaultPort` functions to `rcc-ioredis-adapter` module
41 changes: 22 additions & 19 deletions test/rcc-ioredis-adapter.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ const test = require('tape');
// Get the redis adapter
const redis = require('../rcc-ioredis-adapter');

const host0 = 'localhost';
const port0 = 6379;
const host0 = redis.getDefaultHost();
const port0 = redis.getDefaultPort();

const host1 = '127.0.0.1';
const port1 = 9999;

function addEventListeners(redisClient, desc, customOnError) {
function addEventListeners(redisClient, desc, startMs, customOnError) {
const [host, port] = redisClient.resolveHostAndPort();

const onConnect = () => {
Expand All @@ -27,47 +27,49 @@ function addEventListeners(redisClient, desc, customOnError) {
};

const onError = err => {
console.log(`*** Redis client ${desc} connection to host (${host}) & port (${port}) hit ERROR ${err}`);
console.log(`*** Redis client ${desc} connection to host (${host}) & port (${port}) failed with ERROR - ${err}`);
if (customOnError) {
return customOnError(err);
}
console.error(err);
redisClient.end(true, () => {});
};

// Not supported by `ioredis` module
const onClientError = err => {
console.log(`*** Redis client ${desc} connection to host (${host}) & port (${port}) hit CLIENT ERROR ${err}`);
console.log(`*** Redis client ${desc} connection to host (${host}) & port (${port}) failed with CLIENT ERROR - ${err}`);
console.error(err);
};

const onEnd = () => {
console.log(`*** Redis client ${desc} connection to host (${host}) & port (${port}) has ENDED`);
};

const onClose = () => {
console.log(`*** Redis client ${desc} connection to host (${host}) & port (${port}) has CLOSED`);
};

redisClient.addEventListeners(onConnect, onReady, onReconnecting, onError, onClientError, onEnd);
redisClient.addEventListeners(onConnect, onReady, onReconnecting, onError, onClientError, onEnd, onClose);
}

test('createClient', t => {
test('ioredis - createClient', t => {
// Create a redis client using the redis adapter
let startMs = Date.now();
const redisClient0 = redis.createClient();
t.ok(redisClient0, `redisClient0 must exist`);
t.notOk(redisClient0.isClosing(), `redisClient0 should not be closing yet`);

// console.log(`### redisClient0 = ${JSON.stringify(redisClient0)}`);

let [h, p] = redisClient0.resolveHostAndPort();
t.equal(h, host0, `redisClient0 host must be ${host0}`);
t.equal(p, port0, `redisClient0 port must be ${port0}`);

addEventListeners(redisClient0, 0, err => {
addEventListeners(redisClient0, 0, startMs, err => {
t.pass(`Expected and got an error from redisClient0 (${err})`);
redisClient0.end(true, () => {});

// console.log(`### redisClient0 = ${JSON.stringify(redisClient0)}`);

t.ok(redisClient0.isClosing(), `redisClient0 should be closing now`);
});

startMs = Date.now();
const redisClient1 = redis.createClient({port: port0, host: host0});
t.ok(redisClient1, `redisClient1 must exist`);
t.notOk(redisClient1.isClosing(), `redisClient1 should not be closing yet`);
Expand All @@ -76,12 +78,13 @@ test('createClient', t => {
t.equal(h, host0, `redisClient1 host must be ${host0}`);
t.equal(p, port0, `redisClient1 port must be ${port0}`);

addEventListeners(redisClient1, 1, err => {
addEventListeners(redisClient1, 1, startMs, err => {
t.pass(`Expected and got an error from redisClient1 (${err})`);
redisClient1.end(true, () => {});
t.ok(redisClient1.isClosing(), `redisClient1 should be closing now`);
});

startMs = Date.now();
const redisClientOptions2 = {host: host1, port: port1, string_number: true};
const redisClient2 = redis.createClient(redisClientOptions2);

Expand All @@ -93,18 +96,18 @@ test('createClient', t => {
t.equal(p, port1, `redisClient2 port must be ${port1}`);

// Set and get a value for a key using the underlying `redis` module's `RedisClient` instance's methods
const key = 'KEY';
const expectedValue = 'VALUE';
const key = 'TEST_KEY';
const expectedValue = 'TEST_VALUE';

addEventListeners(redisClient2, 2, err => {
addEventListeners(redisClient2, 2, startMs, err => {
t.pass(`Expected and got an error from redisClient2 (${err})`);
redisClient2.end(true, () => {});
t.ok(redisClient2.isClosing(), `redisClient2 should be closing now`);
});

redisClient2.set(key, expectedValue, (err,) => {
if (err) {
t.pass(`Expected and got a set error (${err})`);
t.pass(`Expected and got a set error (${err}) - ${JSON.stringify(err)} - isMovedError? ${redisClient0.getAdapter().isMovedError(err)}`);
redisClient0.end(true);
redisClient1.end(true);
redisClient2.end(true);
Expand All @@ -118,7 +121,7 @@ test('createClient', t => {
if (err) {
t.pass(`Expected and got a get error (${err})`);
} else {
t.equal(value, 'VALUE', `value must be '${expectedValue}'`);
t.equal(value, expectedValue, `value must be '${expectedValue}'`);
}
redisClient0.end(true);
redisClient1.end(true);
Expand Down

0 comments on commit e7199fc

Please sign in to comment.