Skip to content

Bug: DateHelperTest::testNowSpecific depends on time #6684

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

Conversation

fpoy
Copy link
Contributor

@fpoy fpoy commented Oct 14, 2022

Fixes #5492

Description

In the testNowSpecific() method, there are two different now() calls

There may be sometimes a latency between the two calls, and by default assertCloseEnough test if the difference is lower than 1 second.

To reproduce, just add a delay sleep(n) inside the now() function

A solution may be to increase the tolerance threshold to an acceptable level (eg 10 sec for 2 hours should be sufficient)

Checklist:

  • Securely signed commits
  • Component(s) with PHPDoc blocks, only if necessary or adds value
  • Unit testing, with >80% coverage
  • User guide updated
  • Conforms to style guide

@kenjis
Copy link
Member

kenjis commented Oct 14, 2022

The true solution is to stop time during testing.
But for the time being this PR may be okay.

To be hoest, I would like to deprecate the now() function,
or at least to deprecate the parameter $timezone.
Because I don't understand Unix timestamp with timezone.

@kenjis kenjis added the testing Pull requests that changes tests only label Oct 14, 2022
// Chicago should be two hours ahead of Vancouver
$this->assertCloseEnough(7200, now('America/Chicago') - now('America/Vancouver'));
// Chicago should be two hours ahead of Vancouver (+- 10 sec)
$this->assertCloseEnough(7200, now('America/Chicago') - now('America/Vancouver'), '', 10);
Copy link
Member

Choose a reason for hiding this comment

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

After all, this test is strange.
It is difficult to accept the specification that a 10-second error is not a problem.

Copy link
Member

@kenjis kenjis left a comment

Choose a reason for hiding this comment

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

Sorry, I don't accept this.
Because test code is a specification.

@MGatner
Copy link
Member

MGatner commented Oct 15, 2022

Is the test actually failing? 1 second should be ample time to cover any single test case (especially that doesn't involve database or HTTP).

@kenjis
Copy link
Member

kenjis commented Oct 25, 2022

1) CodeIgniter\Helpers\DateHelperTest::testNowSpecific
Failed asserting that 4 is equal to 1 or is less than 1.

/home/runner/work/CodeIgniter4/CodeIgniter4/system/Test/CIUnitTestCase.php:478
/home/runner/work/CodeIgniter4/CodeIgniter4/tests/system/Helpers/DateHelperTest.php:36
phpvfscomposer:///home/runner/work/CodeIgniter4/CodeIgniter4/vendor/phpunit/phpunit/phpunit:97

https://github.com/codeigniter4/CodeIgniter4/actions/runs/3317017866/jobs/5479434095

@kenjis kenjis mentioned this pull request Oct 25, 2022
4 tasks
@kenjis
Copy link
Member

kenjis commented Oct 25, 2022

I sent another PR #6753

@kenjis
Copy link
Member

kenjis commented Nov 5, 2022

Closed by #6753

@kenjis kenjis closed this Nov 5, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
testing Pull requests that changes tests only
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Bug: DateHelperTest::testNowSpecific depends on time
3 participants