|
| 1 | +import assert from 'assertive'; |
| 2 | +import Bluebird from 'bluebird'; |
| 3 | +import { identity } from 'lodash'; |
| 4 | + |
| 5 | +import Cache from '../lib/cache'; |
| 6 | + |
| 7 | +describe('Cache timeouts', () => { |
| 8 | + const cache = new Cache({ |
| 9 | + backend: { |
| 10 | + get() { |
| 11 | + return Bluebird.resolve({ d: 'get result' }).delay(150); |
| 12 | + }, |
| 13 | + set() { |
| 14 | + return Bluebird.resolve('set result').delay(150); |
| 15 | + }, |
| 16 | + }, |
| 17 | + name: 'awesome-name', |
| 18 | + debug: true, |
| 19 | + }); |
| 20 | + |
| 21 | + describe('with a timeout <150ms', () => { |
| 22 | + before(() => cache.defaults.timeout = 50); |
| 23 | + |
| 24 | + it('get fails fast', async () => { |
| 25 | + const err = await Bluebird.race([ |
| 26 | + cache.get('my-key').then(null, identity), |
| 27 | + Bluebird.delay(100, 'too slow'), // this should not be used |
| 28 | + ]); |
| 29 | + assert.expect(err instanceof Error); |
| 30 | + assert.equal('TimeoutError', err.name); |
| 31 | + }); |
| 32 | + |
| 33 | + it('set fails fast', async () => { |
| 34 | + const err = await Bluebird.race([ |
| 35 | + cache.set('my-key', 'my-value').then(null, identity), |
| 36 | + Bluebird.delay(100, 'too slow'), // this should not be used |
| 37 | + ]); |
| 38 | + assert.expect(err instanceof Error); |
| 39 | + assert.equal('TimeoutError', err.name); |
| 40 | + }); |
| 41 | + |
| 42 | + it('getOrElse fails fast', async () => { |
| 43 | + const value = await Bluebird.race([ |
| 44 | + cache.getOrElse('my-key', 'my-value').then(null, identity), |
| 45 | + // We need to add a bit of time here because we'll run into the |
| 46 | + // timeout twice - once when trying to read and once while writing. |
| 47 | + Bluebird.delay(150, 'too slow'), // this should not be used |
| 48 | + ]); |
| 49 | + assert.equal('my-value', value); |
| 50 | + }); |
| 51 | + }); |
| 52 | + |
| 53 | + describe('with a timeout >150ms', () => { |
| 54 | + before(() => cache.defaults.timeout = 250); |
| 55 | + |
| 56 | + it('receives the value', async () => { |
| 57 | + const value = await Bluebird.race([ |
| 58 | + cache.get('my-key').then(null, identity), |
| 59 | + Bluebird.delay(200, 'too slow'), // this should not be used |
| 60 | + ]); |
| 61 | + assert.equal('get result', value); |
| 62 | + }); |
| 63 | + |
| 64 | + it('sets the value', async () => { |
| 65 | + const value = await Bluebird.race([ |
| 66 | + cache.set('my-key', 'my-value').then(null, identity), |
| 67 | + Bluebird.delay(200, 'too slow'), // this should not be used |
| 68 | + ]); |
| 69 | + assert.equal('set result', value); |
| 70 | + }); |
| 71 | + |
| 72 | + it('getOrElse can retrieve a value', async () => { |
| 73 | + const value = await Bluebird.race([ |
| 74 | + cache.getOrElse('my-key', 'my-value').then(null, identity), |
| 75 | + Bluebird.delay(200, 'too slow'), // this should not be used |
| 76 | + ]); |
| 77 | + assert.equal('get result', value); |
| 78 | + }); |
| 79 | + }); |
| 80 | +}); |
0 commit comments