From 6a522e8b3e703ab6d6b557859e8d70d5f12775f0 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Thu, 17 Jul 2014 16:42:45 -0700 Subject: [PATCH] CRM_Core_CommunityMessagesTest - Reduce probability of test-failure due to timing --- .../phpunit/CRM/Core/CommunityMessagesTest.php | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/tests/phpunit/CRM/Core/CommunityMessagesTest.php b/tests/phpunit/CRM/Core/CommunityMessagesTest.php index a3002fc90f..7e4ee6e12b 100644 --- a/tests/phpunit/CRM/Core/CommunityMessagesTest.php +++ b/tests/phpunit/CRM/Core/CommunityMessagesTest.php @@ -33,6 +33,13 @@ require_once 'CiviTest/CiviUnitTestCase.php'; * Class CRM_Core_CommunityMessagesTest */ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { + + /** + * The max difference between two times such that they should be + * treated as equals (expressed in seconds). + */ + const APPROX_TIME_EQUALITY = 2; + /** * @var CRM_Utils_Cache_Interface */ @@ -189,7 +196,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { ); $doc1 = $communityMessages->getDocument(); $this->assertEquals('

First valid response

', $doc1['messages'][0]['markup']); - $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']); + $this->assertApproxEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires'], self::APPROX_TIME_EQUALITY); // second try, $doc1 hasn't expired yet, so still use it CRM_Utils_Time::setTime('2013-03-01 10:09:00'); @@ -199,7 +206,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { ); $doc2 = $communityMessages->getDocument(); $this->assertEquals('

First valid response

', $doc2['messages'][0]['markup']); - $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc2['expires']); + $this->assertApproxEquals(strtotime('2013-03-01 10:10:00'), $doc2['expires'], self::APPROX_TIME_EQUALITY); // third try, $doc1 expired, update it CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) @@ -209,7 +216,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { ); $doc3 = $communityMessages->getDocument(); $this->assertEquals('

Second valid response

', $doc3['messages'][0]['markup']); - $this->assertEquals(strtotime('2013-03-01 12:10:02'), $doc3['expires']); + $this->assertApproxEquals(strtotime('2013-03-01 12:10:02'), $doc3['expires'], self::APPROX_TIME_EQUALITY); } /** @@ -274,7 +281,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { ); $doc1 = $communityMessages->getDocument(); $this->assertEquals('

First valid response

', $doc1['messages'][0]['markup']); - $this->assertEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires']); + $this->assertApproxEquals(strtotime('2013-03-01 10:10:00'), $doc1['expires'], self::APPROX_TIME_EQUALITY); // second try, $doc1 has expired; bad response; keep old data CRM_Utils_Time::setTime('2013-03-01 12:00:02'); // more than 2 hours later (DEFAULT_RETRY) @@ -304,7 +311,7 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { ); $doc4 = $communityMessages->getDocument(); $this->assertEquals('

Second valid response

', $doc4['messages'][0]['markup']); - $this->assertEquals(strtotime('2013-03-01 12:20:02'), $doc4['expires']); + $this->assertApproxEquals(strtotime('2013-03-01 12:20:02'), $doc4['expires'], self::APPROX_TIME_EQUALITY); } /** -- 2.25.1