11'use strict' ;
22
3+ var bdd = require ( '../helpers/bdd-if' ) ;
34var should = require ( 'should' ) ;
45var helpers = require ( './_helpers' ) ;
56var Promise = require ( 'bluebird' ) ;
67
78module . exports = function ( dataSourceFactory , connectorCapabilities ) {
8- describe ( 'expire' , function ( ) {
9+ // While we support millisecond precision, for the purpose of tests
10+ // it's better to use intervals at least 10ms long.
11+ var ttlPrecision = connectorCapabilities . ttlPrecision || 10 ;
12+
13+ var canExpire = connectorCapabilities . canExpire !== false ;
14+
15+ bdd . describeIf ( canExpire , 'expire' , function ( ) {
916 var CacheItem ;
1017 beforeEach ( function unpackContext ( ) {
1118 CacheItem = helpers . givenCacheItem ( dataSourceFactory ) ;
@@ -14,30 +21,30 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
1421 it ( 'sets key ttl - Callback API' , function ( done ) {
1522 CacheItem . set ( 'a-key' , 'a-value' , function ( err ) {
1623 if ( err ) return done ( err ) ;
17- CacheItem . expire ( 'a-key' , 1 , function ( err ) {
24+ CacheItem . expire ( 'a-key' , ttlPrecision , function ( err ) {
1825 if ( err ) return done ( err ) ;
1926 setTimeout ( function ( ) {
2027 CacheItem . get ( 'a-key' , function ( err , value ) {
2128 if ( err ) return done ( err ) ;
2229 should . equal ( value , null ) ;
2330 done ( ) ;
2431 } ) ;
25- } , 20 ) ;
32+ } , 2 * ttlPrecision ) ;
2633 } ) ;
2734 } ) ;
2835 } ) ;
2936
3037 it ( 'sets key ttl - Promise API' , function ( ) {
3138 return Promise . resolve ( CacheItem . set ( 'a-key' , 'a-value' ) )
32- . then ( function ( ) { return CacheItem . expire ( 'a-key' , 1 ) ; } )
33- . delay ( 20 )
39+ . then ( function ( ) { return CacheItem . expire ( 'a-key' , ttlPrecision ) ; } )
40+ . delay ( 2 * ttlPrecision )
3441 . then ( function ( ) { return CacheItem . get ( 'a-key' ) ; } )
3542 . then ( function ( value ) { should . equal ( value , null ) ; } ) ;
3643 } ) ;
3744
3845 it ( 'returns error when expiring a key that has expired' , function ( ) {
39- return Promise . resolve ( CacheItem . set ( 'expired-key' , 'a-value' , 1 ) )
40- . delay ( 20 )
46+ return Promise . resolve ( CacheItem . set ( 'expired-key' , 'a-value' , ttlPrecision ) )
47+ . delay ( 2 * ttlPrecision )
4148 . then ( function ( ) { return CacheItem . expire ( 'expired-key' , 1000 ) ; } )
4249 . then (
4350 function ( ) { throw new Error ( 'expire() should have failed' ) ; } ,
@@ -48,7 +55,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
4855 } ) ;
4956
5057 it ( 'returns error when key does not exist' , function ( ) {
51- return CacheItem . expire ( 'key-does-not-exist' , 1 ) . then (
58+ return CacheItem . expire ( 'key-does-not-exist' , ttlPrecision ) . then (
5259 function ( ) { throw new Error ( 'expire() should have failed' ) ; } ,
5360 function ( err ) {
5461 err . message . should . match ( / k e y - d o e s - n o t - e x i s t / ) ;
0 commit comments