-
Notifications
You must be signed in to change notification settings - Fork 83
fix (event processor) [OASIS-5907]: stop promise tracks in-flight dispatcher requests #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4908691
Event processor tracks requests
d2b4bfd
Add paren
4511b90
Request tracker
ad81f91
Req tracker test
11f9726
Formatting
880cf90
Just return thee request tracker promise - queue promise is not needed
5b53ccb
CHANGELOG entry
befc6bf
Update license year
c224a06
Fix changelog language
e91a06e
Add comments
5615b07
In changelog, call this new feature instead of fix
79cca5f
Remove backticks from doc comments
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,64 @@ | ||
/** | ||
* Copyright 2020, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
import RequestTracker from '../src/requestTracker' | ||
|
||
describe('requestTracker', () => { | ||
describe('onRequestsComplete', () => { | ||
it('returns an immediately-fulfilled promise when no requests are in flight', async () => { | ||
const tracker = new RequestTracker() | ||
await tracker.onRequestsComplete() | ||
}) | ||
|
||
it('returns a promise that fulfills after in-flight requests are complete', async () => { | ||
let resolveReq1: () => void | ||
const req1 = new Promise<void>(resolve => { | ||
resolveReq1 = resolve | ||
}) | ||
let resolveReq2: () => void | ||
const req2 = new Promise<void>(resolve => { | ||
resolveReq2 = resolve | ||
}) | ||
let resolveReq3: () => void | ||
const req3 = new Promise<void>(resolve => { | ||
resolveReq3 = resolve | ||
}) | ||
|
||
const tracker = new RequestTracker() | ||
tracker.trackRequest(req1) | ||
tracker.trackRequest(req2) | ||
tracker.trackRequest(req3) | ||
|
||
let reqsComplete = false | ||
const reqsCompletePromise = tracker.onRequestsComplete().then(() => { | ||
reqsComplete = true | ||
}) | ||
|
||
resolveReq1!() | ||
await req1 | ||
expect(reqsComplete).toBe(false) | ||
|
||
resolveReq2!() | ||
await req2 | ||
expect(reqsComplete).toBe(false) | ||
|
||
resolveReq3!() | ||
await req3 | ||
await reqsCompletePromise | ||
expect(reqsComplete).toBe(true) | ||
}) | ||
}) | ||
}) |
This file contains hidden or 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 hidden or 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 hidden or 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,60 @@ | ||
/** | ||
* Copyright 2020, Optimizely | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* RequestTracker keeps track of in-flight requests for EventProcessor using | ||
* an internal counter. It exposes methods for adding a new request to be | ||
* tracked, and getting a Promise representing the completion of currently | ||
* tracked requests. | ||
*/ | ||
class RequestTracker { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: please include a doc comment to describe this class |
||
private reqsInFlightCount: number = 0 | ||
private reqsCompleteResolvers: Array<() => void> = [] | ||
|
||
/** | ||
* Track the argument request (represented by a Promise). reqPromise will feed | ||
* into the state of Promises returned by onRequestsComplete. | ||
* @param {Promise<void>} reqPromise | ||
*/ | ||
public trackRequest(reqPromise: Promise<void>): void { | ||
this.reqsInFlightCount++ | ||
const onReqComplete = () => { | ||
this.reqsInFlightCount-- | ||
if (this.reqsInFlightCount === 0) { | ||
this.reqsCompleteResolvers.forEach(resolver => resolver()) | ||
this.reqsCompleteResolvers = [] | ||
} | ||
} | ||
reqPromise.then(onReqComplete, onReqComplete) | ||
} | ||
|
||
/** | ||
* Return a Promise that fulfills after all currently-tracked request promises | ||
* are resolved. | ||
* @return {Promise<void>} | ||
*/ | ||
public onRequestsComplete(): Promise<void> { | ||
return new Promise(resolve => { | ||
if (this.reqsInFlightCount === 0) { | ||
resolve() | ||
} else { | ||
this.reqsCompleteResolvers.push(resolve) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
export default RequestTracker |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we be awaiting this also? Does it matter if onRequestsComplete resolves before this?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's redundant. The
Promise
returned fromqueue.stop
is based on the request it triggers at that time, but all requests are also tracked by theRequestTracker
.