-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b13186a
commit 504a1ad
Showing
2 changed files
with
54 additions
and
3 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
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,46 @@ | ||
const LimitDB = require('../lib/db'); | ||
const MockDate = require('mockdate'); | ||
const assert = require('chai').assert; | ||
|
||
const types = { | ||
ip: { | ||
size: 5, | ||
per_interval: 5, | ||
interval: 500, | ||
discrete: true | ||
} | ||
}; | ||
|
||
describe('when the fill rate is discrete', () => { | ||
afterEach(function () { | ||
MockDate.reset(); | ||
}); | ||
|
||
const db = new LimitDB({ | ||
inMemory: true, | ||
types | ||
}); | ||
|
||
beforeEach(function(done) { | ||
MockDate.set(Date.now()); | ||
db.take({ type: 'ip', key: '21.17.65.41', count: 5 }, done); | ||
}); | ||
|
||
it('should not add less than the per_interval amount', function(done) { | ||
MockDate.set(Date.now() + 150); | ||
db.status({ type: 'ip', prefix: '21.17.65.41' }, (err, result) => { | ||
if (err) { return done(err); } | ||
assert.equal(result.items[0].remaining, 0); | ||
done(); | ||
}); | ||
}); | ||
|
||
it('should add the per_interval amount after the elapsed interval', function(done) { | ||
MockDate.set(Date.now() + 500); | ||
db.status({ type: 'ip', prefix: '21.17.65.41' }, (err, result) => { | ||
if (err) { return done(err); } | ||
assert.equal(result.items[0].remaining, 5); | ||
done(); | ||
}); | ||
}); | ||
}); |