namespace Friendica\Test\src\Core\Lock;
-use dba;
+use Friendica\Core\Cache;
use Friendica\Core\Lock\DatabaseLockDriver;
-use Friendica\Database\DBStructure;
-use PHPUnit\DbUnit\DataSet\YamlDataSet;
-use PHPUnit\DbUnit\TestCaseTrait;
-use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
+use Friendica\Test\Util\DbaLockMockTrait;
+/**
+ * @runTestsInSeparateProcesses
+ * @preserveGlobalState disabled
+ */
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()
+ use DbaLockMockTrait;
+
+ protected $pid = 123;
+
+ protected function setUp()
{
- 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'));
+ $this->mockConnected();
+ $this->mockConnect();
+
+ $this->mockReleaseAll($this->pid, 2);
+
+ parent::setUp();
}
- /**
- * Get dataset to populate the database with.
- * @return YamlDataSet
- * @see https://phpunit.de/manual/5.7/en/database.html
- */
- protected function getDataSet()
+ protected function getInstance()
{
- return new YamlDataSet(__DIR__ . '/../../../datasets/api.yml');
+ return new DatabaseLockDriver($this->pid);
}
- protected function getInstance()
+ public function testLock()
+ {
+ $this->mockIsLocked('foo', false, $this->startTime, 1);
+ $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+ $this->mockIsLocked('foo', true, $this->startTime, 1);
+ $this->mockIsLocked('bar', false, $this->startTime, 1);
+
+ parent::testLock();
+ }
+
+ public function testDoubleLock()
+ {
+ $this->mockIsLocked('foo', false, $this->startTime, 1);
+ $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+ $this->mockIsLocked('foo', true, $this->startTime, 1);
+ $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, true, $this->pid, true, $this->startTime, 1);
+
+ parent::testDoubleLock();
+ }
+
+ public function testReleaseLock()
{
- return new DatabaseLockDriver();
+ $this->mockIsLocked('foo', false, $this->startTime, 1);
+ $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+ $this->mockIsLocked('foo', true, $this->startTime, 1);
+ $this->mockReleaseLock('foo', $this->pid, 1);
+ $this->mockIsLocked('foo', false, $this->startTime, 1);
+
+ parent::testReleaseLock();
+ }
+
+ public function testReleaseAll()
+ {
+ $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+ $this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+ $this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+
+ $this->mockIsLocked('foo', true, $this->startTime, 1);
+ $this->mockIsLocked('bar', true, $this->startTime, 1);
+ $this->mockIsLocked('nice', true, $this->startTime, 1);
+
+ $this->mockReleaseAll($this->pid, 1);
+
+ $this->mockIsLocked('foo', false, $this->startTime, 1);
+ $this->mockIsLocked('bar', false, $this->startTime, 1);
+ $this->mockIsLocked('nice', false, $this->startTime, 1);
+
+ parent::testReleaseAll();
}
- public function tearDown()
+ public function testReleaseAfterUnlock()
{
- dba::delete('locks', [ 'id > 0']);
- parent::tearDown();
+ $this->mockIsLocked('foo', false, $this->startTime, 1);
+ $this->mockIsLocked('bar', false, $this->startTime, 1);
+ $this->mockIsLocked('nice', false, $this->startTime, 1);
+
+ $this->mockAcquireLock('foo', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+ $this->mockAcquireLock('bar', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+ $this->mockAcquireLock('nice', Cache::FIVE_MINUTES, false, $this->pid, false, $this->startTime, 1);
+
+ $this->mockReleaseLock('foo', $this->pid, 1);
+
+ $this->mockIsLocked('foo', false, $this->startTime, 1);
+ $this->mockIsLocked('bar', true, $this->startTime, 1);
+ $this->mockIsLocked('nice', true, $this->startTime, 1);
+
+ $this->mockReleaseAll($this->pid, 1);
+
+ $this->mockIsLocked('bar', false, $this->startTime, 1);
+ $this->mockIsLocked('nice', false, $this->startTime, 1);
+
+ parent::testReleaseAfterUnlock();
}
-}
\ No newline at end of file
+}