Skip to content

Commit

Permalink
added initial structure and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Tokumine committed Aug 9, 2011
0 parents commit d53fc13
Show file tree
Hide file tree
Showing 6 changed files with 191 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/grainstore/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
MMLStore: require('./mml_store')
};
12 changes: 12 additions & 0 deletions lib/grainstore/mml_store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
var RedisPool = require('./redis_pool');

var MMLStore = function(){
me = {};


return me;
}();



module.exports = MMLStore;
73 changes: 73 additions & 0 deletions lib/grainstore/redis_pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var crypto = require('crypto')
, redis = require('redis')
, _ = require('underscore')
, Pool = require('generic-pool').Pool;

// RedisPool constructor.
//
// - `opts` {Object} optional config for redis and pooling
var RedisPool = function(opts){
var opts = opts || {};
var defaults = {
host: '127.0.0.1',
port: '6379',
max: 50,
idleTimeoutMillis: 10000,
reapIntervalMillis: 1000,
log: false
};
var options = _.defaults(opts, defaults)

var me = {
pools: {} // cached pools by DB name
};

// Acquire resource.
//
// - `database` {String} redis database name
// - `callback` {Function} callback to call once acquired. Takes the form
// `callback(err, resource)`
me.acquire = function(database, callback) {
if (!this.pools[database]) {
this.pools[database] = this.makePool(database);
}
this.pools[database].acquire(function(resource) {
callback(resource);
});
};

// Release resource.
//
// - `database` {String} redis database name
// - `resource` {Object} resource object to release
me.release = function(database, resource) {
this.pools[database] && this.pools[database].release(resource);
};

// Factory for pool objects.
me.makePool = function(database) {
return Pool({
name: database,
create: function(callback){
var client = redis.createClient(options.port, options.host);
client.on('connect', function () {
client.send_anyway = true;
client.select(database);
client.send_anyway = false;
});
return callback(client);
},
destroy: function(client) {
return client.quit();
},
max: options.max,
idleTimeoutMillis: options.idleTimeoutMillis,
reapIntervalMillis: options.reapIntervalMillis,
log: options.log
});
};

return me;
}

module.exports = RedisPool;
31 changes: 31 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
{
"name": "grainstore",
"version": "0.0.1",
"main": "./lib/grainstore/index.js",
"description": "Stores map styles and generates postgis friendly MML & XML for Mapnik",
"url": "https://github.com/tokumine/grainstore",
"licenses": [{ "type": "MIT" }],
"repositories": [{
"type": "git",
"url": "git://github.com/tokumine/grainstore.git"
}],
"author": {
"name": "Simon Tokumine",
"url": "http://tokumine.com/",
"email": "si@tinypla.net"
},
"dependencies": {
"underscore" : "1.1.x",
"step": "0.0.x",
"generic-pool": "1.0.x",
"carto": "0.2.3",
"redis": "0.6.7",
"hiredis": "0.1.12"
},
"devDependencies": {
"expresso": "0.8.x"
},
"scripts": {
"test": "which expresso | sh"
}
}
11 changes: 11 additions & 0 deletions test/mml_store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
var fs = require('fs');
var path = require('path');
var assert = require('assert');
var grainstore = require('../lib/grainstore');
var tests = module.exports = {};


tests['true'] = function() {
assert.ok(true);
}

61 changes: 61 additions & 0 deletions test/redis_pool.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
var assert = require('assert')
, _ = require('underscore')
, RedisPool = require('../lib/grainstore/redis_pool')
, tests = module.exports = {};

// configure redis pool instance to use in tests
var test_opts = {
max: 10,
idleTimeoutMillis: 1,
reapIntervalMillis: 1,
}

var redis_pool = new RedisPool(test_opts);

tests['truth'] = function(){
assert.ok(true, 'it is');
};

tests['RedisPool object exists'] = function(){
assert.ok(RedisPool);
}

tests['RedisPool can create new redis_pool objects with default settings'] = function(){
var redis_pool = new RedisPool();
};

tests['RedisPool can create new redis_pool objects with specific settings'] = function(){
var redis_pool = new RedisPool(_.extend({host:'127.0.0.1', port: '6379'}, test_opts));
};


tests['pool object has an aquire function'] = function(){
assert.includes(_.functions(redis_pool), 'acquire');
}

tests['calling aquire returns a redis client object that can get/set'] = function(){
redis_pool.acquire(0, function(client){
client.set("key","value")
client.get("key", function(err,data){
assert.eql(data, "value");
redis_pool.release(0, client); // needed to exit tests
})
});
}

tests['calling aquire on another DB returns a redis client object that can get/set'] = function(){
redis_pool.acquire(2, function(client){
client.set("key","value")
client.get("key", function(err,data){
assert.eql(data, "value");
redis_pool.release(2, client); // needed to exit tests
})
});

redis_pool.acquire(2, function(client){
client.get("key", function(err,data){
assert.eql(data, "value");
redis_pool.release(2, client); // needed to exit tests
})
});
}

0 comments on commit d53fc13

Please sign in to comment.