diff options
author | Thomas Steur <tsteur@users.noreply.github.com> | 2020-03-05 06:40:33 +0300 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-03-05 06:40:33 +0300 |
commit | 6a3fc7d70e231f5b50f1df56c3741cd4c9535fdc (patch) | |
tree | 4d2a5b443db90641aa47c1bfff66234de69a7722 /tests/PHPUnit | |
parent | 0fd0b3039b23524e5959c2243a25fe2ff53db175 (diff) |
Try to prevent a deadlock when writing options (#15666)
Diffstat (limited to 'tests/PHPUnit')
-rw-r--r-- | tests/PHPUnit/Integration/CronArchiveTest.php | 25 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php | 46 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/Tracker/VisitTest.php | 17 | ||||
-rw-r--r-- | tests/PHPUnit/Integration/TrackerTest.php | 6 |
4 files changed, 72 insertions, 22 deletions
diff --git a/tests/PHPUnit/Integration/CronArchiveTest.php b/tests/PHPUnit/Integration/CronArchiveTest.php index 74f0dc561b..fa568d6b9b 100644 --- a/tests/PHPUnit/Integration/CronArchiveTest.php +++ b/tests/PHPUnit/Integration/CronArchiveTest.php @@ -40,12 +40,27 @@ class CronArchiveTest extends IntegrationTestCase $cronarchive->setApiToInvalidateArchivedReport($api); $cronarchive->init(); - $expectedInvalidations = array( - array(array(1,2), '2014-04-05'), - array(array(2), '2014-04-06') - ); + /** + * should look like this but the result is random + * array( + array(array(1,2), '2014-04-05'), + array(array(2), '2014-04-06') + ) + */ + $invalidatedReports = $api->getInvalidatedReports(); + $this->assertCount(2, $invalidatedReports); + sort($invalidatedReports[0][0]); + sort($invalidatedReports[1][0]); + usort($invalidatedReports, function ($a, $b) { + return strcmp($a[1], $b[1]); + }); + + $this->assertSame(array(1,2), $invalidatedReports[0][0]); + $this->assertSame('2014-04-05', $invalidatedReports[0][1]); + + $this->assertSame(array(2), $invalidatedReports[1][0]); + $this->assertSame('2014-04-06', $invalidatedReports[1][1]); - $this->assertEquals($expectedInvalidations, $api->getInvalidatedReports()); } public function test_setSegmentsToForceFromSegmentIds_CorrectlyGetsSegmentDefinitions_FromSegmentIds() diff --git a/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php b/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php index e5d60efddd..acb94e2fb9 100644 --- a/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php +++ b/tests/PHPUnit/Integration/DataAccess/ArchiveInvalidatorTest.php @@ -83,11 +83,12 @@ class ArchiveInvalidatorTest extends IntegrationTestCase { //Updated for change to allow for multiple transactions to invalidate the same report without deadlock. $key = 'report_to_invalidate_2_2014-04-05' . '_' . getmypid(); - $this->assertFalse(Option::get($key)); + $this->assertEmpty(Option::getLike('%'. $key . '%')); - $this->rememberReport(2, '2014-04-05'); + $keyStored = $this->rememberReport(2, '2014-04-05'); - $this->assertSame('1', Option::get($key)); + $this->assertStringEndsWith($key, $keyStored); + $this->assertSame('1', Option::get($keyStored)); } public function test_rememberToInvalidateArchivedReportsLater_shouldNotCreateEntryTwice() @@ -96,7 +97,7 @@ class ArchiveInvalidatorTest extends IntegrationTestCase $this->rememberReport(2, '2014-04-05'); $this->rememberReport(2, '2014-04-05'); - $this->assertCount(1, Option::getLike('report_to_invalidate%')); + $this->assertCount(1, Option::getLike('%report_to_invalidate%')); } public function test_getRememberedArchivedReportsThatShouldBeInvalidated_shouldNotReturnEntriesInCaseNoneAreRemembered() @@ -112,7 +113,22 @@ class ArchiveInvalidatorTest extends IntegrationTestCase $reports = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated(); - $this->assertSame($this->getRememberedReportsByDate(), $reports); + $this->assertSameReports($this->getRememberedReportsByDate(), $reports); + } + + private function assertSameReports($expected, $actual) + { + $keys1 = array_keys($expected); + $keys2 = array_keys($actual); + sort($keys1); + sort($keys2); + + $this->assertSame($keys1, $keys2); + foreach ($expected as $index => $values) { + sort($values); + sort($actual[$index]); + $this->assertSame($values, $actual[$index]); + } } public function test_forgetRememberedArchivedReportsToInvalidateForSite_shouldNotDeleteAnythingInCaseNoReportForThatSite() @@ -122,7 +138,7 @@ class ArchiveInvalidatorTest extends IntegrationTestCase $this->invalidator->forgetRememberedArchivedReportsToInvalidateForSite(10); $reports = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated(); - $this->assertSame($this->getRememberedReportsByDate(), $reports); + $this->assertSameReports($this->getRememberedReportsByDate(), $reports); } public function test_forgetRememberedArchivedReportsToInvalidateForSite_shouldOnlyDeleteReportsBelongingToThatSite() @@ -137,7 +153,7 @@ class ArchiveInvalidatorTest extends IntegrationTestCase '2014-05-05' => array(2, 5), '2014-04-06' => array(3) ); - $this->assertSame($expected, $reports); + $this->assertSameReports($expected, $reports); } public function test_forgetRememberedArchivedReportsToInvalidate_shouldNotForgetAnythingIfThereIsNoMatch() @@ -147,12 +163,12 @@ class ArchiveInvalidatorTest extends IntegrationTestCase // site does not match $this->invalidator->forgetRememberedArchivedReportsToInvalidate(10, Date::factory('2014-04-05')); $reports = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated(); - $this->assertSame($this->getRememberedReportsByDate(), $reports); + $this->assertSameReports($this->getRememberedReportsByDate(), $reports); // date does not match $this->invalidator->forgetRememberedArchivedReportsToInvalidate(7, Date::factory('2012-04-05')); $reports = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated(); - $this->assertSame($this->getRememberedReportsByDate(), $reports); + $this->assertSameReports($this->getRememberedReportsByDate(), $reports); } public function test_forgetRememberedArchivedReportsToInvalidate_shouldOnlyDeleteReportBelongingToThatSiteAndDate() @@ -169,13 +185,13 @@ class ArchiveInvalidatorTest extends IntegrationTestCase '2014-04-08' => array(7), '2014-05-08' => array(7), ); - $this->assertSame($expected, $reports); + $this->assertSameReports($expected, $reports); unset($expected['2014-05-08']); $this->invalidator->forgetRememberedArchivedReportsToInvalidate(7, Date::factory('2014-05-08')); $reports = $this->invalidator->getRememberedArchivedReportsThatShouldBeInvalidated(); - $this->assertSame($expected, $reports); + $this->assertSameReports($expected, $reports); } public function test_markArchivesAsInvalidated_shouldForgetInvalidatedSitesAndDates() @@ -198,21 +214,21 @@ class ArchiveInvalidatorTest extends IntegrationTestCase '2014-04-06' => array(3), '2014-05-08' => array(7), ); - $this->assertSame($expected, $reports); + $this->assertSameReports($expected, $reports); } private function rememberReport($idSite, $date) { $date = Date::factory($date); - $this->invalidator->rememberToInvalidateArchivedReportsLater($idSite, $date); + return $this->invalidator->rememberToInvalidateArchivedReportsLater($idSite, $date); } private function getRememberedReportsByDate() { return array( - '2014-04-05' => array(1, 2, 4, 7), - '2014-05-05' => array(2, 5), '2014-04-06' => array(3), + '2014-04-05' => array(4, 7, 2, 1), + '2014-05-05' => array(5, 2), '2014-04-08' => array(7), '2014-05-08' => array(7), ); diff --git a/tests/PHPUnit/Integration/Tracker/VisitTest.php b/tests/PHPUnit/Integration/Tracker/VisitTest.php index e62f6e6482..0ccc7f4953 100644 --- a/tests/PHPUnit/Integration/Tracker/VisitTest.php +++ b/tests/PHPUnit/Integration/Tracker/VisitTest.php @@ -451,7 +451,22 @@ class VisitTest extends IntegrationTestCase $archive = StaticContainer::get('Piwik\Archive\ArchiveInvalidator'); $remembered = $archive->getRememberedArchivedReportsThatShouldBeInvalidated(); - $this->assertSame($expectedRemeberedArchivedReports, $remembered); + $this->assertSameReportsInvalidated($expectedRemeberedArchivedReports, $remembered); + } + + private function assertSameReportsInvalidated($expected, $actual) + { + $keys1 = array_keys($expected); + $keys2 = array_keys($actual); + sort($keys1); + sort($keys2); + + $this->assertSame($keys1, $keys2); + foreach ($expected as $index => $values) { + sort($values); + sort($actual[$index]); + $this->assertSame($values, $actual[$index]); + } } private function prepareVisitWithRequest($requestParams, $requestDate) diff --git a/tests/PHPUnit/Integration/TrackerTest.php b/tests/PHPUnit/Integration/TrackerTest.php index cd0778a253..574aa3312c 100644 --- a/tests/PHPUnit/Integration/TrackerTest.php +++ b/tests/PHPUnit/Integration/TrackerTest.php @@ -385,8 +385,12 @@ class TrackerTest extends IntegrationTestCase $this->request->setCurrentTimestamp(Date::$now); $this->tracker->trackRequest($this->request); + $keys = Option::getLike('%report_to_invalidate_2_2019-04-02%'); + $this->assertCount(1, $keys); + $key = key($keys); + $this->assertStringEndsWith('report_to_invalidate_2_2019-04-02_' . getmypid(), $key); // make sure today archives are also invalidated - $this->assertEquals(['report_to_invalidate_2_2019-04-02_' . getmypid() => '1'], Option::getLike('report_to_invalidate_2_2019-04-02%')); + $this->assertEquals([$key => '1'], $keys); } public function test_TrackingNewVisitOfKnownVisitor() |