commit 629241611bb70bd9a1bd56cf086f8d5f6814f668 Author: Henry Oswald Date: Thu Sep 25 17:33:27 2014 +0100 v1 of the redis driver wrapper diff --git a/libraries/redis-wrapper/.gitignore b/libraries/redis-wrapper/.gitignore new file mode 100644 index 0000000000..cb03337081 --- /dev/null +++ b/libraries/redis-wrapper/.gitignore @@ -0,0 +1,11 @@ +**.swp + +app.js +app/js/ +test/unit/js/ +public/build/ + +node_modules/ + +/public/js/chat.js +plato/ diff --git a/libraries/redis-wrapper/Gruntfile.coffee b/libraries/redis-wrapper/Gruntfile.coffee new file mode 100644 index 0000000000..59bb3d34b6 --- /dev/null +++ b/libraries/redis-wrapper/Gruntfile.coffee @@ -0,0 +1,35 @@ +module.exports = (grunt) -> + + # Project configuration. + grunt.initConfig + coffee: + server: + expand: true, + flatten: false, + cwd: 'app/coffee', + src: ['**/*.coffee'], + dest: 'app/js/', + ext: '.js' + + server_tests: + expand: true, + flatten: false, + cwd: 'test/unit/coffee', + src: ['**/*.coffee'], + dest: 'test/unit/js/', + ext: '.js' + + mochaTest: + unit: + options: + reporter: process.env.MOCHA_RUNNER || "spec" + grep: grunt.option("grep") + require: 'coffee-script/register' + src: ['test.coffee'] + + + grunt.loadNpmTasks 'grunt-contrib-coffee' + grunt.loadNpmTasks 'grunt-mocha-test' + + grunt.registerTask 'test:unit', ['mochaTest:unit'] + diff --git a/libraries/redis-wrapper/index.coffee b/libraries/redis-wrapper/index.coffee new file mode 100644 index 0000000000..a1154ce9c7 --- /dev/null +++ b/libraries/redis-wrapper/index.coffee @@ -0,0 +1,10 @@ +module.exports = + + createClient: ()-> + if arguments[0] instanceof Array + client = require("redis-sentinel").createClient.apply null, arguments + else + client = require("redis").createClient.apply null, arguments + return client + + diff --git a/libraries/redis-wrapper/package.json b/libraries/redis-wrapper/package.json new file mode 100644 index 0000000000..7209fde4ba --- /dev/null +++ b/libraries/redis-wrapper/package.json @@ -0,0 +1,23 @@ +{ + "name": "redis-sharelatex", + "version": "0.0.0", + "description": "", + "main": "index.coffee", + "scripts": { + "test": "echo \"Error: no test specified\" && exit 1" + }, + "author": "", + "license": "ISC", + "dependencies": { + "chai": "^1.9.1", + "coffee-script": "^1.8.0", + "grunt": "^0.4.5", + "grunt-contrib-coffee": "^0.11.1", + "grunt-mocha-test": "^0.12.0", + "mocha": "^1.21.4", + "redis": "^0.12.1", + "redis-sentinel": "^0.1.1", + "sandboxed-module": "^1.0.1", + "sinon": "^1.10.3" + } +} diff --git a/libraries/redis-wrapper/test.coffee b/libraries/redis-wrapper/test.coffee new file mode 100644 index 0000000000..8fdaa5331e --- /dev/null +++ b/libraries/redis-wrapper/test.coffee @@ -0,0 +1,65 @@ +should = require('chai').should() +SandboxedModule = require('sandboxed-module') +assert = require('assert') +path = require('path') +sinon = require('sinon') +modulePath = path.join __dirname, "./index.coffee" +expect = require("chai").expect + +describe "index", -> + + beforeEach -> + + @settings = {} + @sentinelClient = set:-> + @normalRedisClient = get: -> + + @sentinel = + createClient: sinon.stub().returns(@sentinelClient) + @normalRedis = + createClient: sinon.stub().returns(@normalRedisClient) + @redis = SandboxedModule.require modulePath, requires: + "redis-sentinel":@sentinel + "redis":@normalRedis + @standardOpts = + auth_pass: "my password" + + describe "sentinel", -> + + beforeEach -> + @endpoints = [ + {host: '127.0.0.1', port: 26379}, + {host: '127.0.0.1', port: 26380} + ] + @masterName = "my master" + + it "should use sentinal if the first argument in an array", -> + + client = @redis.createClient @endpoints, @masterName, @standardOpts + @sentinel.createClient.called.should.equal true + @normalRedis.createClient.called.should.equal false + client.should.equal @sentinelClient + + it "should pass the options correctly though", -> + client = @redis.createClient @endpoints, @masterName, @standardOpts + @sentinel.createClient.calledWith(@endpoints, @masterName, @standardOpts).should.equal true + client.should.equal @sentinelClient + + describe "normal redis", -> + + beforeEach -> + @port = 1234 + @host = "redis.mysite.env" + + it "should use the normal redis driver if a non array is passed", -> + + client = @redis.createClient @port, @host, @standardOpts + @sentinel.createClient.called.should.equal false + @normalRedis.createClient.called.should.equal true + client.should.equal @normalRedisClient + + + it "should use the normal redis driver if a non array is passed", -> + + client = @redis.createClient @port, @host, @standardOpts + @normalRedis.createClient.calledWith(@port, @host, @standardOpts).should.equal true