diff options
author | LEDfan <tobia@ledfan.be> | 2016-01-20 13:08:41 +0300 |
---|---|---|
committer | LEDfan <tobia@ledfan.be> | 2016-01-20 13:08:41 +0300 |
commit | 51ee5602651cfe1ee7c5b5458ef9d1648b82b594 (patch) | |
tree | 141ca6bcb5d5a98c1e9323be4f1d999bc6808f1e | |
parent | 352b3f82ce5b098b671a6281bb0e95096573ec8b (diff) |
Add unit tests for MemLock
-rw-r--r-- | .gitignore | 6 | ||||
-rw-r--r-- | appinfo/application.php | 9 | ||||
-rw-r--r-- | lib/memlock.php | 13 | ||||
-rw-r--r-- | tests/integration/DbLockTest.php | 12 | ||||
-rw-r--r-- | tests/integration/MemLockTest.php | 71 |
5 files changed, 57 insertions, 54 deletions
@@ -12,4 +12,8 @@ css/ composer.phar vendor/ .phpstorm_helpers -coverage
\ No newline at end of file +coverage + +# vim +.phpstorm_helpers +.phpcomplete_extended
\ No newline at end of file diff --git a/appinfo/application.php b/appinfo/application.php index 1bd4e4f..adb7344 100644 --- a/appinfo/application.php +++ b/appinfo/application.php @@ -11,6 +11,7 @@ use OCP\AppFramework\App; use OCA\OJSXC\ILock; use OCA\OJSXC\DbLock; use OCA\OJSXC\MemLock; +use OCP\ICache; class Application extends App { @@ -55,9 +56,15 @@ class Application extends App { }); $container->registerService('MemLock', function($c){ + $cache = $c->getServer()->getMemCacheFactory(); + if ($cache->isAvailable()) { + $memcache = $cache->create('ojsxc'); + } else { + die('No memcache available'); // TODO + } return new MemLock( $c->query('UserId'), - $c->getServer()->getMemCacheFactory() + $memcache ); }); diff --git a/lib/memlock.php b/lib/memlock.php index 42b2852..55f3b13 100644 --- a/lib/memlock.php +++ b/lib/memlock.php @@ -2,10 +2,7 @@ namespace OCA\OJSXC; -use OCP\Files\Cache\ICache; -use OCP\ICacheFactory; -use OCP\IMemcache; -use OCP\IMemcacheTTL; +use OCP\ICache; class MemLock implements ILock { @@ -16,13 +13,9 @@ class MemLock implements ILock { private $userId; - public function __construct($userId, ICacheFactory $cache) { + public function __construct($userId, ICache $cache) { $this->userId = $userId; - if ($cache->isAvailable()) { - $this->memcache = $cache->create('ojsxc'); - } else { - die('No memcache available'); // TODO - } + $this->memcache = $cache; $this->pollingId = time(); } diff --git a/tests/integration/DbLockTest.php b/tests/integration/DbLockTest.php index 61ce3ca..e294f43 100644 --- a/tests/integration/DbLockTest.php +++ b/tests/integration/DbLockTest.php @@ -6,8 +6,11 @@ use Test\TestCase; use OCA\OJSXC\AppInfo\Application; use OCA\OJSXC\DbLock; +$time = 0; + function time() { - return DbLockTest::$time; + global $time; + return $time; } /** @@ -30,8 +33,6 @@ class DbLockTest extends TestCase { */ private $con; - public static $time; - public function setUp() { parent::setUp(); $app = new Application(); @@ -45,7 +46,8 @@ class DbLockTest extends TestCase { * and then setting a new lock. */ public function testLock() { - self::$time = 4; + global $time; + $time = 4; $this->dbLock = new DbLock( 'john', $this->container->getServer()->getDb(), @@ -61,7 +63,7 @@ class DbLockTest extends TestCase { $this->assertTrue($this->dbLock->stillLocked()); - self::$time = 5; + $time = 5; $this->dbLock2 = new DbLock( 'john', $this->container->getServer()->getDb(), diff --git a/tests/integration/MemLockTest.php b/tests/integration/MemLockTest.php index 465c82b..2a97e1b 100644 --- a/tests/integration/MemLockTest.php +++ b/tests/integration/MemLockTest.php @@ -30,6 +30,11 @@ class MemLockTest extends TestCase { */ private $container; + /** + * @var \OCP\ICache + */ + private $memCache; + public static $time; public function setUp() { @@ -43,52 +48,44 @@ class MemLockTest extends TestCase { * and then setting a new lock. */ public function testLock() { - self::$time = 4; + global $time; + $time = 4; + $cache = $this->container->getServer()->getMemCacheFactory(); + if ($cache->isAvailable()) { + $this->memCache = $cache->create('ojsxc'); + } else { + die('No memcache available'); // TODO + } + $this->memLock = new MemLock( 'john', - $this->container->getServer()->getMemCacheFactory() + $this->memCache ); $this->memLock->setLock(); + $this->assertTrue($this->memLock->stillLocked()); -// $result = $this->fetchLocks(); -// $this->assertCount(1, $result); -// $this->assertEquals($result[0]['userid'], 'john'); -// $this->assertEquals($result[0]['appid'], 'ojsxc'); -// $this->assertEquals($result[0]['configkey'], 'longpolling'); -// $this->assertEquals($result[0]['configvalue'], '4'); - $this->assertTrue($this->memLock->stillLocked()); -// -// -// self::$time = 5; -// $this->dbLock2 = new DbLock( -// 'john', -// $this->container->getServer()->getDb(), -// $this->container->getServer()->getConfig() -// ); // simulate new lock/request -// $this->dbLock2->setLock(); -// -// $this->assertFalse($this->dbLock->stillLocked()); -// $this->assertTrue($this->dbLock2->stillLocked()); -// $result = $this->fetchLocks(); -// $this->assertCount(1, $result); -// $this->assertEquals($result[0]['userid'], 'john'); -// $this->assertEquals($result[0]['appid'], 'ojsxc'); -// $this->assertEquals($result[0]['configkey'], 'longpolling'); -// $this->assertEquals($result[0]['configvalue'], '5'); -// $this->assertTrue($this->dbLock2->stillLocked()); + $result = $this->fetchLock(); + $this->assertEquals('4', $result); + + + global $time; + $time = 5; + $this->memLock2 = new MemLock( + 'john', + $this->memCache + ); // simulate new lock/request + $this->memLock2->setLock(); + + $this->assertFalse($this->memLock->stillLocked()); + $this->assertTrue($this->memLock2->stillLocked()); + $result = $this->fetchLock(); + $this->assertEquals('5', $result); } - private function fetchLocks() { -// $stmt = $this->con->executeQuery("SELECT * FROM `*PREFIX*preferences` WHERE `appid`='ojsxc' AND `configkey`='longpolling'"); -// -// $reuslt = []; -// -// while($row = $stmt->fetch()){ -// $result[] = $row; -// } -// return $result; + private function fetchLock() { + return $this->memCache->get('-john-ojxsc-lock'); } }
\ No newline at end of file |