refactor: Use GoogleAuth#fetch and Fix Tests#1766
Conversation
| auth | ||
| .getRequestHeaders() | ||
| .then(authHeader => { | ||
| const fetchRequest: RequestInit = { | ||
| headers: gaxios.Gaxios.mergeHeaders(authHeader, headers) as {}, | ||
| body: fetchParameters.body as string | Buffer | undefined, | ||
| method: fetchParameters.method, | ||
| signal: cancelSignal, | ||
| }; | ||
| if (agentOption) { | ||
| fetchRequest.agent = agentOption; | ||
| } | ||
| if ( | ||
| fetchParameters.method === 'GET' || | ||
| fetchParameters.method === 'DELETE' | ||
| ) { | ||
| delete fetchRequest['body']; | ||
| } | ||
| return fetch(url, fetchRequest as {}); | ||
| }) | ||
| .fetch(url, fetchRequest) |
There was a problem hiding this comment.
Using GoogleAuth#fetch to streamline and improve mocking options.
gax/test/unit/apiCallable.ts
Outdated
| } | ||
| const apiCall = createApiCall(func); | ||
| await apiCall(42, undefined, (err, resp) => { | ||
| void apiCall(42, undefined, (err, resp) => { |
There was a problem hiding this comment.
Used void to signal that this is a promise we intend to handle in floating async manner. The stubs by default are not compatible with traditional async/await patterns.
gax/test/unit/apiCallable.ts
Outdated
| assert.ok(deadlineArg); | ||
| done(); | ||
| }) | ||
| .catch(done); |
There was a problem hiding this comment.
This .catch(done) will allow the test to fail faster than using .catch(console.error);
There was a problem hiding this comment.
does this also need the void like the other tests?
re catch, great, love a faster fail
There was a problem hiding this comment.
Good question - the ones with the .catch(done) don't require the void as the .catch handles the floating async lint warning
| setMockFallbackResponse( | ||
| gaxGrpc, | ||
| new Response(Buffer.from(JSON.stringify(response))), | ||
| ); |
There was a problem hiding this comment.
This new utility makes it easier and simple to mock responses using the native Response object. Notice there are no fake mocks and no casting - just real data types which makes this easier to maintain.
| const gaxGrpcMock = new GrpcClient(); | ||
| setMockFallbackResponse( | ||
| gaxGrpc, | ||
| new Response(Buffer.from(JSON.stringify(jsonError)), {status: 400}), |
There was a problem hiding this comment.
...it even supports errors in a simple way
leahecole
left a comment
There was a problem hiding this comment.
it seems like there's a few things going on in this PR:
- there's the work that was done to fix the fallback tests
- there's also additional work - changing the browser tests
- and the majority of these changes are updates to many of the unit tests and how we handle the async nature of them
The browser test changes are small, and given that they have some relevance to fetch + fallback from the linked issue, I would be okay grouping those in the PR, but I feel strongly that the other unit tests changes should be put in a separate PR (that I'm happy to review!)
gax/test/unit/apiCallable.ts
Outdated
| assert.ok(deadlineArg); | ||
| done(); | ||
| }) | ||
| .catch(done); |
There was a problem hiding this comment.
does this also need the void like the other tests?
re catch, great, love a faster fail
GoogleAuth#fetch and Fix TestsGoogleAuth#fetch and Fix Tests
leahecole
left a comment
There was a problem hiding this comment.
Thanks for the some of the fixes to these comments! This PR still needs to be split up into multiple PRs that represent the discrete aspects of what is going on (fixing the fallback stuff, fixing the timing stuff, and potentially having the browser tests as a small third)- this will not only make it so #1737 is fixed faster (which means work on BigQuery modernization will be unblocked and has positive business impact! yay!) but having smaller PRs is safer in the unfortunate event that we did need to roll back a PR.
As mentioned here, we will need the browser changes in the PR. To further expand:
To expedite this I would be happy to provide a walkthrough of these changes. |
I am willing to leave them in this PR, this is fine.
Understood - however, linked issue googleapis/google-cloud-node-core#182 issue specifically calls out the grpc-fallback tests - I think that fixing all of the async issues and tests is valid, but needs to be split into two PRs:
Understood, this makes sense. I think this is a reasonable alternative and a good choice given the circumstances. No issues with this choice.
I appreciate the offer, but am happy to async review both PRs when this has been split up! |
Thanks for clarifying - I’ll split out the async/void changes from this PR. Just noting here, that if the other async/void PR isn’t merged, this library would remain in an unstable state. |
| done(); | ||
| }); | ||
| }) | ||
| .catch(done); |
There was a problem hiding this comment.
If the assertion fails in 278, it's not caught by the .catch - the test instead fails because it times out after 2000ms. Was the intent of the new .catch to avoid that? If not, and it's fulfilling another purpose, I suspect that we had this problem before and I'll open an issue, but if so, it might need a small adjustment
There was a problem hiding this comment.
This catch is for the createStub call. We could further improve by fast-failing here - would you like me to add that?
There was a problem hiding this comment.
given that we had this issue before, I will open up an issue to track and fast follow. The tests do fail, but we can make em "fail better". Opened a tracking issue https://github.com/googleapis/gax-nodejs/issues/1769
gax/test/unit/grpc-fallback.ts
Outdated
| setMockFallbackResponse( | ||
| gaxGrpc, | ||
| new Response(JSON.stringify({}), { | ||
| status: 403, |
There was a problem hiding this comment.
why is the status 403 instead of OK like it was when we were attempting to proxyquire?
There was a problem hiding this comment.
Good catch - it didn't make a difference for the tested functionality, but I can make it match.
gax/test/unit/grpc-fallback.ts
Outdated
| JSON.stringify(err.statusDetails), | ||
| JSON.stringify(expectedError.details), | ||
| ); | ||
| done(); |
There was a problem hiding this comment.
I'm not sure how quickly it was failing before, but right now even with the done in place, if the assertion error fails, it times out instead of failing fast
There was a problem hiding this comment.
gax/test/unit/regapic.ts
Outdated
| it('should not send prettyPrint setting when json minification is not requested', done => { | ||
| const requestObject = {name: 'shelves/shelf-name'}; | ||
| const responseObject = { | ||
| name: 'shelf-name', | ||
| theme: 'shelf-theme', | ||
| type: 100, // unknown enum value |
There was a problem hiding this comment.
what are these json minification tests? did they not exist before?
There was a problem hiding this comment.
That's strange - I merged and repaired from main and I fixed them here. Is this something you want removed or spun-off?
There was a problem hiding this comment.
ugh hate when git syncs don't go as planned!
yeah if it doesn't have to do with this PR, let's spin it off into another in the name of small PRs please!
gax/test/unit/regapic.ts
Outdated
| }); | ||
| }); | ||
|
|
||
| describe('should support json minification', () => { |
There was a problem hiding this comment.
added #1770 to track this quick re-add!
Description
This PR aims to:
Using
GoogleAuth#fetchstreamlines allows us to streamline the code and simplify the unit tests as we can use a nativeResponseobject.*system-tests are not included and are still flaky.
Impact
In addition to the above, as a side-effect we no longer need to directly import
node-fetchwithin this library. We can remove in a separate PR.Testing
Lots of testing improvements.
Additional Information
gax/src/proxyquire, which won't be supported in an ESM future, along with building improved patterns around using mock data with real data types (without casting).nockraises anAbortControllererror when ran due to our overwriting the native AbortController. Fortunately, theGoogleAuth#fetch/Responseroute is more performant, however we shouldn't modify/replace native objects (anti-pattern).BEGIN_COMMIT_OVERRIDE
fix: passing gaxios fetch through auth
END_COMMIT_OVERRIDE
Fixes: googleapis/google-cloud-node-core#182 🦕