UNPKG

node-cache-redis

Version:

Simplistic node redis cache ready can scale with generic-pool support

362 lines (361 loc) 18.3 kB
"use strict"; var __assign = (this && this.__assign) || function () { __assign = Object.assign || function(t) { for (var s, i = 1, n = arguments.length; i < n; i++) { s = arguments[i]; for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p]; } return t; }; return __assign.apply(this, arguments); }; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); var RedisConnectionPool_1 = __importDefault(require("./RedisConnectionPool")); describe('redisConnectionPool', function () { var name = 'testPool'; var redisOptions = { host: process.env.REDIS_HOST || '127.0.0.1', auth_pass: process.env.REDIS_AUTH }; var poolOptions = { min: 2, max: 4 }; var options = { name: name, redisOptions: redisOptions, poolOptions: poolOptions }; var pool; beforeEach(function () { pool = new RedisConnectionPool_1.default(options); }); describe('constructor', function () { test('set db when sent as constructor option', function () { return __awaiter(void 0, void 0, void 0, function () { var db, localPool, client; return __generator(this, function (_a) { switch (_a.label) { case 0: db = 1; localPool = new RedisConnectionPool_1.default(__assign(__assign({}, options), { redisOptions: __assign(__assign({}, redisOptions), { db: db }) })); return [4 /*yield*/, localPool.acquire() // @ts-ignore ]; case 1: client = _a.sent(); // @ts-ignore expect(client.selected_db).toBe(db); return [2 /*return*/]; } }); }); }); }); describe('getName', function () { test('set given name', function () { expect(pool.getName()).toBe(name); }); test('set random name if not set', function () { var poolUnNamed = new RedisConnectionPool_1.default(options); expect(poolUnNamed.getName()).not.toHaveLength(0); }); }); describe('getRedisOptions', function () { test('set given redis options', function () { expect(pool.getRedisOptions()).toBe(redisOptions); }); }); describe('getPoolOptions', function () { test('set given pool options', function () { expect(pool.getPoolOptions()).toBe(poolOptions); }); }); describe('status', function () { test('get pool stats', function () { var status = pool.status(); expect(status.name).toBe(name); expect(status.size).toBe(poolOptions.min); expect(status.available).toBe(0); expect(status.pending).toBe(0); }); }); describe('acquire', function () { test('acquire connection with valid host', function () { return __awaiter(void 0, void 0, void 0, function () { var client; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, pool.acquire()]; case 1: client = _a.sent(); expect(client).toBeTruthy(); return [2 /*return*/]; } }); }); }); test('acquire connection to db when set', function () { return __awaiter(void 0, void 0, void 0, function () { var db, client; return __generator(this, function (_a) { switch (_a.label) { case 0: db = 1; return [4 /*yield*/, pool.acquire(0, db) // @ts-ignore ]; case 1: client = _a.sent(); // @ts-ignore expect(client.selected_db).toBe(db); return [2 /*return*/]; } }); }); }); test('wait to acquire if all used up', function () { return __awaiter(void 0, void 0, void 0, function () { var localPoolOptions, localPool, client; return __generator(this, function (_a) { switch (_a.label) { case 0: localPoolOptions = { min: 0, max: 1 }; localPool = new RedisConnectionPool_1.default(__assign(__assign({}, options), { poolOptions: localPoolOptions })); expect(localPool.availableCount()).toBe(localPoolOptions.min); expect(localPool.getPoolSize()).toBe(localPoolOptions.min); expect(localPool.pendingCount()).toBe(0); return [4 /*yield*/, localPool.acquire()]; case 1: client = _a.sent(); expect(localPool.availableCount()).toBe(localPoolOptions.min); expect(localPool.getPoolSize()).toBe(1); expect(localPool.pendingCount()).toBe(0); return [4 /*yield*/, localPool.release(client)]; case 2: _a.sent(); expect(localPool.availableCount()).toBe(1); return [4 /*yield*/, localPool.acquire()]; case 3: _a.sent(); expect(localPool.availableCount()).toBe(0); expect(localPool.getPoolSize()).toBe(1); expect(localPool.pendingCount()).toBe(0); // eslint-disable-next-line @typescript-eslint/no-floating-promises localPool.acquire(); // this is hanging op so no return expect(localPool.availableCount()).toBe(0); expect(localPool.getPoolSize()).toBe(1); expect(localPool.pendingCount()).toBe(1); return [2 /*return*/]; } }); }); }); test('not fail with many higher min connections', function () { return __awaiter(void 0, void 0, void 0, function () { var localPool, client; return __generator(this, function (_a) { switch (_a.label) { case 0: localPool = new RedisConnectionPool_1.default(__assign(__assign({}, options), { poolOptions: { min: 5, max: 10 } })); return [4 /*yield*/, localPool.acquire()]; case 1: client = _a.sent(); expect(client).toBeTruthy(); return [2 /*return*/]; } }); }); }); test('invalid host fail acquire connection', function () { return __awaiter(void 0, void 0, void 0, function () { var localPool; return __generator(this, function (_a) { switch (_a.label) { case 0: localPool = new RedisConnectionPool_1.default(__assign(__assign({}, options), { redisOptions: __assign(__assign({}, redisOptions), { host: 'UNAVAILABLE_HOST' }) })); return [4 /*yield*/, expect(localPool.acquire()).rejects.toThrowError('Failed redis createClient, {"host":"UNAVAILABLE_HOST"}')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); test('conn timeout fail acquire connection', function () { return __awaiter(void 0, void 0, void 0, function () { var localPool; return __generator(this, function (_a) { switch (_a.label) { case 0: localPool = new RedisConnectionPool_1.default(__assign(__assign({}, options), { poolOptions: { min: 1, acquireTimeoutMillis: 1 } })); // make the conn is in-use return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, 300); })]; case 1: // make the conn is in-use _a.sent(); return [4 /*yield*/, localPool.acquire()]; case 2: _a.sent(); return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, 1500); })]; case 3: _a.sent(); return [4 /*yield*/, expect(function () { return localPool.acquire(); }).rejects.toThrowError('ResourceRequest timed out')]; case 4: _a.sent(); return [2 /*return*/]; } }); }); }); }); describe('release', function () { test('release connection with valid host', function () { return __awaiter(void 0, void 0, void 0, function () { var localPool, client; return __generator(this, function (_a) { switch (_a.label) { case 0: localPool = new RedisConnectionPool_1.default(options); return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, 300); })]; case 1: _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min); expect(localPool.getPoolSize()).toBe(poolOptions.min); expect(localPool.pendingCount()).toBe(0); return [4 /*yield*/, localPool.acquire()]; case 2: client = _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min - 1); return [4 /*yield*/, localPool.release(client)]; case 3: _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min); return [2 /*return*/]; } }); }); }); test('release connection with invalid host', function () { return __awaiter(void 0, void 0, void 0, function () { var localPool; return __generator(this, function (_a) { switch (_a.label) { case 0: localPool = new RedisConnectionPool_1.default(__assign(__assign({}, options), { redisOptions: __assign(__assign({}, redisOptions), { host: 'UNAVAILABLE_HOST' }) })); return [4 /*yield*/, expect(localPool.release()).rejects.toThrowError('Resource not currently part of this pool')]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); }); describe('destroy', function () { test('destroy connection with valid host', function () { return __awaiter(void 0, void 0, void 0, function () { var localPool, client; return __generator(this, function (_a) { switch (_a.label) { case 0: localPool = new RedisConnectionPool_1.default(options); return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, 300); })]; case 1: _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min); expect(localPool.getPoolSize()).toBe(poolOptions.min); expect(localPool.pendingCount()).toBe(0); return [4 /*yield*/, localPool.acquire()]; case 2: client = _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min - 1); return [4 /*yield*/, localPool.destroy(client)]; case 3: _a.sent(); return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, 300); })]; case 4: _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min); return [2 /*return*/]; } }); }); }); }); describe('drain', function () { test('drain all the coonections', function () { return __awaiter(void 0, void 0, void 0, function () { var localPool, client; return __generator(this, function (_a) { switch (_a.label) { case 0: localPool = new RedisConnectionPool_1.default(options); return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, 300); })]; case 1: _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min); expect(localPool.getPoolSize()).toBe(poolOptions.min); expect(localPool.pendingCount()).toBe(0); return [4 /*yield*/, localPool.acquire()]; case 2: client = _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min - 1); return [4 /*yield*/, localPool.destroy(client)]; case 3: _a.sent(); return [4 /*yield*/, localPool.drain()]; case 4: _a.sent(); return [4 /*yield*/, new Promise(function (r) { return setTimeout(r, 300); })]; case 5: _a.sent(); expect(localPool.availableCount()).toBe(poolOptions.min); expect(localPool.getPoolSize()).toBe(0); return [2 /*return*/]; } }); }); }); }); describe('sendCommand', function () { var key = 'MyNameIs'; var value = 'RealSlimShady'; beforeEach(function () { return pool.sendCommand('del', ['*']); }); test('execute given command', function () { return __awaiter(void 0, void 0, void 0, function () { var result; return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, pool.sendCommand('set', [key, value])]; case 1: _a.sent(); return [4 /*yield*/, pool.sendCommand('get', [key])]; case 2: result = _a.sent(); return [2 /*return*/, expect(result).toBe(value)]; } }); }); }); test('reject when cmd failed', function () { return __awaiter(void 0, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, expect(pool.sendCommand('keys')).rejects.toThrowError(/ERR wrong number of arguments for 'keys' command/)]; case 1: _a.sent(); return [2 /*return*/]; } }); }); }); }); });