Blame view

node_modules/loopback-datasource-juggler/test/kvao/expire.suite.js 2.37 KB
f7563de62   Palak Handa   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
  'use strict';
  
  var bdd = require('../helpers/bdd-if');
  var should = require('should');
  var helpers = require('./_helpers');
  var Promise = require('bluebird');
  
  module.exports = function(dataSourceFactory, connectorCapabilities) {
    // While we support millisecond precision, for the purpose of tests
    // it's better to use intervals at least 10ms long.
    var ttlPrecision = connectorCapabilities.ttlPrecision || 10;
  
    var canExpire = connectorCapabilities.canExpire !== false;
  
    bdd.describeIf(canExpire, 'expire', function() {
      var CacheItem;
      beforeEach(function unpackContext() {
        CacheItem = helpers.givenCacheItem(dataSourceFactory);
      });
  
      it('sets key ttl - Callback API', function(done) {
        CacheItem.set('a-key', 'a-value', function(err) {
          if (err) return done(err);
          CacheItem.expire('a-key', ttlPrecision, function(err) {
            if (err) return done(err);
            setTimeout(function() {
              CacheItem.get('a-key', function(err, value) {
                if (err) return done(err);
                should.equal(value, null);
                done();
              });
            }, 2 * ttlPrecision);
          });
        });
      });
  
      it('sets key ttl - Promise API', function() {
        return Promise.resolve(CacheItem.set('a-key', 'a-value'))
          .then(function() { return CacheItem.expire('a-key', ttlPrecision); })
          .delay(2 * ttlPrecision)
          .then(function() { return CacheItem.get('a-key'); })
          .then(function(value) { should.equal(value, null); });
      });
  
      it('returns error when expiring a key that has expired', function() {
        return Promise.resolve(CacheItem.set('expired-key', 'a-value', ttlPrecision))
          .delay(2 * ttlPrecision)
          .then(function() { return CacheItem.expire('expired-key', 1000); })
          .then(
            function() { throw new Error('expire() should have failed'); },
            function(err) {
              err.message.should.match(/expired-key/);
              err.should.have.property('statusCode', 404);
            });
      });
  
      it('returns error when key does not exist', function() {
        return CacheItem.expire('key-does-not-exist', ttlPrecision).then(
          function() { throw new Error('expire() should have failed'); },
          function(err) {
            err.message.should.match(/key-does-not-exist/);
            err.should.have.property('statusCode', 404);
          });
      });
    });
  };