-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5182 from epixa/4342-fixing-master
Fix timing issue when fetching pre-flighted requests
- Loading branch information
Showing
5 changed files
with
164 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
describe('ui/courier/fetch/_fetch_these', () => { | ||
const _ = require('lodash'); | ||
const sinon = require('auto-release-sinon'); | ||
const expect = require('expect.js'); | ||
const ngMock = require('ngMock'); | ||
|
||
let Promise; | ||
let $rootScope; | ||
let fetchThese; | ||
let request; | ||
let requests; | ||
let fakeResponses; | ||
|
||
beforeEach(ngMock.module('kibana', (PrivateProvider) => { | ||
function FakeResponsesProvider(Promise) { | ||
fakeResponses = sinon.spy(function () { | ||
return Promise.map(requests, mockRequest => { | ||
return { mockRequest }; | ||
}); | ||
}); | ||
return fakeResponses; | ||
} | ||
|
||
PrivateProvider.swap(require('ui/courier/fetch/_call_client'), FakeResponsesProvider); | ||
PrivateProvider.swap(require('ui/courier/fetch/_call_response_handlers'), FakeResponsesProvider); | ||
PrivateProvider.swap(require('ui/courier/fetch/_continue_incomplete'), FakeResponsesProvider); | ||
})); | ||
|
||
beforeEach(ngMock.inject((Private, $injector) => { | ||
$rootScope = $injector.get('$rootScope'); | ||
Promise = $injector.get('Promise'); | ||
fetchThese = Private(require('ui/courier/fetch/_fetch_these')); | ||
request = mockRequest(); | ||
requests = [ request ]; | ||
})); | ||
|
||
context('when request has not started', () => { | ||
beforeEach(() => requests.forEach(req => req.started = false)); | ||
|
||
it('starts request', () => { | ||
fetchThese(requests); | ||
expect(request.start.called).to.be(true); | ||
expect(request.continue.called).to.be(false); | ||
}); | ||
|
||
it('waits for returned promise from start() to be fulfilled', () => { | ||
request.start = sinon.stub().returns(Promise.resolve(request)); | ||
fetchThese(requests); | ||
|
||
expect(request.start.callCount).to.be(1); | ||
expect(fakeResponses.callCount).to.be(0); | ||
$rootScope.$apply(); | ||
expect(fakeResponses.callCount).to.be(3); | ||
}); | ||
}); | ||
|
||
context('when request has already started', () => { | ||
it('continues request', () => { | ||
fetchThese(requests); | ||
expect(request.start.called).to.be(false); | ||
expect(request.continue.called).to.be(true); | ||
}); | ||
it('waits for returned promise to be fulfilled', () => { | ||
request.continue = sinon.stub().returns(Promise.resolve(request)); | ||
fetchThese(requests); | ||
|
||
expect(request.continue.callCount).to.be(1); | ||
expect(fakeResponses.callCount).to.be(0); | ||
$rootScope.$apply(); | ||
expect(fakeResponses.callCount).to.be(3); | ||
}); | ||
}); | ||
|
||
function mockRequest() { | ||
return { | ||
strategy: 'mock', | ||
started: true, | ||
aborted: false, | ||
retry: sinon.spy(function () { return this; }), | ||
continue: sinon.spy(function () { return this; }), | ||
start: sinon.spy(function () { return this; }) | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
55 changes: 55 additions & 0 deletions
55
src/ui/public/courier/fetch/request/__tests__/segmented.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
describe('ui/courier/fetch/request/segmented', () => { | ||
const sinon = require('auto-release-sinon'); | ||
const expect = require('expect.js'); | ||
const ngMock = require('ngMock'); | ||
|
||
let Promise; | ||
let $rootScope; | ||
let SegmentedReq; | ||
let segmentedReq; | ||
let searchReqStart; | ||
|
||
beforeEach(ngMock.module('kibana')); | ||
|
||
beforeEach(ngMock.inject((Private, $injector) => { | ||
Promise = $injector.get('Promise'); | ||
$rootScope = $injector.get('$rootScope'); | ||
SegmentedReq = Private(require('ui/courier/fetch/request/segmented')); | ||
searchReqStart = sinon.spy(Private(require('ui/courier/fetch/request/search')).prototype, 'start'); | ||
})); | ||
|
||
describe('#start()', () => { | ||
let returned; | ||
beforeEach(() => { | ||
init(); | ||
returned = segmentedReq.start(); | ||
}); | ||
|
||
it('returns promise', () => { | ||
expect(returned.then).to.be.Function; | ||
}); | ||
|
||
it('does not call super.start() until promise is resolved', () => { | ||
expect(searchReqStart.called).to.be(false); | ||
$rootScope.$apply(); | ||
expect(searchReqStart.called).to.be(true); | ||
}); | ||
}); | ||
|
||
function init() { | ||
segmentedReq = new SegmentedReq(mockSource()); | ||
} | ||
|
||
function mockSource() { | ||
return { | ||
get: sinon.stub().returns(mockIndexPattern()) | ||
}; | ||
} | ||
|
||
function mockIndexPattern() { | ||
const queue = [1, 2, 3]; | ||
return { | ||
toIndexList: sinon.stub().returns(Promise.resolve(queue)) | ||
}; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters