-
Notifications
You must be signed in to change notification settings - Fork 753
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
Add request start and end time to request and response events #673
Merged
Merged
Changes from all commits
Commits
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 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
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 |
---|---|---|
|
@@ -324,15 +324,18 @@ describe('Flows', function() { | |
const idempotencyKey = Math.random() | ||
.toString(36) | ||
.slice(2); | ||
const lowerBoundStartTime = Date.now(); | ||
|
||
function onRequest(request) { | ||
expect(request).to.eql({ | ||
api_version: 'latest', | ||
idempotency_key: idempotencyKey, | ||
method: 'POST', | ||
path: '/v1/charges', | ||
account: connectedAccountId, | ||
}); | ||
expect(request.api_version).to.equal('latest'); | ||
expect(request.idempotency_key).to.equal(idempotencyKey); | ||
expect(request.account).to.equal(connectedAccountId); | ||
expect(request.method).to.equal('POST'); | ||
expect(request.path).to.equal('/v1/charges'); | ||
expect(request.request_start_time).to.be.within( | ||
lowerBoundStartTime, | ||
Date.now() | ||
); | ||
|
||
done(); | ||
} | ||
|
@@ -379,7 +382,13 @@ describe('Flows', function() { | |
expect(response.path).to.equal('/v1/charges'); | ||
expect(response.request_id).to.match(/req_[\w\d]/); | ||
expect(response.status).to.equal(402); | ||
expect(response.elapsed).to.be.within(50, 30000); | ||
expect(response.elapsed).to.equal( | ||
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. nice! |
||
response.request_end_time - response.request_start_time | ||
); | ||
expect(response.request_end_time).to.be.within( | ||
response.request_end_time, | ||
Date.now() | ||
); | ||
|
||
done(); | ||
} | ||
|
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.
Just a note that in general, I don't like tests that rely on a real clock and prefer using dependency injection or even mocks/stubs to make the test more deterministic... Not sure if we have an established pattern for doing this in stripe-node. We can probably keep this as-is and revisit if the test ever becomes flaky.
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 concur with you on your sentiments on tests like this. I went with this approach because I saw this pattern used for testing the elapsed time so I figured it was what Stripe expects/prefers. Perhaps just a type check of these values to ensure they are a number would be a better option?
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.
While I generally also dislike relying on the real clock, I don't think we have delightful mocking available at this time.
That said, we can probably set the starting bound to be a
Date.now()
set at the top of the test, which should be both more accurate and robust. Similarly, below, in an ideal world we'd check thatresponse.request_end_time
is greater thanrequest.request_start_time
(which would require adding anonRequest
handler).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.
Good suggestions @rattrayalex-stripe, I went ahead and added the request_start_time to the response event as well as that lets me more easily implement your request_end_time greater that request_start_time test as well as improve the elapsed test to not be vulnerable to a bizarre long request. Plus I suspect for some people's usage it will be beneficial for adding logging of the start and end time of any Stripe request. I also made the set lower bound at start of test change. This should be ready for you to review again. Thanks!
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.
Great thinking! Thanks!