File size: 6,686 Bytes
bf8c0ce | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | <?php
/**
* Matomo - free/libre analytics platform
*
* @link https://matomo.org
* @license https://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/
namespace Piwik\Tests\Integration;
use Piwik\CronArchive\SharedSiteIds;
use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
/**
* @group Core
* @group SharedSiteIdsTest
*/
class SharedSiteIdsTest extends IntegrationTestCase
{
/**
* @var SharedSiteIds
*/
private $sharedSiteIds;
public function setUp(): void
{
parent::setUp();
if (!SharedSiteIds::isSupported()) {
$this->markTestSkipped('Not supported on this platform');
return;
}
$this->sharedSiteIds = $this->makeSharedSiteIds(array(1,2,5,9));
}
public function tearDown(): void
{
if (!SharedSiteIds::isSupported()) {
return;
}
$siteIdsToCleanup = $this->makeSharedSiteIds(array());
$siteIdsToCleanup->setSiteIdsToArchive(array());
parent::tearDown();
}
private function makeSharedSiteIds($siteIds, $optionalKey = SharedSiteIds::OPTION_DEFAULT)
{
return new SharedSiteIds($siteIds, $optionalKey);
}
public function testConstructWithEmptyValue()
{
$this->sharedSiteIds->setSiteIdsToArchive(array());
$siteIds = new SharedSiteIds(null);
$this->assertEquals(0, $siteIds->getNumSites());
$this->assertNull($siteIds->getNextSiteId());
}
public function testConstructWithCustomOptionName()
{
$first = new SharedSiteIds(array(1, 2), 'SharedSiteIdsToArchive_Test');
$second = new SharedSiteIds(array(), 'SharedSiteIdsToArchive_Test');
$this->assertEquals(array(1, 2), $first->getAllSiteIdsToArchive());
$this->assertEquals(array(1, 2), $second->getAllSiteIdsToArchive());
}
public function testGetNumSites()
{
$this->assertEquals(4, $this->sharedSiteIds->getNumSites());
}
public function testGetAllSiteIdsToArchive()
{
$this->assertEquals(array(1,2,5,9), $this->sharedSiteIds->getAllSiteIdsToArchive());
}
public function testGetNumProcessedWebsitesGetNextSiteId()
{
$this->assertEquals(0, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(1, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(1, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(2, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(2, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(5, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(3, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(9, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertNull($this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
}
public function testGetNextSiteIdShouldDetectWhenTheQueueHasBeenResetMeanwhile()
{
$this->assertEquals(1, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(2, $this->sharedSiteIds->getNextSiteId());
// we fake to reset the sharedSiteIds by another process
$this->sharedSiteIds->setQueueWasReset();
// it detects that sites must have been processed by now
$this->assertNull($this->sharedSiteIds->getNextSiteId());
}
public function testGetNextSiteIdQueueWithOnlyOneSite()
{
$sharedSiteIds = $this->makeSharedSiteIds(array(1), 'mytest');
$this->assertEquals(1, $sharedSiteIds->getNextSiteId());
$this->assertNull($sharedSiteIds->getNextSiteId());
// still returns null even when calling it again
$this->assertNull($sharedSiteIds->getNextSiteId());
}
public function testUsingMultipleSharedSiteIds()
{
$second = new SharedSiteIds(array(7,9,11,6,1,2));
// should ignore his queue and help processing the existing queue
$this->assertEquals(4, $second->getNumSites());
$this->assertEquals(4, $this->sharedSiteIds->getNumSites());
$this->assertEquals(array(1,2,5,9), $second->getAllSiteIdsToArchive());
$this->assertEquals(1, $second->getNextSiteId());
$this->assertEquals(1, $second->getNumProcessedWebsites());
$this->assertEquals(array(2,5,9), $this->sharedSiteIds->getAllSiteIdsToArchive());
$this->assertEquals(2, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(2, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(array(5,9), $second->getAllSiteIdsToArchive());
$this->assertEquals(5, $second->getNextSiteId());
$this->assertEquals(3, $second->getNumProcessedWebsites());
$this->assertEquals(array(9), $this->sharedSiteIds->getAllSiteIdsToArchive());
$this->assertEquals(9, $this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertNull($second->getNextSiteId());
$this->assertEquals(4, $second->getNumProcessedWebsites());
$this->assertEquals(array(), $second->getAllSiteIdsToArchive());
$this->assertNull($this->sharedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->sharedSiteIds->getNumProcessedWebsites());
$this->assertEquals(array(), $this->sharedSiteIds->getAllSiteIdsToArchive());
}
public function testUsingMultipleSharedSiteIdsDetectsFinishedAlready()
{
$this->sharedSiteIds = $this->makeSharedSiteIds(array(1), 'test');
// should ignore his queue and help processing the existing queue
$this->assertEquals(1, $this->sharedSiteIds->getNumSites());
// process the first and only site, the queue should be empty afterwards and will be reset next time
$this->assertEquals(1, $this->sharedSiteIds->getNextSiteId());
$second = $this->makeSharedSiteIds(array(1), 'test');
$this->assertEquals(1, $second->getNumSites()); // now the second will init the sites back
// should return null as it already processed site 1 before meaning there must have been a "reset" of sites
// within one archive run we do not want to process same siteID twice as we prefer the archiver to exit and then
// the next archiver works on that site again. Otherwise there could be race conditions where a core:archive
// process basically never ends
$this->assertNull($this->sharedSiteIds->getNextSiteId());
}
}
|