Skip to content

Commit f121fef

Browse files
Update memcached test
1 parent 3f89db3 commit f121fef

File tree

1 file changed

+17
-32
lines changed

1 file changed

+17
-32
lines changed

tests/mutex/MemcachedMutexTest.php

Lines changed: 17 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -10,48 +10,34 @@
1010
* @author Markus Malkusch <[email protected]>
1111
* @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
1212
* @license WTFPL
13+
* @requires extension memcached
1314
* @see MemcachedMutex
1415
*/
1516
class MemcachedMutexTest extends \PHPUnit_Framework_TestCase
1617
{
17-
18-
protected function setUp()
19-
{
20-
if (!getenv("MEMCACHE_HOST")) {
21-
$this->markTestSkipped();
22-
return;
23-
}
24-
}
25-
2618
/**
27-
* Builds a MemcachedMutex.
28-
*
29-
* @param string $name The mutex name.
30-
* @param \Memcached &$memcache The memcached API.
31-
* @param int $timeout The timeout.
32-
*
33-
* @return MemcachedMutex The Mutex
19+
* @var \Memcached
3420
*/
35-
private static function buildMemcachedMutex($name, &$memcache, $timeout = 3)
21+
protected $memcached;
22+
23+
protected function setUp()
3624
{
37-
if (!getenv("MEMCACHE_HOST")) {
38-
return;
39-
}
40-
$memcache = new \Memcached();
41-
$memcache->addServer(getenv("MEMCACHE_HOST"), 11211);
42-
return new MemcachedMutex($name, $memcache, $timeout);
25+
$this->memcached = new \Memcached();
26+
$this->memcached->addServer(getenv("MEMCACHE_HOST") ?: "localhost", 11211);
27+
$this->memcached->flush();
4328
}
44-
29+
4530
/**
4631
* Tests failing to acquire the lock.
4732
*
4833
* @test
49-
* @expectedException malkusch\lock\exception\TimeoutException
34+
* @expectedException \malkusch\lock\exception\TimeoutException
5035
*/
5136
public function testFailAcquireLock()
5237
{
53-
$mutex = self::buildMemcachedMutex("testFailAcquireLock", $memcache, 1);
54-
$memcache->add(MemcachedMutex::PREFIX."testFailAcquireLock", true, 2);
38+
$mutex = new MemcachedMutex("testFailAcquireLock", $this->memcached, 1);
39+
40+
$this->memcached->add(MemcachedMutex::PREFIX."testFailAcquireLock", true, 2);
5541

5642
$mutex->synchronized(function () {
5743
$this->fail("execution is not expected");
@@ -62,14 +48,13 @@ public function testFailAcquireLock()
6248
* Tests failing to release a lock.
6349
*
6450
* @test
65-
* @expectedException malkusch\lock\exception\LockReleaseException
51+
* @expectedException \malkusch\lock\exception\LockReleaseException
6652
*/
6753
public function testFailReleasingLock()
6854
{
69-
$mutex = self::buildMemcachedMutex("testFailReleasingLock", $memcache);
70-
71-
$mutex->synchronized(function () use ($memcache) {
72-
$memcache->delete(MemcachedMutex::PREFIX."testFailReleasingLock");
55+
$mutex = new MemcachedMutex("testFailReleasingLock", $this->memcached, 1);
56+
$mutex->synchronized(function () {
57+
$this->memcached->delete(MemcachedMutex::PREFIX."testFailReleasingLock");
7358
});
7459
}
7560
}

0 commit comments

Comments
 (0)