Skip to content
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

Remove unnecessary setTimeout in test files #2699

Closed
chinesedfan opened this issue Jan 28, 2020 · 3 comments
Closed

Remove unnecessary setTimeout in test files #2699

chinesedfan opened this issue Jan 28, 2020 · 3 comments

Comments

@chinesedfan
Copy link
Collaborator

@chinesedfan chinesedfan commented Jan 28, 2020

There are lots of setTimeout in our tests. Some are unnecessary, i.e.

// test/unit/adapters/http.js

axios.get('http://localhost:4444/', {
  maxContentLength: 2000
}).then(function (res) {
  success = true;
}).catch(function (err) {
  error = err;
  failure = true;
});

setTimeout(function () {
  assert.equal(success, false, 'request should not succeed');
  assert.equal(failure, true, 'request should fail');
  assert.equal(error.message, 'maxContentLength size of 2000 exceeded');
  done();
}, 100);
@chinesedfan
Copy link
Collaborator Author

@chinesedfan chinesedfan commented Jan 29, 2020

@yasuf I came up with a kind of explanation that it makes sure done will be called within 100ms, no matter whether axios.get is succeed or failed. Though you may think writing in a then next to catch can also achieve that, the current CI runs in about 2 mins, which means the time is not an emergency to consider.

@reviewher
Copy link

@reviewher reviewher commented Feb 18, 2020

@chinesedfan the timeout is to give the test a little bit of time to run. In that particular test, the get has to be performed and the catch block has to run (since that sets failure = true). Running the checks synchronously or with timeout 0 is too early to guarantee the axios part has time to run.

The tests probably should use Promise#finally:

      axios.get('http://localhost:4444/', {
        maxContentLength: 2000
      }).then(function (res) {
        success = true;
      }).catch(function (err) {
        error = err;
        failure = true;
      }).finally(function () {
        assert.equal(success, false, 'request should not succeed');
        assert.equal(failure, true, 'request should fail');
        assert.equal(error.message, 'maxContentLength size of 2000 exceeded');
        done();
      });

The problem is browser compatibility. For example, chrome 32+ support catch but finally requires chrome 63+

@chinesedfan
Copy link
Collaborator Author

@chinesedfan chinesedfan commented Feb 19, 2020

The tests probably should use Promise#finally

Exactly. And even a second then can act as finally without compatibility problems.

it makes sure done will be called within 100ms

I think that's the biggest advantage. If someone breaks axios to be never resolved or rejected, we don't need wait the case to timeout.

@axios axios locked and limited conversation to collaborators May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
2 participants
You can’t perform that action at this time.