Skip to content

Commit

Permalink
refs matomo-org#4903 added tests and fixed some issues
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur committed Mar 25, 2014
1 parent 257d6c7 commit 08a0e53
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 3 deletions.
12 changes: 10 additions & 2 deletions core/ArchiveProcessor/FixedSiteIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@ class FixedSiteIds

public function __construct($websiteIds)
{
$this->siteIds = $websiteIds;
if (!empty($websiteIds)) {
$this->siteIds = $websiteIds;
}
}

/**
Expand All @@ -40,7 +42,13 @@ public function getNumSites()
*/
public function getNumProcessedWebsites()
{
return $this->index + 1;
$numProcessed = $this->index + 1;

if ($numProcessed > $this->getNumSites()) {
return $this->getNumSites();
}

return $numProcessed;
}

public function getNextSiteId()
Expand Down
14 changes: 14 additions & 0 deletions core/ArchiveProcessor/SharedSiteIds.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,14 @@ class SharedSiteIds
{
private $siteIds = array();
private $currentSiteId;
private $done = false;

public function __construct($websiteIds)
{
if (empty($websiteIds)) {
$websiteIds = array();
}

$self = $this;
$this->siteIds = $this->runExclusive(function () use ($self, $websiteIds) {
// if there are already sites to be archived registered, prefer the list of existing archive, meaning help
Expand Down Expand Up @@ -56,6 +61,10 @@ public function getNumSites()
*/
public function getNumProcessedWebsites()
{
if ($this->done) {
return $this->getNumSites();
}

if (empty($this->currentSiteId)) {
return 0;
}
Expand Down Expand Up @@ -103,6 +112,7 @@ public function getAllSiteIdsToArchive()
private function runExclusive($closure)
{
$process = new Process('archive.sharedsiteids');

while ($process->isRunning() && $process->getSecondsSinceCreation() < 5) {
// wait max 5 seconds, such an operation should not take longer
usleep(25);
Expand Down Expand Up @@ -145,6 +155,10 @@ public function getNextSiteId()
return $nextSiteId;
});

if (is_null($this->currentSiteId)) {
$this->done = true;
}

return $this->currentSiteId;
}

Expand Down
3 changes: 2 additions & 1 deletion core/CronArchive.php
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,8 @@ public function run()

// Also reprocess when day has ended since last run
if ($dayHasEndedMustReprocess
&& !$this->hasBeenProcessedSinceMidnight($idsite, $lastTimestampWebsiteProcessedDay) // it might have reprocessed for that day by another cron
// it might have reprocessed for that day by another cron
&& !$this->hasBeenProcessedSinceMidnight($idsite, $lastTimestampWebsiteProcessedDay)
&& !$existingArchiveIsValid) {
$skipDayArchive = false;
}
Expand Down
55 changes: 55 additions & 0 deletions tests/PHPUnit/Core/ArchiveProcessor/FixedSiteIdsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

use \Piwik\ArchiveProcessor\FixedSiteIds;

/**
* @group Core
*/
class FixedSiteIdsTest extends PHPUnit_Framework_TestCase
{
/**
* @var FixedSiteIds
*/
private $fixedSiteIds;

public function setUp()
{
$this->fixedSiteIds = new FixedSiteIds(array(1,2,5,9));
}

public function test_construct_withEmptyValue()
{
$siteIds = new FixedSiteIds(null);
$this->assertEquals(0, $siteIds->getNumSites());
$this->assertNull($siteIds->getNextSiteId());
}

public function test_getNumSites()
{
$this->assertEquals(4, $this->fixedSiteIds->getNumSites());
}

public function test_getNumProcessedWebsites_getNextSiteId()
{
$this->assertEquals(0, $this->fixedSiteIds->getNumProcessedWebsites());
$this->assertEquals(1, $this->fixedSiteIds->getNextSiteId());
$this->assertEquals(1, $this->fixedSiteIds->getNumProcessedWebsites());
$this->assertEquals(2, $this->fixedSiteIds->getNextSiteId());
$this->assertEquals(2, $this->fixedSiteIds->getNumProcessedWebsites());
$this->assertEquals(5, $this->fixedSiteIds->getNextSiteId());
$this->assertEquals(3, $this->fixedSiteIds->getNumProcessedWebsites());
$this->assertEquals(9, $this->fixedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->fixedSiteIds->getNumProcessedWebsites());

$this->assertNull($this->fixedSiteIds->getNextSiteId());
$this->assertEquals(4, $this->fixedSiteIds->getNumProcessedWebsites());
}


}
108 changes: 108 additions & 0 deletions tests/PHPUnit/Core/ArchiveProcessor/SharedSiteIdsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php
/**
* Piwik - Open source web analytics
*
* @link http://piwik.org
* @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
*/

use \Piwik\ArchiveProcessor\SharedSiteIds;

/**
* @group Core
*/
class SharedSiteIdsTest extends PHPUnit_Framework_TestCase
{
/**
* @var SharedSiteIds
*/
private $sharedSiteIds;

public function setUp()
{
$this->sharedSiteIds = new SharedSiteIds(array(1,2,5,9));
}

public function tearDown()
{
$this->sharedSiteIds->setSiteIdsToArchive(array());
}

public function test_construct_withEmptyValue()
{
$this->sharedSiteIds->setSiteIdsToArchive(array());

$siteIds = new SharedSiteIds(null);
$this->assertEquals(0, $siteIds->getNumSites());
$this->assertNull($siteIds->getNextSiteId());
}

public function test_isSupported()
{
$this->assertTrue(SharedSiteIds::isSupported());
}

public function test_getNumSites()
{
$this->assertEquals(4, $this->sharedSiteIds->getNumSites());
}

public function test_getAllSiteIdsToArchive()
{
$this->assertEquals(array(1,2,5,9), $this->sharedSiteIds->getAllSiteIdsToArchive());
}

public function test_getNumProcessedWebsites_getNextSiteId()
{
$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 test_usingMultipleSharedSiteIds()
{
$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());
}

}

0 comments on commit 08a0e53

Please sign in to comment.