fixed scoped promise issue, tests pass :D

This commit is contained in:
Joe Fleming 2014-08-22 17:59:11 -07:00
parent adc7258e18
commit bf6c79cb86
2 changed files with 54 additions and 12 deletions

View file

@ -58,7 +58,8 @@ define(function (require) {
})
.then(function () {
return self._executeRequest(req, opts);
}).then(function () {
})
.then(function () {
return self._stopRequest();
});
};
@ -152,7 +153,7 @@ define(function (require) {
queue = queue.reverse();
}
self.queue = queue;
return self.queue = queue;
};
segmentedFetch.prototype._createRequest = function () {
@ -195,14 +196,18 @@ define(function (require) {
// initial status report
self._statusReport(null);
searchStrategy.getSourceStateFromRequest(req)
return searchStrategy.getSourceStateFromRequest(req)
.then(function (state) {
var loopCount = -1;
return self._processQueue(req, state, remainingSize, loopCount);
})
.then(req.defer.resolve, req.defer.reject);
return req.defer.promise;
.then(function (count) {
return req.defer.resolve(count);
})
.catch(function (err) {
req.defer.reject(err);
return err;
});
};
segmentedFetch.prototype._processQueue = function (req, state, remainingSize, loopCount) {

View file

@ -35,11 +35,13 @@ define(function (require) {
searchSourceStubs = {
get: sinon.stub(),
toIndexList: sinon.stub().returns([]),
createRequest: sinon.stub().returns({
defer: Promise,
source: {
activeFetchCount: 0
}
createRequest: sinon.spy(function () {
return {
defer: Promise.defer(),
source: {
activeFetchCount: 0
}
};
})
};
mockSearchSource = {
@ -147,9 +149,44 @@ define(function (require) {
return abort;
});
it('should abort the existing fetch');
it('should abort the existing fetch', function () {
var loopCount = 3;
var queue = [];
for (var i = 0; i < 20; i++) {
queue.push('queue-index-' + i);
}
sinon.stub(SegmentedFetch.prototype, '_extractQueue', function () {
this.queue = queue;
});
sinon.stub(SegmentedFetch.prototype, '_executeSearch', function () {
return new Promise(function (resolve) {
resolve({
took: 10,
hits: {
total: 10,
max_score: 1,
hits: []
}
});
});
});
searchStrategy.getSourceStateFromRequest.returns(Promise.resolve({
body: {
size: 10
}
}));
var eachHandler = sinon.spy(function () {
if (eachHandler.callCount === loopCount) {
segmentedFetch.abort();
}
});
return segmentedFetch.fetch({ each: eachHandler }).then(function () {
expect(eachHandler.callCount).to.be(loopCount);
});
});
it('should abort the es promise');