From f09701f13914a2e0d4e1ec1a3f2efff71dfb54d5 Mon Sep 17 00:00:00 2001 From: Luka Skukan Date: Wed, 16 Nov 2016 13:06:58 +0100 Subject: [PATCH 1/3] Update async to 2.0 and fix collection resource to match --- lib/resource/CollectionResource.js | 35 +++--- package.json | 2 +- test/sp.resource.collectionResource_test.js | 124 ++------------------ 3 files changed, 28 insertions(+), 133 deletions(-) diff --git a/lib/resource/CollectionResource.js b/lib/resource/CollectionResource.js index 399a734c..af7df226 100644 --- a/lib/resource/CollectionResource.js +++ b/lib/resource/CollectionResource.js @@ -114,6 +114,7 @@ function wrapAsyncCallToAllPages(coll, func, args, callbackWrapper){ [task.collection.items], !!callbackWrapper.wrapPageArgs ? callbackWrapper.wrapPageArgs(args) : args, callbackWrapper.wrapPageCallback(parallel_cb.bind(this, null))); + func.apply(this, callArgs); }, function(parallel_cb){ @@ -125,7 +126,6 @@ function wrapAsyncCallToAllPages(coll, func, args, callbackWrapper){ if (err || !nextPage){ return parallel_cb(err); } - q.push({collection: nextPage}); parallel_cb(); }); @@ -993,16 +993,16 @@ utils.inherits(CollectionResource, Resource); this.isDone = false; this.wrapPageCallback = function wrapPageCallback(cb){ - return function funcCallback(result){ - that.isDone = !result; + return function funcCallback(err, result){ + that.isDone = err || !result; res = res && result; - cb(result); + cb(err, result); }; }; this.wrapCallback = function(callback ){ - return function(){ - callback(res); + return function(err, result){ + callback(err, result); }; }; } @@ -1012,16 +1012,16 @@ utils.inherits(CollectionResource, Resource); this.isDone = false; var that = this; this.wrapPageCallback = function wrapPageCallback(cb){ - return function funcCallback(result){ + return function funcCallback(err, result){ that.isDone = !!result; res = res || result; - cb(result); + cb(err, result); }; }; this.wrapCallback = function(callback ){ - return function(){ - callback(res); + return function(err, result){ + callback(err, result); }; }; } @@ -1032,16 +1032,16 @@ utils.inherits(CollectionResource, Resource); this.isDone = false; this.wrapPageCallback = function wrapPageCallback(cb){ - return function funcCallback(result){ + return function funcCallback(err, result){ that.isDone = !!result; res = res || result; - cb(res); + cb(err, res); }; }; this.wrapCallback = function(callback ){ - return function(){ - callback(res); + return function(err, result){ + callback(err, result); }; }; } @@ -1137,16 +1137,15 @@ utils.inherits(CollectionResource, Resource); }; } - var W1 = function(){ return new CallbackWrapper(1);}; var W2 = function(){ return new CallbackWrapper(2);}; var methods = { each: {wrapper: W2}, eachSeries: {wrapper: W2}, eachLimit: {wrapper: W2}, forEach: {wrapper: W2}, forEachSeries: {wrapper: W2}, forEachLimit: {wrapper: W2}, map: {wrapper: W2}, mapSeries: {wrapper: W2}, mapLimit:{wrapper: W2}, - filter: {wrapper: W1}, filterSeries: {wrapper: W1}, - select: {wrapper: W1}, selectSeries: {wrapper: W1}, - reject: {wrapper: W1}, rejectSeries: {wrapper: W1}, + filter: {wrapper: W2}, filterSeries: {wrapper: W2}, + select: {wrapper: W2}, selectSeries: {wrapper: W2}, + reject: {wrapper: W2}, rejectSeries: {wrapper: W2}, reduce: {wrapper: Reduce}, inject: {wrapper: Reduce}, foldl: {wrapper: Reduce}, reduceRight: {wrapper: ReduceRight, method: 'eachSeries'}, foldr: {wrapper: ReduceRight, method: 'eachSeries'}, diff --git a/package.json b/package.json index 16894ebf..dcc0ca9e 100644 --- a/package.json +++ b/package.json @@ -41,7 +41,7 @@ "test": "grunt" }, "dependencies": { - "async": "~1.5.2", + "async": "^2.1.2", "deep-extend": "^0.4.1", "jwt-simple": "~0.4.0", "memcached": "~2.2.2", diff --git a/test/sp.resource.collectionResource_test.js b/test/sp.resource.collectionResource_test.js index 5a29bb99..f2f3fcfd 100644 --- a/test/sp.resource.collectionResource_test.js +++ b/test/sp.resource.collectionResource_test.js @@ -576,7 +576,7 @@ describe('Resources: ', function () { result = false; } - cb(result); + cb(null, result); return result; } @@ -611,7 +611,7 @@ describe('Resources: ', function () { }); it('should call iterators with same arguments', function(){ - for (var i = 0; i < shouldBeCalledCount; i++){ + for (var i = 0; i < iteratorSpy.callCount; i++){ var asyncArgs = asyncIteratorSpy.getCall(i).args; var args = iteratorSpy.getCall(i).args; asyncArgs[0].should.be.deep.equal(_.pick(args[0], 'href', 'name', 'description', 'status')); @@ -624,110 +624,6 @@ describe('Resources: ', function () { }; } - function testCheck(method){ - return function(){ - var n; - - function createAppsCollection(items, offset, limit){ - return { - href: '/tenants/78KBoSJ5EkMD8OVmBV934Y/applications', - offset: offset, - limit: limit, - items: items.slice(offset, offset+limit) - }; - } - - function application(i){ - return { href: '/applications/' + i, - name: 'testing ' + i, - description: i, - status: 'ENABLED' - }; - } - - function createNApps(n){ - var items = []; - for (var i = 0; i < n; i++){ - items.push(application(i)); - } - return items; - } - - var i, items, pages, applications; - var sandbox, iteratorSpy, callbackSpy, asyncIteratorSpy, asyncCallbackSpy; - - before(function(done){ - n = 250; - pages = []; - - // set up: - // 1. items - items = createNApps(n); - // 2. create app collection resource - for (i = 0; i < Math.ceil(n/100); i++){ - pages.push(createAppsCollection(items, i*100, (i+1)*100)); - } - applications = instantiate(Application, pages[0], {}, ds); - // 3. nock - nock(u.BASE_URL).get(u.v1(applications.href)).reply(200,pages); - for (i = 1; i < Math.ceil(n/100); i++) { - var ref = u.v1(applications.href) + '?' + querystring.stringify({offset: i * 100, limit: 100}); - nock(u.BASE_URL).get(ref).reply(200, pages[i]); - } - sandbox = sinon.sandbox.create(); - // 4. iterator and callback spies - function iterator(item, cb){ - cb(item.description % 2 === 0); - } - iteratorSpy = sandbox.spy(iterator); - asyncIteratorSpy = sandbox.spy(iterator); - - async.series([ - function(cb){ - function callback(){ - cb(); - } - callbackSpy = sandbox.spy(callback); - applications[method](iteratorSpy, callbackSpy); - }, - function(cb){ - function callback(){ - cb(); - } - asyncCallbackSpy = sandbox.spy(callback); - async[method](items, asyncIteratorSpy, asyncCallbackSpy); - } - ], done); - }); - - after(function(){ - sandbox.restore(); - }); - - it('should call iterators with same arguments', function(){ - for (var i = 0; i < n; i++){ - var asyncArgs = asyncIteratorSpy.getCall(i).args; - var args = iteratorSpy.getCall(i).args; - asyncArgs[0].should.be.deep.equal(_.pick(args[0], 'href', 'name', 'description', 'status')); - } - }); - - it('should call iterator n times', function(){ - iteratorSpy.should.have.been.calledBefore(callbackSpy); - iteratorSpy.callCount.should.be.equal(n); - }); - - it('should call callback once', function(){ - callbackSpy.should.have.been.calledOnce; - var args = _.map(callbackSpy.getCall(0).args[0], - function(item) { - return _.pick(item, 'href', 'name', 'description', 'status'); - }); - var asyncArgs = asyncCallbackSpy.getCall(0).args[0]; - args.should.be.deep.equal(asyncArgs); - }); - }; - } function testSortBy(method){ return function(){ @@ -944,12 +840,12 @@ describe('Resources: ', function () { describe('map', test('map')); describe('mapSeries', test('mapSeries')); describe('mapLimit', testLimit('mapLimit')); - describe('filter', testCheck('filter')); - describe('filterSeries', testCheck('filterSeries')); - describe('select', testCheck('select')); - describe('selectSeries', testCheck('selectSeries')); - describe('reject', testCheck('reject')); - describe('rejectSeries', testCheck('rejectSeries')); + describe('filter', test('filter')); + describe('filterSeries', test('filterSeries')); + describe('select', test('select')); + describe('selectSeries', test('selectSeries')); + describe('reject', test('reject')); + describe('rejectSeries', test('rejectSeries')); describe('reduce', testReduce('reduce')); describe('inject', testReduce('inject')); describe('foldl', testReduce('foldl')); @@ -960,8 +856,8 @@ describe('Resources: ', function () { describe('sortBy', testSortBy('sortBy')); describe('some', testBoolean('some')); describe('any', testBoolean('any')); - describe('every', testBoolean('every')); - describe('all', testBoolean('all')); + describe('every', testBoolean('every', true)); + describe('all', testBoolean('all', true)); describe('concat', test('concat')); describe('concatSeries', test('concatSeries')); }); From af083126b65afdf9952940615ebb310253280676 Mon Sep 17 00:00:00 2001 From: Luka Skukan Date: Wed, 16 Nov 2016 13:31:17 +0100 Subject: [PATCH 2/3] Update code related to Collection changes --- lib/resource/Application.js | 18 ++++++++++++++---- lib/resource/CollectionResource.js | 12 ++++++------ test/it/application_it.js | 13 +++++++------ 3 files changed, 27 insertions(+), 16 deletions(-) diff --git a/lib/resource/Application.js b/lib/resource/Application.js index caa7381b..64d34121 100644 --- a/lib/resource/Application.js +++ b/lib/resource/Application.js @@ -1225,10 +1225,16 @@ Application.prototype.setDefaultAccountStore = function setDefaultAccountStore(s return callback(err); } - res.detectSeries(function(asm, cb){cb(asm.accountStore.href === store.href);}, onAsmFound); + res.detectSeries(function(asm, cb){ + cb(null, asm.accountStore.href === store.href); + }, onAsmFound); }); - function onAsmFound(asm) { + function onAsmFound(err, asm) { + if (err) { + throw err; + } + if (asm) { asm.isDefaultAccountStore = true; return asm.save(clearCache); @@ -1337,10 +1343,14 @@ Application.prototype.setDefaultGroupStore = function setDefaultGroupStore(store if (err) { return callback(err); } - res.detectSeries(function(asm, cb){cb(asm.accountStore.href === store.href);}, onAsmFound); + res.detectSeries(function(asm, cb){cb(null, asm.accountStore.href === store.href);}, onAsmFound); }); - function onAsmFound(asm) { + function onAsmFound(err, asm) { + if (err) { + throw err; + } + if (asm) { asm.isDefaultGroupStore = true; return asm.save(updateApp); diff --git a/lib/resource/CollectionResource.js b/lib/resource/CollectionResource.js index af7df226..3d419320 100644 --- a/lib/resource/CollectionResource.js +++ b/lib/resource/CollectionResource.js @@ -1001,8 +1001,8 @@ utils.inherits(CollectionResource, Resource); }; this.wrapCallback = function(callback ){ - return function(err, result){ - callback(err, result); + return function(err){ + callback(err, res); }; }; } @@ -1020,8 +1020,8 @@ utils.inherits(CollectionResource, Resource); }; this.wrapCallback = function(callback ){ - return function(err, result){ - callback(err, result); + return function(err){ + callback(err, res); }; }; } @@ -1040,8 +1040,8 @@ utils.inherits(CollectionResource, Resource); }; this.wrapCallback = function(callback ){ - return function(err, result){ - callback(err, result); + return function(err){ + callback(err, res); }; }; } diff --git a/test/it/application_it.js b/test/it/application_it.js index 2c5df1d1..a86e7aa4 100644 --- a/test/it/application_it.js +++ b/test/it/application_it.js @@ -156,27 +156,28 @@ describe('Application',function(){ describe('setDefaultAccountStore',function () { describe('with a href string property',function(){ - var result; + var error; + before(function(done){ app.setDefaultAccountStore(directory.href,function(err){ - result = err; + error = err; done(); }); }); it('should not err',function(){ - assert.equal(result,null); + assert.notOk(error); }); }); describe('with a directory object',function(){ - var result; + var error; before(function(done){ app.setDefaultAccountStore(directory,function(err){ - result = err; + error = err; done(); }); }); it('should not err',function(){ - assert.equal(result,null); + assert.notOk(error); }); }); }); From 4352359ac883b209be1c996f50b2189019db3fdb Mon Sep 17 00:00:00 2001 From: Luka Skukan Date: Thu, 17 Nov 2016 11:51:54 +0100 Subject: [PATCH 3/3] Update examples --- collection_each_example.js | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/collection_each_example.js b/collection_each_example.js index 5c15a8b3..9c02c874 100644 --- a/collection_each_example.js +++ b/collection_each_example.js @@ -146,15 +146,15 @@ function filter(apps, cb) { l.reset(); var i = 0; function iterator(app, cb) { - cb(i++ % 2 === 0); + cb(null, i++ % 2 === 0); } async.series([ function (cb) { - apps.filter(l.i(iterator), l.c(cb, 1)); + apps.filter(l.i(iterator), l.c(cb)); }, function (cb) { - apps.filterSeries(l.i(iterator), l.c(cb, 1)); + apps.filterSeries(l.i(iterator), l.c(cb)); } ], cb); } @@ -163,15 +163,15 @@ function reject(apps, cb) { l.reset(); var i = 0; function iterator(app, cb) { - cb(i++ % 2 === 0); + cb(null, i++ % 2 === 0); } async.series([ function (cb) { - apps.reject(l.i(iterator), l.c(cb, 1)); + apps.reject(l.i(iterator), l.c(cb)); }, function (cb) { - apps.rejectSeries(l.i(iterator), l.c(cb, 1)); + apps.rejectSeries(l.i(iterator), l.c(cb)); } ], cb); } @@ -197,15 +197,15 @@ function detect(apps, cb) { l.reset(); var i = 0; function iterator(app, cb) { - cb(0 === i++ % 2); + cb(null, 0 === i++ % 2); } async.series([ function (cb) { - apps.detect(l.i(iterator), l.c(cb, 1)); + apps.detect(l.i(iterator), l.c(cb)); }, function (cb) { - apps.detectSeries(l.i(iterator), l.c(cb, 1)); + apps.detectSeries(l.i(iterator), l.c(cb)); } ], cb); } @@ -214,20 +214,20 @@ function some(apps, cb) { l.reset(); var i = 0; function iterator(app, cb) { - cb(0 === i++ % 2); + cb(null, 0 === i++ % 2); } - apps.some(l.i(iterator), l.c(cb, 1)); + apps.some(l.i(iterator), l.c(cb)); } function every(apps, cb) { l.reset(); var i = 0; function iterator(app, cb) { - cb(0 === i++ % 2); + cb(null, 0 === i++ % 2); } - apps.every(l.i(iterator), l.c(cb, 1)); + apps.every(l.i(iterator), l.c(cb)); } function sortBy(apps, cb) { @@ -251,4 +251,4 @@ function concat(apps, cb) { apps.concatSeries(l.i(iterator), l.c(cb)); } ], cb); -} \ No newline at end of file +}