Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($httpBackend): fixup patch for #9979 #9993

Closed
wants to merge 2 commits into from

Conversation

caitp
Copy link
Contributor

@caitp caitp commented Nov 10, 2014

Builds ontop of c480462 from #9979

gyx1000 and others added 2 commits November 10, 2014 18:29
httpBackend with ngMock browser.defer can never cancel the first deferredFn because the timeoutId returned by defer for the first fn is a zero value.
Compare timeoutId with undefined fix this issue.
@mary-poppins
Copy link

I'm sorry, but I wasn't able to verify your Contributor License Agreement (CLA) signature. CLA signature is required for any code contributions to AngularJS.

Please sign our CLA and ensure that the CLA signature email address and the email address in this PR's commits match.

If you signed the CLA as a corporation, please let us know the company's name.

Thanks a bunch!

PS: If you signed the CLA in the past then most likely the email addresses don't match. Please sign the CLA again or update the email address in the commit of this PR.
PS2: If you are a Googler, please sign the CLA as well to simplify the CLA verification process.

@@ -126,7 +126,9 @@ function createHttpBackend($browser, createXhr, $browserDefer, callbacks, rawDoc

function completeRequest(callback, status, response, headersString, statusText) {
// cancel timeout and subsequent timeout promise resolution
timeoutId && $browserDefer.cancel(timeoutId);
if (timeoutId !== void 0) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why the void 0 instead of undefined ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

smaller, can't be rebound accidentally

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Considering undefined is used throughout the code (in fact I'be never seen any void 0 occurrence in the core), this just introduces inconsistency and leaves maintainers wonder what's special about this case.
I am very surprised that this suggestion came from a maintainability advocate like you 😃

We should either change all occurrences to void 0 or none.
And considering that there are already meassures taken against rebinding undefined, I think using undefined is pretty safe.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tend to agree with @gkalpak - void 0 looks more "magical" and it is not the pattern we are using in the other parts of the code base. I would stick to if (timeoutId !== undefined) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would rather we change other cases of undefined to void 0 in the tree instead. void 0 is by definition an undefined value, but it's not a binding identifier, so accidents don't happen with it. That, and it's smaller. It's not something worth worrying about though, tbh

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@caitp I hear what you say but maybe let's focus on landing this PR open a separate issue / PR if care about reworking all undefined to void 0.

What do you think about my comment regarding tests? Do you want to land yours that is similar to the existing one or just change the counter to start from 0?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think ideally, the counter should just restart for each test case, it doesn't make a lot of sense to have it continue across tests, and it doesn't seem to be used very much in the tree. But I'd rather not worry about it for this CL

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, so should we change void 0 => undefined and land this one with the test you've created? I'm still a bit worried about its name / location in the file....

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so should we change void 0 => undefined

I don't see why.

I'm still a bit worried about its name / location in the file....

I'm not, but if it makes you feel better it could be rearranged while landing.

@pkozlowski-opensource pkozlowski-opensource self-assigned this Nov 11, 2014
@pkozlowski-opensource
Copy link
Member

@caitp I've left some minor comments. Looking at the tests, though, I've realised that we are not using mocked browser.deffer (BTW, there is even a TODO from Vojta for this..) which makes the test look funny...

As noted your test looks very similar to the one that already exists and changing this line from fakeTimeoutId = 0 to fakeTimeoutId = -1 also exposes the issue we are trying to fix here with an existing test.

So it is up to you to decide if we want to have a separate test or just start fake timeout ids counter from 0.

@gyx1000
Copy link
Contributor

gyx1000 commented Nov 11, 2014

Right, the TODO from Vojta will not work if this issue is not fixed. (I discovered it when I try to complete this todo).This test

it('should cancel timeout on completion', function() {
will fail.

@pkozlowski-opensource
Copy link
Member

@caitp I was about to merge this one but then I've realised that there is something wrong with a test - it seems to be passing even without a fix. It might be that I've messed up something with my env so could you please verify that your test breaks if the fix is not present?

On a separate note we should really do something about the way timeouts are mocked in the httpBackendSpec - it is rather messy atm...

@gyx1000
Copy link
Contributor

gyx1000 commented Nov 18, 2014

@pkozlowski-opensource in this comment #9979 (comment) I asked you to use the defer mock instead of write a new test because the fix will be tested by

it('should cancel timeout on completion', function() {
Can I make this change ? Change made in my branch https://github.com/gyx1000/angular.js/tree/compare-timeoutid-with-undefined-in-completerequest

@pkozlowski-opensource
Copy link
Member

@gyx1000 definitivelly - if we are able to replace fakeTimeout with defer mock - this would be even better! Could you please open a new pull request with a fix and changes to the tests?

@pkozlowski-opensource
Copy link
Member

We've got a version that gets rid of shaky tests: #10177 - let's focus on this one.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants