From 51ee5602651cfe1ee7c5b5458ef9d1648b82b594 Mon Sep 17 00:00:00 2001 From: LEDfan Date: Wed, 20 Jan 2016 11:08:41 +0100 Subject: Add unit tests for MemLock --- tests/integration/DbLockTest.php | 12 ++++--- tests/integration/MemLockTest.php | 71 +++++++++++++++++++-------------------- 2 files changed, 41 insertions(+), 42 deletions(-) (limited to 'tests/integration') 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 -- cgit v1.2.3