Skip to content

Commit

Permalink
test: improve cache tests (#850)
Browse files Browse the repository at this point in the history
Signed-off-by: Ilya Amelevich <ilya.amelevich@gmail.com>
  • Loading branch information
iamelevich committed Mar 21, 2024
1 parent d704ae2 commit afb2e46
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions test/cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ test('Using cache with TTL', t => {
});

test('Using cache with custom get cache key', t => {
t.plan(9);
t.plan(11);
const expected = 34;
const options = {
cache: true,
Expand All @@ -106,6 +106,9 @@ test('Using cache with custom get cache key', t => {
})
.then(() => breaker.fire(expected))
.then(arg => {
t.equals(breaker.options.cacheTransport.cache.size, 1, 'cache have one entry');
t.ok(breaker.options.cacheTransport.cache.has(`key-${expected}`), 'cache has the key');

const stats = breaker.status.stats;
t.equals(stats.cacheHits, 1, 'hit the cache');
t.equals(stats.cacheMisses, 1, 'did not emit miss');
Expand All @@ -114,6 +117,7 @@ test('Using cache with custom get cache key', t => {
`cache hits:misses ${stats.cacheHits}:${stats.cacheMisses}`);
breaker.clearCache();
})
.then(() => breaker.clearCache())
.then(() => breaker.fire(expected))
.then(arg => {
const stats = breaker.status.stats;
Expand All @@ -126,7 +130,7 @@ test('Using cache with custom get cache key', t => {
});

test('Using cache with custom transport', t => {
t.plan(9);
t.plan(15);
const expected = 34;
const cache = new Map();
const options = {
Expand All @@ -139,8 +143,11 @@ test('Using cache with custom transport', t => {
};
const breaker = new CircuitBreaker(passFail, options);

t.equals(cache.size, 0, 'cache is empty');

breaker.fire(expected)
.then(arg => {
t.equals(cache.size, 1, 'cache has one entry');
const stats = breaker.status.stats;
t.equals(stats.cacheHits, 0, 'does not hit the cache');
t.equals(stats.cacheMisses, 1, 'emits a cacheMiss');
Expand All @@ -150,21 +157,27 @@ test('Using cache with custom transport', t => {
})
.then(() => breaker.fire(expected))
.then(arg => {
t.equals(cache.size, 1, 'cache has one entry');
const stats = breaker.status.stats;
t.equals(stats.cacheHits, 1, 'hit the cache');
t.equals(stats.cacheMisses, 1, 'did not emit miss');
t.equals(stats.fires, 2, 'fired twice');
t.equals(arg, expected,
`cache hits:misses ${stats.cacheHits}:${stats.cacheMisses}`);
breaker.clearCache();
t.equals(cache.size, 0, 'cache is empty');
})
.then(() => breaker.fire(expected))
.then(arg => {
t.equals(cache.size, 1, 'cache has one entry');
const stats = breaker.status.stats;
t.equals(arg, expected,
`cache hits:misses ${stats.cacheHits}:${stats.cacheMisses}`);
})
.then(() => breaker.shutdown())
.then(() => {
t.equals(cache.size, 0, 'cache is empty');
})
.then(t.end)
.catch(t.fail);
});

0 comments on commit afb2e46

Please sign in to comment.