Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/matomo-org/matomo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiosmosis <diosmosis@users.noreply.github.com>2019-07-29 04:45:08 +0300
committerGitHub <noreply@github.com>2019-07-29 04:45:08 +0300
commit521eb11cc27491b3274f585f45ef92e611e40a09 (patch)
treef6d8f96e604310d328f000ca6ab7caf5bc1458b0 /tests/PHPUnit/Integration
parent70e4a7d90bec20cd42a4553e03cf77aac4ea48d7 (diff)
Move queued tracking lock implementation to core for other uses. (#14686)
* Move queued tracking lock implementation to core for other uses. * tweaks * Fix up PR and move table creation to schema since it is simpler. * fix namespace
Diffstat (limited to 'tests/PHPUnit/Integration')
-rw-r--r--tests/PHPUnit/Integration/Concurrency/LockBackend/MysqlLockBackendTest.php189
-rw-r--r--tests/PHPUnit/Integration/Concurrency/LockTest.php141
2 files changed, 330 insertions, 0 deletions
diff --git a/tests/PHPUnit/Integration/Concurrency/LockBackend/MysqlLockBackendTest.php b/tests/PHPUnit/Integration/Concurrency/LockBackend/MysqlLockBackendTest.php
new file mode 100644
index 0000000000..432491591b
--- /dev/null
+++ b/tests/PHPUnit/Integration/Concurrency/LockBackend/MysqlLockBackendTest.php
@@ -0,0 +1,189 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link https://matomo.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ *
+ */
+
+namespace Piwik\Tests\Integration\Concurrency\LockBackend;
+
+use Piwik\Concurrency\LockBackend\MySqlLockBackend;
+use Piwik\Db;
+use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
+
+class MysqlLockBackendTest extends IntegrationTestCase
+{
+ /**
+ * @var MySQLLockBackend
+ */
+ private $backend;
+ private $key = 'testKeyValueKey';
+
+ public function setUp()
+ {
+ if (!$this->hasDependencies()) {
+ parent::setUp();
+ }
+
+ // ensure tracker DB will be used
+ Db::destroyDatabaseObject();
+ $GLOBALS['PIWIK_TRACKER_MODE'] = true;
+
+ $this->backend = $this->createMysqlBackend();
+ }
+
+ public function tearDown()
+ {
+ $GLOBALS['PIWIK_TRACKER_MODE'] = false;
+ Db::destroyDatabaseObject();
+ parent::tearDown();
+ }
+
+ protected function createMysqlBackend()
+ {
+ return new MySQLLockBackend();
+ }
+
+ public function test_deleteIfKeyHasValue_ShouldNotWork_IfKeyDoesNotExist()
+ {
+ $success = $this->backend->deleteIfKeyHasValue('inVaLidKeyTest', '1');
+ $this->assertFalse($success);
+ }
+
+ public function test_deleteIfKeyHasValue_ShouldWork_ShouldBeAbleToDeleteARegularKey()
+ {
+ $success = $this->backend->setIfNotExists($this->key, 'test', 60);
+ $this->assertTrue($success);
+
+ $success = $this->backend->deleteIfKeyHasValue($this->key, 'test');
+ $this->assertTrue($success);
+ }
+
+ public function test_deleteIfKeyHasValue_ShouldNotWork_IfValueIsDifferent()
+ {
+ $this->backend->setIfNotExists($this->key, 'test', 60);
+
+ $success = $this->backend->deleteIfKeyHasValue($this->key, 'test2');
+ $this->assertFalse($success);
+ }
+
+ public function test_setIfNotExists_ShouldWork_IfNoValueIsSetYet()
+ {
+ $success = $this->backend->setIfNotExists($this->key, 'value', 60);
+ $this->assertTrue($success);
+ }
+
+ /**
+ * @depends test_setIfNotExists_ShouldWork_IfNoValueIsSetYet
+ */
+ public function test_setIfNotExists_ShouldNotWork_IfValueIsAlreadySet()
+ {
+ $success = $this->backend->setIfNotExists($this->key, 'value', 60);
+ $this->assertFalse($success);
+ }
+
+ /**
+ * @depends test_setIfNotExists_ShouldNotWork_IfValueIsAlreadySet
+ */
+ public function test_setIfNotExists_ShouldAlsoNotWork_IfTryingToSetDifferentValue()
+ {
+ $success = $this->backend->setIfNotExists($this->key, 'another val', 60);
+ $this->assertFalse($success);
+ }
+
+ public function test_get_ShouldReturnFalse_IfKeyNotSet()
+ {
+ $value = $this->backend->get($this->key);
+ $this->assertFalse($value);
+ }
+
+ public function test_get_ShouldReturnTheSetValue_IfOneIsSet()
+ {
+ $this->backend->setIfNotExists($this->key, 'mytest', 60);
+ $value = $this->backend->get($this->key);
+ $this->assertEquals('mytest', $value);
+ }
+
+ public function test_keyExists_ShouldReturnFalse_IfKeyNotSet()
+ {
+ $value = $this->backend->keyExists($this->key);
+ $this->assertFalse($value);
+ }
+
+ public function test_get_ShouldReturnTrueIfValueIsSet()
+ {
+ $this->backend->setIfNotExists($this->key, 'mytest', 60);
+ $this->assertTrue($this->backend->keyExists($this->key));
+ }
+
+ public function test_expire_ShouldWork()
+ {
+ $success = $this->backend->setIfNotExists($this->key, 'test', 60);
+ $this->assertTrue($success);
+
+ $success = $this->backend->expireIfKeyHasValue($this->key, 'test', $seconds = 1);
+ $this->assertTrue($success);
+
+ // should not work as value still saved and not expired yet
+ $success = $this->backend->setIfNotExists($this->key, 'test', 60);
+ $this->assertFalse($success);
+
+ sleep($seconds + 1);
+
+ // value is expired and should work now!
+ $success = $this->backend->setIfNotExists($this->key, 'test', 60);
+ $this->assertTrue($success);
+ }
+
+ public function test_expire_ShouldNotWorkIfValueIsDifferent()
+ {
+ $success = $this->backend->setIfNotExists($this->key, 'test', 60);
+ $this->assertTrue($success);
+
+ $success = $this->backend->expireIfKeyHasValue($this->key, 'test2', $seconds = 1);
+ $this->assertFalse($success);
+ }
+
+ public function test_expire_ShouldStilReturnTrueEvenWhenSettingSameTimeout()
+ {
+ $success = $this->backend->setIfNotExists($this->key, 'test', 60);
+ $this->assertTrue($success);
+
+ $success = $this->backend->expireIfKeyHasValue($this->key, 'test', 60);
+ $this->assertTrue($success);
+
+ $success = $this->backend->expireIfKeyHasValue($this->key, 'test', 60);
+ $this->assertTrue($success);
+ }
+
+ public function test_getKeysMatchingPattern_shouldReturnMatchingKeys()
+ {
+ $backend = $this->createMysqlBackend();
+ $backend->setIfNotExists('abcde', 'val0', 100);
+ $backend->setIfNotExists('test1', 'val1', 100);
+ $backend->setIfNotExists('Test3', 'val2', 100);
+ $backend->setIfNotExists('Test1', 'val3', 100);
+ $backend->setIfNotExists('Test2', 'val4', 100);
+
+ $keys = $backend->getKeysMatchingPattern('Test*');
+ sort($keys);
+ $this->assertEquals(array('Test2', 'Test3', 'test1'), $keys);
+
+ $keys = $backend->getKeysMatchingPattern('test1*');
+ sort($keys);
+ $this->assertEquals(array('test1'), $keys);
+
+ $keys = $backend->getKeysMatchingPattern('*est*');
+ sort($keys);
+ $this->assertEquals(array('Test2', 'Test3', 'test1'), $keys);
+ }
+
+ public function test_getKeysMatchingPattern_shouldReturnAnEmptyArrayIfNothingMatches()
+ {
+ $backend = $this->createMysqlBackend();
+ $keys = $backend->getKeysMatchingPattern('*fere*');
+ $this->assertEquals(array(), $keys);
+ }
+}
diff --git a/tests/PHPUnit/Integration/Concurrency/LockTest.php b/tests/PHPUnit/Integration/Concurrency/LockTest.php
new file mode 100644
index 0000000000..cb1345c2f2
--- /dev/null
+++ b/tests/PHPUnit/Integration/Concurrency/LockTest.php
@@ -0,0 +1,141 @@
+<?php
+/**
+ * Piwik - free/libre analytics platform
+ *
+ * @link http://piwik.org
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GPL v3 or later
+ */
+
+namespace Piwik\Tests\Integration\Concurrency;
+
+
+use Piwik\Concurrency\Lock;
+use Piwik\Concurrency\LockBackend\MySqlLockBackend;
+use Piwik\Tests\Framework\TestCase\IntegrationTestCase;
+
+/**
+ * @group Core
+ */
+class LockTest extends IntegrationTestCase
+{
+ /**
+ * @var Lock
+ */
+ public $lock;
+
+ public function setUp()
+ {
+ parent::setUp();
+
+ $mysql = new MySqlLockBackend();
+ $this->lock = $this->createLock($mysql);
+ }
+
+ public function tearDown()
+ {
+ parent::tearDown();
+ }
+
+ public function test_acquireLock_ShouldLockInCaseItIsNotLockedYet()
+ {
+ $this->assertTrue($this->lock->acquireLock(0));
+ $this->assertFalse($this->lock->acquireLock(0));
+
+ $this->lock->unlock();
+
+ $this->assertTrue($this->lock->acquireLock(0));
+ $this->assertFalse($this->lock->acquireLock(0));
+ }
+
+ public function test_acquireLock_ShouldBeAbleToLockMany()
+ {
+ $this->assertTrue($this->lock->acquireLock(0));
+ $this->assertFalse($this->lock->acquireLock(0));
+ $this->assertTrue($this->lock->acquireLock(1));
+ $this->assertTrue($this->lock->acquireLock(2));
+ $this->assertFalse($this->lock->acquireLock(1));
+ }
+
+ public function test_isLocked_ShouldDetermineWhetherALockIsLocked()
+ {
+ $this->assertFalse($this->lock->isLocked());
+ $this->lock->acquireLock(0);
+
+ $this->assertTrue($this->lock->isLocked());
+
+ $this->lock->unlock();
+
+ $this->assertFalse($this->lock->isLocked());
+ }
+
+ public function test_unlock_OnlyUnlocksTheLastOne()
+ {
+ $this->assertTrue($this->lock->acquireLock(0));
+ $this->assertTrue($this->lock->acquireLock(1));
+ $this->assertTrue($this->lock->acquireLock(2));
+
+ $this->lock->unlock();
+
+ $this->assertFalse($this->lock->acquireLock(0));
+ $this->assertFalse($this->lock->acquireLock(1));
+ $this->assertTrue($this->lock->acquireLock(2));
+ }
+
+ public function test_expireLock_ShouldReturnTrueOnSuccess()
+ {
+ $this->lock->acquireLock(0);
+ $this->assertTrue($this->lock->expireLock(2));
+ }
+
+ public function test_expireLock_ShouldReturnFalseIfNoTimeoutGiven()
+ {
+ $this->lock->acquireLock(0);
+ $this->assertFalse($this->lock->expireLock(0));
+ }
+
+ public function test_expireLock_ShouldReturnFalseIfNotLocked()
+ {
+ $this->assertFalse($this->lock->expireLock(2));
+ }
+
+ public function test_getNumberOfAcquiredLocks_shouldReturnNumberOfLocks()
+ {
+ $this->assertNumberOfLocksEquals(0);
+
+ $this->lock->acquireLock(0);
+ $this->assertNumberOfLocksEquals(1);
+
+ $this->lock->acquireLock(4);
+ $this->lock->acquireLock(5);
+ $this->assertNumberOfLocksEquals(3);
+
+ $this->lock->unlock();
+ $this->assertNumberOfLocksEquals(2);
+ }
+
+ public function test_getAllAcquiredLockKeys_shouldReturnUsedKeysThatAreLocked()
+ {
+ $this->assertSame(array(), $this->lock->getAllAcquiredLockKeys());
+
+ $this->lock->acquireLock(0);
+ $this->assertSame(array('TestLock0'), $this->lock->getAllAcquiredLockKeys());
+
+ $this->lock->acquireLock(4);
+ $this->lock->acquireLock(5);
+
+ $locks = $this->lock->getAllAcquiredLockKeys();
+ sort($locks);
+ $this->assertSame(array('TestLock0', 'TestLock4', 'TestLock5'), $locks);
+ }
+
+ private function assertNumberOfLocksEquals($numExpectedLocks)
+ {
+ $this->assertSame($numExpectedLocks, $this->lock->getNumberOfAcquiredLocks());
+ }
+
+ private function createLock($mysql)
+ {
+ return new Lock($mysql, 'TestLock');
+ }
+
+}