From aac059525beadf56153539c732fd953bc0888c77 Mon Sep 17 00:00:00 2001 From: Tim Otten Date: Sat, 6 Jun 2020 15:21:01 -0700 Subject: [PATCH] CommunityMessages - Define more consistent behavior in the face of slow execution This passes under some brutally slow scenarios: ``` for TIME_FUNC in natural frozen linear:500 linear:1250 prng:500 prng:666 prng:1000 prng:1500 ; do export TIME_FUNC; echo; echo "TIME_FUNC=$TIME_FUNC" ; env CIVICRM_UF=UnitTests phpunit6 tests/phpunit/CRM/Core/CommunityMessagesTest.php ; done ``` --- CRM/Core/CommunityMessages.php | 7 ++++--- tests/phpunit/CRM/Core/CommunityMessagesTest.php | 6 +++--- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/CRM/Core/CommunityMessages.php b/CRM/Core/CommunityMessages.php index ff76489da4..081457351c 100644 --- a/CRM/Core/CommunityMessages.php +++ b/CRM/Core/CommunityMessages.php @@ -95,15 +95,16 @@ class CRM_Core_CommunityMessages { $isChanged = TRUE; } - if ($document['expires'] <= CRM_Utils_Time::getTimeRaw()) { + $refTime = CRM_Utils_Time::getTimeRaw(); + if ($document['expires'] <= $refTime) { $newDocument = $this->fetchDocument(); if ($newDocument && $this->validateDocument($newDocument)) { $document = $newDocument; - $document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['ttl']; + $document['expires'] = $refTime + $document['ttl']; } else { // keep the old messages for now, try again later - $document['expires'] = CRM_Utils_Time::getTimeRaw() + $document['retry']; + $document['expires'] = $refTime + $document['retry']; } $isChanged = TRUE; } diff --git a/tests/phpunit/CRM/Core/CommunityMessagesTest.php b/tests/phpunit/CRM/Core/CommunityMessagesTest.php index 6ebda73e48..a2c8d800eb 100644 --- a/tests/phpunit/CRM/Core/CommunityMessagesTest.php +++ b/tests/phpunit/CRM/Core/CommunityMessagesTest.php @@ -19,7 +19,7 @@ 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; + const APPROX_TIME_EQUALITY = 4; /** * @var CRM_Utils_Cache_Interface @@ -293,14 +293,14 @@ class CRM_Core_CommunityMessagesTest extends CiviUnitTestCase { $this->assertEquals($doc2['expires'], $doc3['expires']); // fourth try, $doc2 has expired yet; new request; replace data - CRM_Utils_Time::setTime('2013-03-01 12:10:02'); + CRM_Utils_Time::setTime('2013-03-01 12:10:05'); $communityMessages = new CRM_Core_CommunityMessages( $this->cache, $this->expectOneHttpRequest(self::$webResponses['second-valid-response']) ); $doc4 = $communityMessages->getDocument(); $this->assertEquals('

Second valid response

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