Merge pull request #7515 from nupplaphil/task/console_lock

New Console Command: Lock
This commit is contained in:
Hypolite Petovan 2019-08-15 10:31:34 -04:00 committed by GitHub
commit 48caf55cff
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 743 additions and 41 deletions

View file

@ -0,0 +1,215 @@
<?php
namespace Friendica\Test\src\Console;
use Friendica\App;
use Friendica\App\Mode;
use Friendica\Console\Lock;
use Friendica\Core\Lock\ILock;
use Mockery\MockInterface;
class LockConsoleTest extends ConsoleTest
{
/**
* @var App\Mode|MockInterface $appMode
*/
private $appMode;
/**
* @var ILock|MockInterface
*/
private $lockMock;
protected function setUp()
{
parent::setUp();
\Mockery::getConfiguration()->setConstantsMap([
Mode::class => [
'DBCONFIGAVAILABLE' => 0
]
]);
$this->appMode = \Mockery::mock(App\Mode::class);
$this->appMode->shouldReceive('has')
->andReturn(true);
$this->lockMock = \Mockery::mock(ILock::class);
}
public function testList()
{
$this->lockMock
->shouldReceive('getLocks')
->andReturn(['test', 'test2'])
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'list');
$txt = $this->dumpExecute($console);
$this->assertEquals("Listing all Locks:\ntest\ntest2\n2 locks found\n", $txt);
}
public function testListPrefix()
{
$this->lockMock
->shouldReceive('getLocks')
->with('test')
->andReturn(['test', 'test2'])
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'list');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("Listing all Locks starting with \"test\":\ntest\ntest2\n2 locks found\n", $txt);
}
public function testDelLock()
{
$this->lockMock
->shouldReceive('releaseLock')
->with('test', true)
->andReturn(true)
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'del');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("Lock 'test' released.\n", $txt);
}
public function testDelUnknownLock()
{
$this->lockMock
->shouldReceive('releaseLock')
->with('test', true)
->andReturn(false)
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'del');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("Couldn't release Lock 'test'\n", $txt);
}
public function testSetLock()
{
$this->lockMock
->shouldReceive('isLocked')
->with('test')
->andReturn(false)
->once();
$this->lockMock
->shouldReceive('acquireLock')
->with('test')
->andReturn(true)
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'set');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("Lock 'test' acquired.\n", $txt);
}
public function testSetLockIsLocked()
{
$this->lockMock
->shouldReceive('isLocked')
->with('test')
->andReturn(true)
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'set');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("[Error] 'test' is already set.\n", $txt);
}
public function testSetLockNotWorking()
{
$this->lockMock
->shouldReceive('isLocked')
->with('test')
->andReturn(false)
->once();
$this->lockMock
->shouldReceive('acquireLock')
->with('test')
->andReturn(false)
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'set');
$console->setArgument(1, 'test');
$txt = $this->dumpExecute($console);
$this->assertEquals("[Error] Unable to lock 'test'.\n", $txt);
}
public function testReleaseAll()
{
$this->lockMock
->shouldReceive('releaseAll')
->andReturn(true)
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'clear');
$txt = $this->dumpExecute($console);
$this->assertEquals("Locks successfully cleared.\n", $txt);
}
public function testReleaseAllFailed()
{
$this->lockMock
->shouldReceive('releaseAll')
->andReturn(false)
->once();
$console = new Lock($this->appMode, $this->lockMock, $this->consoleArgv);
$console->setArgument(0, 'clear');
$txt = $this->dumpExecute($console);
$this->assertEquals("[Error] Unable to clear the locks.\n", $txt);
}
public function testGetHelp()
{
// Usable to purposely fail if new commands are added without taking tests into account
$theHelp = <<<HELP
console lock - Manage node locks
Synopsis
bin/console lock list [<prefix>] [-h|--help|-?] [-v]
bin/console lock set <lock> [<timeout> [<ttl>]] [-h|--help|-?] [-v]
bin/console lock del <lock> [-h|--help|-?] [-v]
bin/console lock clear [-h|--help|-?] [-v]
Description
bin/console lock list [<prefix>]
List all locks, optionally filtered by a prefix
bin/console lock set <lock> [<timeout> [<ttl>]]
Sets manually a lock, optionally with the provided TTL (time to live) with a default of five minutes.
bin/console lock del <lock>
Deletes a lock.
bin/console lock clear
Clears all locks
Options
-h|--help|-? Show help information
-v Show more debug information.
HELP;
$console = new Lock($this->appMode, $this->lockMock, [$this->consoleArgv]);
$console->setOption('help', true);
$txt = $this->dumpExecute($console);
$this->assertEquals($txt, $theHelp);
}
}

View file

@ -2,7 +2,6 @@
namespace Friendica\Test\src\Core\Cache;
use Friendica\Core\Cache\MemcachedCache;
use Friendica\Test\MockedTest;
use Friendica\Util\PidFile;
@ -202,10 +201,6 @@ abstract class CacheTest extends MockedTest
*/
public function testGetAllKeys($value1, $value2, $value3)
{
if ($this->cache instanceof MemcachedCache) {
$this->markTestSkipped('Memcached doesn\'t support getAllKeys anymore');
}
$this->assertTrue($this->instance->set('value1', $value1));
$this->assertTrue($this->instance->set('value2', $value2));
$this->assertTrue($this->instance->set('test_value3', $value3));
@ -219,5 +214,7 @@ abstract class CacheTest extends MockedTest
$list = $this->instance->getAllKeys('test');
$this->assertContains('test_value3', $list);
$this->assertNotContains('value1', $list);
$this->assertNotContains('value2', $list);
}
}

View file

@ -23,12 +23,12 @@ abstract class LockTest extends MockedTest
parent::setUp();
$this->instance = $this->getInstance();
$this->instance->releaseAll();
$this->instance->releaseAll(true);
}
protected function tearDown()
{
$this->instance->releaseAll();
$this->instance->releaseAll(true);
parent::tearDown();
}
@ -123,6 +123,46 @@ abstract class LockTest extends MockedTest
$this->assertFalse($this->instance->isLocked('test'));
}
/**
* @small
*/
public function testGetLocks()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('bar', 1));
$this->assertTrue($this->instance->acquireLock('nice', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('bar'));
$this->assertTrue($this->instance->isLocked('nice'));
$locks = $this->instance->getLocks();
$this->assertContains('foo', $locks);
$this->assertContains('bar', $locks);
$this->assertContains('nice', $locks);
}
/**
* @small
*/
public function testGetLocksWithPrefix()
{
$this->assertTrue($this->instance->acquireLock('foo', 1));
$this->assertTrue($this->instance->acquireLock('test1', 1));
$this->assertTrue($this->instance->acquireLock('test2', 1));
$this->assertTrue($this->instance->isLocked('foo'));
$this->assertTrue($this->instance->isLocked('test1'));
$this->assertTrue($this->instance->isLocked('test2'));
$locks = $this->instance->getLocks('test');
$this->assertContains('test1', $locks);
$this->assertContains('test2', $locks);
$this->assertNotContains('foo', $locks);
}
/**
* @medium
*/

View file

@ -12,8 +12,6 @@ class SemaphoreLockTest extends LockTest
{
public function setUp()
{
parent::setUp();
$dice = \Mockery::mock(Dice::class)->makePartial();
$app = \Mockery::mock(App::class);
@ -29,6 +27,8 @@ class SemaphoreLockTest extends LockTest
// @todo Because "get_temppath()" is using static methods, we have to initialize the BaseObject
BaseObject::setDependencyInjection($dice);
parent::setUp();
}
protected function getInstance()