Skip to content

Commit

Permalink
use co-mocha and generators instead of co for tests (see comment in k…
Browse files Browse the repository at this point in the history
  • Loading branch information
DaAwesomeP committed Jan 8, 2016
1 parent a64752f commit 8837c62
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 87 deletions.
8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@
"redis": "2.4.*"
},
"devDependencies": {
"mocha": "*",
"autod": "*",
"co-mocha": "*",
"istanbul": "*",
"co": "4.6.*",
"should": "*",
"autod": "*"
"mocha": "*",
"should": "*"
},
"optionalDependencies": {
"hiredis": "0.4.*"
Expand Down
140 changes: 57 additions & 83 deletions test/koa-redis.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,119 +14,93 @@
*/

var should = require('should');
var co = require('co');
require('co-mocha');
var redis = require('redis');
var redisWrapper = require('co-redis');

function event(object, name) { // Convert events to promises
return new Promise(function(resolve) {
object.once(name, resolve);
});
}

describe('test/koa-redis.test.js', function () {
it('should connect and ready with external client and quit ok', function (done) {
it('should connect and ready with external client and quit ok', function* () {
var store = require('../')({client: redis.createClient()});
store.once('connect', function() {
store.connected.should.eql(true);
store.once('ready', function() {
co.wrap(function *() {
yield store.quit();
})();
});
store.once('end', function() {
store.connected.should.eql(false);
done();
});
});
yield event(store, 'connect');
store.connected.should.eql(true);
yield event(store, 'ready');
yield store.quit();
yield event(store, 'end');
store.connected.should.eql(false);
});

it('should connect and ready with duplicated external client and disconnect ok', function (done) {
it('should connect and ready with duplicated external client and disconnect ok', function* () {
var store = require('../')({
client: redis.createClient(),
duplicate: true
});
store.once('connect', function() {
store.connected.should.eql(true);
store.on('ready', function() {
co.wrap(function *() {
yield store.end();
})();
});
store.once('disconnect', function() {
store.connected.should.eql(false);
done();
});
});
yield event(store, 'connect');
store.connected.should.eql(true);
yield event(store, 'ready');
yield store.end()
yield event(store, 'disconnect');
store.connected.should.eql(false);
});

it('should set with db ok', function (done) {
it('should set with db ok', function* () {
var store = require('../')({db: 2});
var client = redis.createClient();
client.select(2);
client = redisWrapper(client);
co.wrap(function *() {
yield store.set('key:db1', {a: 1});
(yield store.get('key:db1')).should.eql({a: 1});
JSON.parse(yield client.get('key:db1')).should.eql({a: 1});
yield store.quit();
done();
})();
yield store.set('key:db1', {a: 1});
(yield store.get('key:db1')).should.eql({a: 1});
JSON.parse(yield client.get('key:db1')).should.eql({a: 1});
yield store.quit();
});

it('should set with ttl ok', function (done) {
it('should set with ttl ok', function* () {
var store = require('../')();
co.wrap(function *() {
yield store.set('key:ttl', {a: 1}, 86400000);
(yield store.get('key:ttl')).should.eql({a: 1});
(yield store.client.ttl('key:ttl')).should.equal(86400);
yield store.quit();
done();
})();
yield store.set('key:ttl', {a: 1}, 86400000);
(yield store.get('key:ttl')).should.eql({a: 1});
(yield store.client.ttl('key:ttl')).should.equal(86400);
yield store.quit();
});

it('should not throw error with bad JSON', function (done) {
it('should not throw error with bad JSON', function* () {
var store = require('../')();
co.wrap(function *() {
yield store.client.set('key:badKey', '{I will cause an error!}');
should.not.exist(yield store.get('key:badKey'));
yield store.quit();
done();
})();
yield store.client.set('key:badKey', '{I will cause an error!}');
should.not.exist(yield store.get('key:badKey'));
yield store.quit();
});

it('should set without ttl ok', function (done) {
it('should set without ttl ok', function* () {
var store = require('../')();
co.wrap(function *() {
yield store.set('key:nottl', {a: 1});
(yield store.get('key:nottl')).should.eql({a: 1});
(yield store.client.ttl('key:nottl')).should.equal(-1);
yield store.quit();
done();
})();
yield store.set('key:nottl', {a: 1});
(yield store.get('key:nottl')).should.eql({a: 1});
(yield store.client.ttl('key:nottl')).should.equal(-1);
yield store.quit();
});

it('should destroy ok', function (done) {
it('should destroy ok', function* () {
var store = require('../')();
co.wrap(function *() {
yield store.destroy('key:nottl');
yield store.destroy('key:ttl');
yield store.destroy('key:badKey');
should.not.exist(yield store.get('key:nottl'));
should.not.exist(yield store.get('key:ttl'));
should.not.exist(yield store.get('key:badKey'));
yield store.quit();
done();
})();
yield store.destroy('key:nottl');
yield store.destroy('key:ttl');
yield store.destroy('key:badKey');
should.not.exist(yield store.get('key:nottl'));
should.not.exist(yield store.get('key:ttl'));
should.not.exist(yield store.get('key:badKey'));
yield store.quit();
});

it('should expire after 1s', function (done) {

it('should expire after 1s', function* () {
this.timeout(2000);
function sleep(t) { return new Promise(function(resolve) { setTimeout(resolve, t); }); }

var store = require('../')();
co.wrap(function *() {
yield store.set('key:ttl2', {a: 1, b: 2}, 1000);
function sleep(t) {
return function (done) {
setTimeout(done, t);
}
}
yield sleep(1000);
should.not.exist(yield store.get('key1'));
yield store.quit();
done();
})();
yield store.set('key:ttl2', {a: 1, b: 2}, 1000);
yield sleep(1200); // Some odd delay introduced by co-mocha
should.not.exist(yield store.get('key:ttl2'));
yield store.quit();
});
});

0 comments on commit 8837c62

Please sign in to comment.