forked from ptarjan/node-cache
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added more-legitimate tests + code coverage.
- Loading branch information
1 parent
745bd36
commit 1c70278
Showing
9 changed files
with
195 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
node_modules | ||
.DS_Store | ||
Thumbs.db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
language: node_js | ||
node_js: | ||
- "0.11" | ||
- "0.10" | ||
- "0.8" | ||
script: | ||
- make test | ||
- make test-ci-coverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
.PHONY: test test-ci-coverage | ||
|
||
MOCHA=node_modules/.bin/mocha | ||
COVERALLS=node_modules/.bin/coveralls | ||
_MOCHA=node_modules/.bin/_mocha | ||
ISTANBUL=node_modules/.bin/istanbul | ||
|
||
test: | ||
npm test | ||
|
||
test-ci-coverage: | ||
npm install coveralls | ||
npm install istanbul | ||
@rm -rf coverage | ||
$(ISTANBUL) cover $(_MOCHA) --report lcovonly -- -R tap | ||
|
||
@echo | ||
@echo Sending report to coveralls.io... | ||
@cat ./coverage/lcov.info | $(COVERALLS) | ||
@rm -rf ./coverage | ||
@echo Done |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,24 +1,31 @@ | ||
{ | ||
"name" : "tinycache", | ||
"description" : "A simple in-memory cache in ~100 lines.", | ||
"author" : "Andy Burke <aburke@bitflood.org>", | ||
"url" : "https://github.com/andyburke/tinycache", | ||
"name": "tinycache", | ||
"description": "A simple in-memory cache in ~100 lines.", | ||
"author": "Andy Burke <aburke@bitflood.org>", | ||
"url": "https://github.com/andyburke/tinycache", | ||
"contributors": [ | ||
{ | ||
"name": "Paul Tarjan", | ||
"email": "npm@paulisageek.com", | ||
"web": "https://github.com/ptarjan" | ||
"web": "https://github.com/ptarjan" | ||
} | ||
], | ||
"keywords" : [ | ||
"keywords": [ | ||
"cache", | ||
"in-memory", | ||
"storage" | ||
], | ||
"main" : "./tinycache.js", | ||
"version" : "0.1.1", | ||
"repository" : { | ||
"type" : "git", | ||
"url" : "git://github.com/andyburke/tinycache.git" | ||
"main": "./tinycache.js", | ||
"version": "0.1.1", | ||
"scripts": { | ||
"test": "mocha -R list" | ||
}, | ||
"repository": { | ||
"type": "git", | ||
"url": "git://github.com/andyburke/tinycache.git" | ||
}, | ||
"devDependencies": { | ||
"mocha": "^1.21.3", | ||
"chai": "^1.9.1" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
var assert = require('chai').assert; | ||
var TinyCache = require('../tinycache.js'); | ||
var i = 0, new_key = function() { return 'key_' + (++i); }; | ||
|
||
var suite = function(cache) { | ||
describe('size()', function() { | ||
it('should return 0 when empty', function() { | ||
cache.clear(); | ||
assert.equal(cache.size(), 0); | ||
}); | ||
it('should return size', function() { | ||
cache.clear(); | ||
cache.put(new_key(), 1, 10); | ||
assert.equal(cache.size(), 1); | ||
}); | ||
it('should return correct value after object falls out of cache', function(done) { | ||
var start = cache.size(); | ||
cache.put(new_key(), 1, 2); | ||
assert.equal(cache.size(), start + 1); | ||
setTimeout(function() { | ||
assert.equal(cache.size(), start); | ||
done(); | ||
}, 4); | ||
}); | ||
}); | ||
|
||
describe('clear()', function() { | ||
it('should clear all objects', function() { | ||
var size_start = cache.size(); | ||
var keys = [new_key(), new_key(), new_key()]; | ||
cache.put(keys[0], 1, 10); | ||
cache.put(keys[1], 2, 10); | ||
cache.put(keys[2], 3, 10); | ||
assert.equal(cache.size(), size_start + 3); | ||
|
||
cache.clear(); | ||
assert.equal(cache.size(), 0); | ||
assert.isNull(cache.get(keys[0])); | ||
assert.isNull(cache.get(keys[1])); | ||
assert.isNull(cache.get(keys[2])); | ||
}); | ||
}); | ||
|
||
describe('get()', function() { | ||
it('should return null if key doesn\'t exist', function() { | ||
assert.isNull(cache.get('awf1n1a')); | ||
assert.isNull(cache.get(null)); | ||
assert.isNull(cache.get()); | ||
assert.isNull(cache.get(1)); | ||
assert.isNull(cache.get(true)); | ||
assert.isNull(cache.get(false)); | ||
}); | ||
it('should return value', function() { | ||
var complicated = ['a',{'b':'c','d':['e',3]},'@']; | ||
var key = new_key(); | ||
cache.put(key, complicated, 100); | ||
assert.deepEqual(cache.get(key), complicated); | ||
}); | ||
}); | ||
|
||
describe('put()', function() { | ||
it('should overwrite existing object if exists', function(done) { | ||
var ttl1 = 20; | ||
var ttl2 = 20; | ||
var value1 = {a:1}; | ||
var value2 = {a:2}; | ||
var key = new_key(); | ||
|
||
cache.put(key, value1, ttl1); | ||
assert.deepEqual(cache.get(key), value1); | ||
setTimeout(function() { | ||
cache.put(key, value2, ttl2); | ||
assert.deepEqual(cache.get(key), value2); | ||
}, 10); | ||
|
||
// test that the value isn't wiped out by the first | ||
// put()'s timeout | ||
setTimeout(function() { | ||
assert.deepEqual(cache.get(key), value2); | ||
done(); | ||
}, 25); | ||
}); | ||
describe('stored object', function() { | ||
it('should exist during ttl (ms)', function(done) { | ||
var ttl = 20; | ||
var value = {a:1}; | ||
var key = new_key(); | ||
cache.put(key, value, ttl); | ||
setTimeout(function() { | ||
assert.deepEqual(cache.get(key), value); | ||
done(); | ||
}, 10); | ||
}); | ||
it('should expire after ttl (ms)', function(done) { | ||
var ttl = 20; | ||
var value = {a:1}; | ||
var key = new_key(); | ||
cache.put(key, value, ttl) | ||
setTimeout(function() { | ||
assert.isNull(cache.get(key)); | ||
done(); | ||
}, 30); | ||
}); | ||
}); | ||
}); | ||
|
||
describe('hits()', function() { | ||
it('should return number of cache hits', function() { | ||
var key = new_key(); | ||
cache.put(key, 1, 1); | ||
var start = cache.hits(); | ||
cache.get('missing'); | ||
cache.get(key); | ||
cache.get(key); | ||
cache.get(key); | ||
assert.equal(cache.hits(), start + 3); | ||
}); | ||
}); | ||
|
||
describe('misses()', function() { | ||
it('should return number of cache misses', function() { | ||
var key = new_key(); | ||
cache.put(key, 1, 1); | ||
var start = cache.misses(); | ||
cache.get('missing'); | ||
cache.get('missing'); | ||
cache.get(key); | ||
assert.equal(cache.misses(), start + 2); | ||
}); | ||
}); | ||
}; | ||
|
||
describe('tinycache (instance)', function() { suite(new TinyCache()); }); | ||
describe('tinycache (shared)', function() { suite(TinyCache.shared); }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.