Add more test helpers

While working on some tests, I was having a tricky problem in a test suite. Eventually I tracked it down to an interaction between tests. I suspected the test library, but once I tried to make an isolated test case, I realized the test library was working fine. It turns out it was the server’s request cache. The fix is to clear the cache between tests.

Not needed for this PR, though I’m adding it to this branch because it conflicts with this change.
This commit is contained in:
Paul Melnikow 2017-04-07 19:45:09 -04:00 committed by Thaddee Tyl
parent 5a45003bc3
commit de18dce94a
4 changed files with 30 additions and 2 deletions

View File

@ -103,6 +103,12 @@ Cache.prototype = {
return 1;
}
},
clear: function () {
this.cache.clear();
this.newest = null;
this.oldest = null;
}
};
// In bytes.

View File

@ -11,7 +11,6 @@ var camp = Camp.start({
hostname: bindAddress,
secure: secureServer
});
module.exports = camp;
Camp.log.unpipe('warn', 'stderr');
var tryUrl = require('url').format({
protocol: secureServer ? 'https' : 'http',
@ -299,6 +298,11 @@ function cache(f) {
};
}
module.exports = {
camp,
requestCache
};
camp.notfound(/\.(svg|png|gif|jpg|json)/, function(query, match, end, request) {
var format = match[1];
var badgeData = getBadgeData("404", query);

View File

@ -137,4 +137,22 @@ describe('The LRU cache', function () {
assert.equal(slot2.newer, null);
});
it('should clear', function () {
// Set up.
const cache = new LRU(2);
cache.set('key1', 'value1');
cache.set('key2', 'value2');
// Confidence check.
assert.equal(cache.get('key1'), 'value1');
assert.equal(cache.get('key2'), 'value2');
// Run.
cache.clear();
// Test.
assert.equal(cache.get('key1'), null);
assert.equal(cache.get('key2'), null);
assert.equal(cache.cache.size, 0);
});
});

View File

@ -79,7 +79,7 @@ describe('The server', function () {
server = require('../server');
});
after('Shut down the server', function(done) {
server.close(function () { done(); });
server.camp.close(function () { done(); });
});
it('should produce colorscheme badges', function(done) {