Skip to content

Commit

Permalink
Merge b6a43dd into 5eb81aa
Browse files Browse the repository at this point in the history
  • Loading branch information
bajtos committed Oct 5, 2018
2 parents 5eb81aa + b6a43dd commit 3cd54a6
Show file tree
Hide file tree
Showing 11 changed files with 26 additions and 20 deletions.
1 change: 0 additions & 1 deletion lib/utils.js
Expand Up @@ -30,7 +30,6 @@ exports.rankArrayElements = rankArrayElements;
var g = require('strong-globalize')();
var traverse = require('traverse');
var assert = require('assert');
var Promise = require('bluebird');

function safeRequire(module) {
try {
Expand Down
1 change: 0 additions & 1 deletion package.json
Expand Up @@ -48,7 +48,6 @@
},
"dependencies": {
"async": "^2.6.0",
"bluebird": "^3.1.1",
"debug": "^3.1.0",
"depd": "^1.0.0",
"inflection": "^1.6.0",
Expand Down
1 change: 0 additions & 1 deletion test/async-observer.test.js
Expand Up @@ -6,7 +6,6 @@

var ModelBuilder = require('../').ModelBuilder;
var should = require('./init');
var Promise = require('bluebird');

describe('async observer', function() {
var TestModel;
Expand Down
9 changes: 7 additions & 2 deletions test/kvao/_helpers.js
@@ -1,10 +1,9 @@
'use strict';

var Promise = require('bluebird');

exports.givenCacheItem = givenCacheItem;
exports.givenKeys = givenKeys;
exports.givenModel = givenModel;
exports.delay = delay;

function givenCacheItem(dataSourceFactory) {
const modelProperties = {
Expand Down Expand Up @@ -34,3 +33,9 @@ function givenKeys(Model, keys, cb) {
}
return p;
};

function delay(ms) {
return new Promise((resolve) => {
setTimeout(resolve, ms);
});
}
5 changes: 2 additions & 3 deletions test/kvao/expire.suite.js
Expand Up @@ -3,7 +3,6 @@
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
Expand Down Expand Up @@ -35,14 +34,14 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
it('sets key ttl - Promise API', function() {
return CacheItem.set('a-key', 'a-value')
.then(function() { return CacheItem.expire('a-key', ttlPrecision); })
.delay(2 * ttlPrecision)
.then(() => helpers.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(() => helpers.delay(2 * ttlPrecision))
.then(function() { return CacheItem.expire('expired-key', 1000); })
.then(
function() { throw new Error('expire() should have failed'); },
Expand Down
7 changes: 3 additions & 4 deletions test/kvao/get-set.suite.js
Expand Up @@ -2,7 +2,6 @@

var should = require('should');
var helpers = require('./_helpers');
var Promise = require('bluebird');

module.exports = function(dataSourceFactory, connectorCapabilities) {
var TTL_PRECISION = connectorCapabilities.ttlPrecision;
Expand Down Expand Up @@ -69,7 +68,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {

it('honours options.ttl', function() {
return CacheItem.set('a-key', 'a-value', {ttl: TTL_PRECISION})
.delay(2 * TTL_PRECISION)
.then(() => helpers.delay(2 * TTL_PRECISION))
.then(function() { return CacheItem.get('a-key'); })
.then(function(value) { should.equal(value, null); });
});
Expand All @@ -84,7 +83,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
describe('set', function() {
it('converts numeric options arg to options.ttl', function() {
return CacheItem.set('a-key', 'a-value', TTL_PRECISION)
.delay(2 * TTL_PRECISION)
.then(() => helpers.delay(2 * TTL_PRECISION))
.then(function() { return CacheItem.get('a-key'); })
.then(function(value) { should.equal(value, null); });
});
Expand All @@ -94,7 +93,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
.then(function() {
return CacheItem.set('a-key', 'another-value'); // no TTL
})
.delay(2 * TTL_PRECISION)
.then(() => helpers.delay(2 * TTL_PRECISION))
.then(function() { return CacheItem.get('a-key'); })
.then(function(value) { should.equal(value, 'another-value'); });
});
Expand Down
14 changes: 12 additions & 2 deletions test/kvao/iterate-keys.suite.js
Expand Up @@ -3,9 +3,7 @@
var asyncIterators = require('async-iterators');
var bdd = require('../helpers/bdd-if');
var helpers = require('./_helpers');
var Promise = require('bluebird');
var should = require('should');
var toArray = Promise.promisify(asyncIterators.toArray);

module.exports = function(dataSourceFactory, connectorCapabilities) {
var canIterateKeys = connectorCapabilities.canIterateKeys !== false;
Expand Down Expand Up @@ -52,3 +50,15 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
};
});
};

// A promisified version of asyncIterators.toArray
// Node.js 8.x does not have util.promisify function,
// we are adding promise support manually here
function toArray(iter) {
return new Promise((resolve, reject) => {
asyncIterators.toArray(iter, (err, result) => {
if (err) reject(err);
else resolve(result);
});
});
}
1 change: 0 additions & 1 deletion test/kvao/keys.suite.js
Expand Up @@ -2,7 +2,6 @@

var bdd = require('../helpers/bdd-if');
var helpers = require('./_helpers');
var Promise = require('bluebird');
var should = require('should');

module.exports = function(dataSourceFactory, connectorCapabilities) {
Expand Down
5 changes: 2 additions & 3 deletions test/kvao/ttl.suite.js
Expand Up @@ -3,7 +3,6 @@
var bdd = require('../helpers/bdd-if');
var should = require('should');
var helpers = require('./_helpers');
var Promise = require('bluebird');

module.exports = function(dataSourceFactory, connectorCapabilities) {
var TTL_PRECISION = connectorCapabilities.ttlPrecision;
Expand All @@ -28,7 +27,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
return Promise.resolve(
CacheItem.set('a-key', 'a-value', {ttl: INITIAL_TTL})
)
.delay(SMALL_DELAY)
.then(() => helpers.delay(SMALL_DELAY))
.then(function() { return CacheItem.ttl('a-key'); })
.then(function(ttl) { ttl.should.be.within(1, INITIAL_TTL); });
});
Expand All @@ -55,7 +54,7 @@ module.exports = function(dataSourceFactory, connectorCapabilities) {
return Promise.resolve(
CacheItem.set('expired-key', 'a-value', {ttl: TTL_PRECISION})
)
.delay(2 * TTL_PRECISION)
.then(() => helpers.delay(2 * TTL_PRECISION))
.then(function() {
return CacheItem.ttl('expired-key');
})
Expand Down
1 change: 0 additions & 1 deletion test/operation-hooks.suite/embeds-many-destroy.suite.js
Expand Up @@ -5,7 +5,6 @@

'use strict';

var Promise = require('bluebird');
var ValidationError = require('../..').ValidationError;

var contextTestHelpers = require('../helpers/context-test-helpers');
Expand Down
Expand Up @@ -5,7 +5,6 @@

'use strict';

var Promise = require('bluebird');
var ValidationError = require('../..').ValidationError;

var contextTestHelpers = require('../helpers/context-test-helpers');
Expand Down

0 comments on commit 3cd54a6

Please sign in to comment.