mirror of
https://git.sekbaer.de/Friendica/friendica.git
synced 2025-06-16 20:05:14 +02:00
Adding multihost - locking
Adding Unit-Tests for it
This commit is contained in:
parent
b07dfbb03f
commit
aac94d1d74
22 changed files with 741 additions and 154 deletions
|
@ -14,7 +14,7 @@ user:
|
|||
uid: 42
|
||||
username: Test user
|
||||
nickname: selfcontact
|
||||
verified: true
|
||||
verified: 1
|
||||
password: $2y$10$DLRNTRmJgKe1cSrFJ5Jb0edCqvXlA9sh/RHdSnfxjbR.04yZRm4Qm
|
||||
theme: frio
|
||||
|
||||
|
@ -24,12 +24,12 @@ contact:
|
|||
uid: 42
|
||||
name: Self contact
|
||||
nick: selfcontact
|
||||
self: true
|
||||
self: 1
|
||||
nurl: http://localhost/profile/selfcontact
|
||||
url: http://localhost/profile/selfcontact
|
||||
about: User used in tests
|
||||
pending: false
|
||||
blocked: false
|
||||
pending: 0
|
||||
blocked: 0
|
||||
rel: 1
|
||||
network: dfrn
|
||||
-
|
||||
|
@ -39,11 +39,11 @@ contact:
|
|||
# the fallback to api_get_nick() in api_get_user()
|
||||
name: othercontact
|
||||
nick: othercontact
|
||||
self: false
|
||||
self: 0
|
||||
nurl: http://localhost/profile/othercontact
|
||||
url: http://localhost/profile/othercontact
|
||||
pending: false
|
||||
blocked: false
|
||||
pending: 0
|
||||
blocked: 0
|
||||
rel: 0
|
||||
network: dfrn
|
||||
-
|
||||
|
@ -51,150 +51,150 @@ contact:
|
|||
uid: 0
|
||||
name: Friend contact
|
||||
nick: friendcontact
|
||||
self: false
|
||||
self: 0
|
||||
nurl: http://localhost/profile/friendcontact
|
||||
url: http://localhost/profile/friendcontact
|
||||
pending: false
|
||||
blocked: false
|
||||
pending: 0
|
||||
blocked: 0
|
||||
rel: 2
|
||||
network: dfrn
|
||||
|
||||
item:
|
||||
-
|
||||
id: 1
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 42
|
||||
author-id: 42
|
||||
owner-id: 45
|
||||
uid: 42
|
||||
verb: http://activitystrea.ms/schema/1.0/post
|
||||
unseen: true
|
||||
unseen: 1
|
||||
body: Parent status
|
||||
parent: 1
|
||||
author-link: http://localhost/profile/selfcontact
|
||||
wall: true
|
||||
starred: true
|
||||
origin: true
|
||||
wall: 1
|
||||
starred: 1
|
||||
origin: 1
|
||||
allow_cid: ''
|
||||
allow_gid: ''
|
||||
deny_cid: ''
|
||||
deny_gid: ''
|
||||
-
|
||||
id: 2
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 42
|
||||
author-id: 42
|
||||
owner-id: 45
|
||||
uid: 42
|
||||
verb: http://activitystrea.ms/schema/1.0/post
|
||||
unseen: false
|
||||
unseen: 0
|
||||
body: Reply
|
||||
parent: 1
|
||||
author-link: http://localhost/profile/selfcontact
|
||||
wall: true
|
||||
starred: false
|
||||
origin: true
|
||||
wall: 1
|
||||
starred: 0
|
||||
origin: 1
|
||||
-
|
||||
id: 3
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 43
|
||||
author-id: 43
|
||||
owner-id: 42
|
||||
uid: 42
|
||||
verb: http://activitystrea.ms/schema/1.0/post
|
||||
unseen: false
|
||||
unseen: 0
|
||||
body: Other user status
|
||||
parent: 3
|
||||
author-link: http://localhost/profile/othercontact
|
||||
wall: true
|
||||
starred: false
|
||||
origin: true
|
||||
wall: 1
|
||||
starred: 0
|
||||
origin: 1
|
||||
-
|
||||
id: 4
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 44
|
||||
author-id: 44
|
||||
owner-id: 42
|
||||
uid: 42
|
||||
verb: http://activitystrea.ms/schema/1.0/post
|
||||
unseen: false
|
||||
unseen: 0
|
||||
body: Friend user reply
|
||||
parent: 1
|
||||
author-link: http://localhost/profile/othercontact
|
||||
wall: true
|
||||
starred: false
|
||||
origin: true
|
||||
wall: 1
|
||||
starred: 0
|
||||
origin: 1
|
||||
-
|
||||
id: 5
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 42
|
||||
author-id: 42
|
||||
owner-id: 42
|
||||
uid: 42
|
||||
verb: http://activitystrea.ms/schema/1.0/post
|
||||
unseen: false
|
||||
unseen: 0
|
||||
body: '[share]Shared status[/share]'
|
||||
parent: 1
|
||||
author-link: http://localhost/profile/othercontact
|
||||
wall: true
|
||||
starred: false
|
||||
origin: true
|
||||
wall: 1
|
||||
starred: 0
|
||||
origin: 1
|
||||
allow_cid: ''
|
||||
allow_gid: ''
|
||||
deny_cid: ''
|
||||
deny_gid: ''
|
||||
-
|
||||
id: 6
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 44
|
||||
author-id: 44
|
||||
owner-id: 42
|
||||
uid: 42
|
||||
verb: http://activitystrea.ms/schema/1.0/post
|
||||
unseen: false
|
||||
unseen: 0
|
||||
body: Friend user status
|
||||
parent: 6
|
||||
author-link: http://localhost/profile/othercontact
|
||||
wall: true
|
||||
starred: false
|
||||
origin: true
|
||||
wall: 1
|
||||
starred: 0
|
||||
origin: 1
|
||||
|
||||
thread:
|
||||
-
|
||||
iid: 1
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 42
|
||||
author-id: 42
|
||||
owner-id: 42
|
||||
uid: 42
|
||||
wall: true
|
||||
wall: 1
|
||||
-
|
||||
iid: 3
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 43
|
||||
author-id: 43
|
||||
owner-id: 43
|
||||
uid: 0
|
||||
wall: true
|
||||
wall: 1
|
||||
-
|
||||
iid: 6
|
||||
visible: true
|
||||
visible: 1
|
||||
contact-id: 44
|
||||
author-id: 44
|
||||
owner-id: 44
|
||||
uid: 0
|
||||
wall: true
|
||||
wall: 1
|
||||
|
||||
group:
|
||||
-
|
||||
id: 1
|
||||
uid: 42
|
||||
visible: true
|
||||
visible: 1
|
||||
name: Visible list
|
||||
-
|
||||
id: 2
|
||||
uid: 42
|
||||
visible: false
|
||||
visible: 0
|
||||
name: Private list
|
||||
|
||||
search:
|
||||
|
|
27
tests/src/Core/Lock/CacheLockDriverTest.php
Normal file
27
tests/src/Core/Lock/CacheLockDriverTest.php
Normal file
|
@ -0,0 +1,27 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
|
||||
use Friendica\Core\Cache\ArrayCache;
|
||||
use Friendica\Core\Lock\CacheLockDriver;
|
||||
|
||||
class CacheLockDriverTest extends LockTest
|
||||
{
|
||||
/**
|
||||
* @var \Friendica\Core\Cache\IMemoryCacheDriver
|
||||
*/
|
||||
private $cache;
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
$this->cache = new ArrayCache();
|
||||
return new CacheLockDriver($this->cache);
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
$this->cache->clear();
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
67
tests/src/Core/Lock/DatabaseLockDriverTest.php
Normal file
67
tests/src/Core/Lock/DatabaseLockDriverTest.php
Normal file
|
@ -0,0 +1,67 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use dba;
|
||||
use Friendica\Core\Lock\DatabaseLockDriver;
|
||||
use Friendica\Database\DBStructure;
|
||||
use PHPUnit\DbUnit\DataSet\YamlDataSet;
|
||||
use PHPUnit\DbUnit\TestCaseTrait;
|
||||
use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
|
||||
|
||||
class DatabaseLockDriverTest extends LockTest
|
||||
{
|
||||
use TestCaseTrait;
|
||||
|
||||
/**
|
||||
* Get database connection.
|
||||
*
|
||||
* This function is executed before each test in order to get a database connection that can be used by tests.
|
||||
* If no prior connection is available, it tries to create one using the USER, PASS and DB environment variables.
|
||||
*
|
||||
* If it could not connect to the database, the test is skipped.
|
||||
*
|
||||
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
|
||||
* @see https://phpunit.de/manual/5.7/en/database.html
|
||||
*/
|
||||
protected function getConnection()
|
||||
{
|
||||
if (!dba::$connected) {
|
||||
dba::connect('localhost', getenv('USER'), getenv('PASS'), getenv('DB'));
|
||||
|
||||
if (dba::$connected) {
|
||||
$app = get_app();
|
||||
// We need to do this in order to disable logging
|
||||
$app->module = 'install';
|
||||
|
||||
// Create database structure
|
||||
DBStructure::update(false, true, true);
|
||||
} else {
|
||||
$this->markTestSkipped('Could not connect to the database.');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->createDefaultDBConnection(dba::get_db(), getenv('DB'));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get dataset to populate the database with.
|
||||
* @return YamlDataSet
|
||||
* @see https://phpunit.de/manual/5.7/en/database.html
|
||||
*/
|
||||
protected function getDataSet()
|
||||
{
|
||||
return new YamlDataSet(__DIR__ . '/../../../datasets/api.yml');
|
||||
}
|
||||
|
||||
protected function getInstance()
|
||||
{
|
||||
return new DatabaseLockDriver();
|
||||
}
|
||||
|
||||
public function tearDown()
|
||||
{
|
||||
dba::delete('locks', [ 'id > 0']);
|
||||
parent::tearDown();
|
||||
}
|
||||
}
|
80
tests/src/Core/Lock/LockTest.php
Normal file
80
tests/src/Core/Lock/LockTest.php
Normal file
|
@ -0,0 +1,80 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
use Friendica\App;
|
||||
use Friendica\Core\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
abstract class LockTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Friendica\Core\Lock\ILockDriver
|
||||
*/
|
||||
protected $instance;
|
||||
|
||||
abstract protected function getInstance();
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
global $a;
|
||||
parent::setUp();
|
||||
$this->instance = $this->getInstance();
|
||||
|
||||
// Reusable App object
|
||||
$this->app = new App(__DIR__.'/../');
|
||||
$a = $this->app;
|
||||
|
||||
// Default config
|
||||
Config::set('config', 'hostname', 'localhost');
|
||||
Config::set('system', 'throttle_limit_day', 100);
|
||||
Config::set('system', 'throttle_limit_week', 100);
|
||||
Config::set('system', 'throttle_limit_month', 100);
|
||||
Config::set('system', 'theme', 'system_theme');
|
||||
}
|
||||
|
||||
public function testLock() {
|
||||
$this->instance->acquireLock('foo', 1);
|
||||
$this->assertTrue($this->instance->isLocked('foo'));
|
||||
$this->assertFalse($this->instance->isLocked('bar'));
|
||||
}
|
||||
|
||||
public function testDoubleLock() {
|
||||
$this->instance->acquireLock('foo', 1);
|
||||
$this->assertTrue($this->instance->isLocked('foo'));
|
||||
// We already locked it
|
||||
$this->assertTrue($this->instance->acquireLock('foo', 1));
|
||||
}
|
||||
|
||||
public function testReleaseLock() {
|
||||
$this->instance->acquireLock('foo', 1);
|
||||
$this->assertTrue($this->instance->isLocked('foo'));
|
||||
$this->instance->releaseLock('foo');
|
||||
$this->assertFalse($this->instance->isLocked('foo'));
|
||||
}
|
||||
|
||||
public function testReleaseAll() {
|
||||
$this->instance->acquireLock('foo', 1);
|
||||
$this->instance->acquireLock('bar', 1);
|
||||
$this->instance->acquireLock('#/$%§', 1);
|
||||
|
||||
$this->instance->releaseAll();
|
||||
|
||||
$this->assertFalse($this->instance->isLocked('foo'));
|
||||
$this->assertFalse($this->instance->isLocked('bar'));
|
||||
$this->assertFalse($this->instance->isLocked('#/$%§'));
|
||||
}
|
||||
|
||||
public function testReleaseAfterUnlock() {
|
||||
$this->instance->acquireLock('foo', 1);
|
||||
$this->instance->acquireLock('bar', 1);
|
||||
$this->instance->acquireLock('#/$%§', 1);
|
||||
|
||||
$this->instance->releaseLock('foo');
|
||||
|
||||
$this->instance->releaseAll();
|
||||
|
||||
$this->assertFalse($this->instance->isLocked('bar'));
|
||||
$this->assertFalse($this->instance->isLocked('#/$%§'));
|
||||
}
|
||||
}
|
14
tests/src/Core/Lock/SemaphoreLockDriverTest.php
Normal file
14
tests/src/Core/Lock/SemaphoreLockDriverTest.php
Normal file
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
|
||||
namespace Friendica\Test\src\Core\Lock;
|
||||
|
||||
|
||||
use Friendica\Core\Lock\SemaphoreLockDriver;
|
||||
|
||||
class SemaphoreLockDriverTest extends LockTest
|
||||
{
|
||||
protected function getInstance()
|
||||
{
|
||||
return new SemaphoreLockDriver();
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue