Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache tests, Fix response processing header order #211

Merged
merged 2 commits into from
Nov 2, 2012
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 42 additions & 37 deletions lib/cache/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,32 @@ exports.Client = internals.Client = function (options) {

// Create internal connection

var implementation = null;
if (this.settings.engine === 'redis') {
implementation = Redis;
}
else if (this.settings.engine === 'mongodb') {
implementation = Mongo;
}
else if (this.settings.engine === 'memory') {
implementation = Memory;
var engine = self.settings.engine;
if (typeof engine === 'object') {
this.connection = engine;
engine = 'extension';
}
else {
var factory = null;

if (engine === 'redis') {
factory = Redis;
}
else if (engine === 'mongodb') {
factory = Mongo;
}
else if (engine === 'memory') {
factory = Memory;
}

Utils.assert(implementation, 'Unknown cache engine type');
Utils.assert(factory, 'Unknown cache engine type');
this.connection = new factory.Connection(this.settings);
}

this.connection = new implementation.Connection(this.settings);
this.connection.start(function (err) {

if (err) {
Log.event(['cache', 'error', self.settings.engine], 'Failed initializing cache engine');
Log.event(['cache', 'error', engine], 'Failed initializing cache engine');
}
});

Expand All @@ -54,25 +62,23 @@ exports.Client = internals.Client = function (options) {

internals.Client.prototype.stop = function () {

if (this.connection) {
this.connection.stop();
}
this.connection.stop();
};


internals.Client.prototype.start = function (callback) {

if (this.connection) {
this.connection.start(callback);
}
this.connection.start(callback);
};


internals.Client.prototype.validateSegmentName = function (name) {
internals.Client.prototype.isReady = function () {

if (!this.connection) {
return new Error('Disconnected');
}
return this.connection.isReady();
};


internals.Client.prototype.validateSegmentName = function (name) {

return this.connection.validateSegmentName(name);
};
Expand All @@ -82,17 +88,16 @@ internals.Client.prototype.get = function (key, callback) {

var self = this;

if (!this.connection.isReady()) {
// Disconnected
return callback(new Error('Disconnected'));
}

if (key === null) {
// null key not allowed
return callback(null, null);
}

if (!this.connection) {
// Disconnected
return callback(new Error('Disconnected'));
}

this.connection.get(key, function (err, result) {

if (err) {
Expand Down Expand Up @@ -130,16 +135,16 @@ internals.Client.prototype.get = function (key, callback) {

internals.Client.prototype.set = function (key, value, ttl, callback) {

if (!this.connection.isReady()) {
// Disconnected
return callback(new Error('Disconnected'));
}

if (key === null) {
// null key not allowed
return callback(new Error('null key not allowed'));
}

if (!this.connection) {
// Disconnected
return callback(new Error('Disconnected'));
}

if (ttl <= 0) {
// Not cachable (or bad rules)
return callback();
Expand All @@ -151,16 +156,16 @@ internals.Client.prototype.set = function (key, value, ttl, callback) {

internals.Client.prototype.drop = function (key, callback) {

if (!this.connection.isReady()) {
// Disconnected
return callback(new Error('Disconnected'));
}

if (key === null) {
// null key not allowed
return callback(new Error('null key not allowed'));
}

if (!this.connection) {
// Disconnected
return callback(new Error('Disconnected'));
}

this.connection.drop(key, callback); // Always drop, regardless of caching rules
};

Expand Down
11 changes: 8 additions & 3 deletions lib/cache/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,10 @@ exports.Connection = internals.Connection = function (options) {

internals.Connection.prototype.start = function (callback) {

if (this.cache) {
return callback(new Error('Connection already established'));
if (!this.cache) {
this.cache = {};
}

this.cache = {};
return callback();
};

Expand All @@ -36,6 +35,12 @@ internals.Connection.prototype.stop = function () {
};


internals.Connection.prototype.isReady = function () {

return (!!this.cache);
};


internals.Connection.prototype.validateSegmentName = function (name) {

if (!name) {
Expand Down
78 changes: 57 additions & 21 deletions lib/cache/mongo.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,9 @@ exports.Connection = internals.Connection = function (options) {

this.settings = options;
this.client = null;
this.isReady = false;
this.isConnected = false;
this.collections = {};
this.startPending = null; // Set to an array of callbacks if start pending
return this;
};

Expand All @@ -39,41 +40,87 @@ internals.Connection.prototype.start = function (callback) {

var self = this;

if (this.client) {
return callback(new Error('Connection already established'));
// Check if already connected

if (this.isConnected) {

return callback();
}

// Check if start already pending

if (this.startPending) {
this.startPending.push(callback);
return;
}

// Set start pending state

this.startPending = [callback];

var connected = function (err) {

self.isConnected = !err;

for (var i = 0, il = self.startPending.length; i < il; ++i) {
self.startPending[i](err);
}

self.startPending = null;
};

// Create client

var server = new MongoDB.Server(this.settings.host, this.settings.port, { auto_reconnect: true, poolSize: this.settings.poolSize });
this.client = new MongoDB.Db(this.settings.partition, server, { safe: true });

// Open connection

this.client.open(function (err, client) {

if (err) {
return callback(new Error('Failed opening connection'));
return connected(new Error('Failed opening connection'));
}

// Authenticate

if (self.settings.username) {
self.client.authenticate(self.settings.username, self.settings.password, function (err, result) {

if (err ||
!result) {

self.stop();
return callback(new Error('Database authentication error: ' + (err ? JSON.stringify(err) : 'failed')));
return connected(new Error('Database authentication error: ' + (err ? JSON.stringify(err) : 'failed')));
}

self.isReady = true;
return callback();
return connected();
});
}
else {
self.isReady = true;
return callback();
return connected();
}
});
};


internals.Connection.prototype.stop = function () {

if (this.client) {
this.client.close();
this.client = null;
this.collections = {};
this.isConnected = false;
}
};


internals.Connection.prototype.isReady = function () {

return this.isConnected;
};


internals.Connection.prototype.validateSegmentName = function (name) {

/*
Expand Down Expand Up @@ -114,7 +161,7 @@ internals.Connection.prototype.getCollection = function (name, callback) {

var self = this;

if (!this.isReady) {
if (!this.isConnected) {
return callback(new Error('Connection not ready'));
}

Expand Down Expand Up @@ -142,17 +189,6 @@ internals.Connection.prototype.getCollection = function (name, callback) {
};


internals.Connection.prototype.stop = function () {

if (this.client) {
this.client.close();
this.client = null;
this.collections = {};
this.isReady = false;
}
};


internals.Connection.prototype.get = function (key, callback) {

if (!this.client) {
Expand Down
8 changes: 7 additions & 1 deletion lib/cache/redis.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ exports.Connection = internals.Connection = function (options) {
internals.Connection.prototype.start = function (callback) {

if (this.client) {
return callback(new Error('Connection already established'));
return callback();
}

this.client = Redis.createClient(this.settings.port, this.settings.host);
Expand Down Expand Up @@ -60,6 +60,12 @@ internals.Connection.prototype.stop = function () {
};


internals.Connection.prototype.isReady = function () {

return (!!this.client);
};


internals.Connection.prototype.validateSegmentName = function (name) {

if (!name) {
Expand Down
13 changes: 7 additions & 6 deletions lib/log.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@ NodeUtil.inherits(internals.Logger, Events.EventEmitter);

internals.Logger.prototype.event = function (tags, data, timestamp) {

if (process.env.NODE_ENV === 'test') {

return; // Silence log output during test execution
}

tags = (tags instanceof Array ? tags : [tags]);
var now = (timestamp ? (timestamp instanceof Date ? timestamp : new Date(timestamp)) : new Date());

Expand All @@ -43,7 +38,13 @@ internals.Logger.prototype.event = function (tags, data, timestamp) {
};


internals.Logger.prototype.print = function (event, _isBypass) {
internals.Logger.prototype.print = function (event, _isBypass, _isTest) {

if (process.env.NODE_ENV === 'test' &&
!_isTest) {

return; // Silence log output during test execution
}

var pad = function (value) {

Expand Down
Loading