Skip to content

Commit 879b5cd

Browse files
committed
⭐ Employing default React loop (react/loop v1.2+)
1 parent 97e4f81 commit 879b5cd

2 files changed

Lines changed: 18 additions & 29 deletions

File tree

src/Custodian.php

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace RTCKit\React\Redlock;
66

77
use Clue\React\Redis\Client;
8-
use React\EventLoop\LoopInterface;
8+
use React\EventLoop\Loop;
99
use React\Promise\Deferred;
1010
use React\Promise\PromiseInterface;
1111
use function bin2hex;
@@ -23,21 +23,16 @@ final class Custodian
2323
end
2424
EOD;
2525

26-
/** @var LoopInterface Event loop we're bound to */
27-
private $loop;
28-
2926
/** @var Client ReactPHP Redis client */
3027
private $client;
3128

3229
/**
3330
* Custodian constructor
3431
*
35-
* @param LoopInterface $loop Event loop we're bound to
3632
* @param Client $client ReactPHP Redis client
3733
*/
38-
public function __construct(LoopInterface $loop, Client $client)
34+
public function __construct(Client $client)
3935
{
40-
$this->loop = $loop;
4136
$this->client = $client;
4237
}
4338

@@ -91,7 +86,7 @@ public function spin(int $attempts, float $interval, string $resource, float $tt
9186
if (!is_null($lock)) {
9287
$deferred->resolve($lock);
9388
} else {
94-
$this->loop->addTimer($interval, function () use ($deferred, $attempts, $interval, $resource, $ttl, $token) {
89+
Loop::addTimer($interval, function () use ($deferred, $attempts, $interval, $resource, $ttl, $token) {
9590
$deferred->resolve($this->spin(--$attempts, $interval, $resource, $ttl, $token));
9691
});
9792
}

tests/CustodianTest.php

Lines changed: 15 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
namespace RTCKit\React\Redlock;
66

77
use PHPUnit\Framework\TestCase;
8-
use React\EventLoop\Factory;
8+
use React\EventLoop\Loop;
99
use React\Promise\PromiseInterface;
1010
use function React\Promise\resolve;
1111

@@ -14,21 +14,19 @@
1414
*/
1515
class CustodianTest extends TestCase
1616
{
17-
private $loop;
1817
private $client;
1918

2019
/**
2120
* @before
2221
*/
2322
public function setUpFactory()
2423
{
25-
$this->loop = $this->getMockBuilder('React\EventLoop\LoopInterface')->getMock();
2624
$this->client = $this->getMockBuilder('Clue\React\Redis\Client')->getMock();
2725
}
2826

2927
public function testConstructor()
3028
{
31-
$custodian = new Custodian($this->loop, $this->client);
29+
$custodian = new Custodian($this->client);
3230

3331
$this->assertNotNull($custodian);
3432
$this->assertInstanceOf(Custodian::class, $custodian);
@@ -41,7 +39,7 @@ public function testSuccessfulAcquire()
4139
->with('set', ['resource', 'r4nd0m', 'NX', 'PX', 60000])
4240
->willReturn(resolve('OK'));
4341

44-
$custodian = new Custodian($this->loop, $this->client);
42+
$custodian = new Custodian($this->client);
4543

4644
$promise = $custodian->acquire('resource', 60, 'r4nd0m');
4745

@@ -64,7 +62,7 @@ public function testFailedAcquire()
6462
->with('set', ['2fail', 'r4nd0m', 'NX', 'PX', 60000])
6563
->willReturn(resolve(null));
6664

67-
$custodian = new Custodian($this->loop, $this->client);
65+
$custodian = new Custodian($this->client);
6866

6967
$promise = $custodian->acquire('2fail', 60, 'r4nd0m');
7068

@@ -82,7 +80,7 @@ public function testAcquireOptionalToken()
8280
->method('__call')
8381
->willReturn(resolve('OK'));
8482

85-
$custodian = new Custodian($this->loop, $this->client);
83+
$custodian = new Custodian($this->client);
8684

8785
$promise = $custodian->acquire('resource', 60);
8886

@@ -100,8 +98,6 @@ public function testAcquireOptionalToken()
10098

10199
public function testSuccessfulSpin()
102100
{
103-
$loop = Factory::create();
104-
105101
$this->client->expects($this->exactly(5))
106102
->method('__call')
107103
->with('set', ['resource', 'r4nd0m', 'NX', 'PX', 60000])
@@ -113,30 +109,28 @@ public function testSuccessfulSpin()
113109
resolve('OK')
114110
);
115111

116-
$custodian = new Custodian($loop, $this->client);
112+
$custodian = new Custodian($this->client);
117113

118114
$promise = $custodian->spin(5, 0.000001, 'resource', 60, 'r4nd0m');
119115

120116
$this->assertNotNull($promise);
121117
$this->assertInstanceOf(PromiseInterface::class, $promise);
122118

123-
$promise->then(function (?Lock $lock) use ($loop) {
119+
$promise->then(function (?Lock $lock) {
124120
$this->assertNotNull($lock);
125121
$this->assertInstanceOf(Lock::class, $lock);
126122
$this->assertEquals('resource', $lock->getResource());
127123
$this->assertEquals(60, $lock->getTTL());
128124
$this->assertEquals('r4nd0m', $lock->getToken());
129125

130-
$loop->stop();
126+
Loop::stop();
131127
});
132128

133-
$loop->run();
129+
Loop::run();
134130
}
135131

136132
public function testFailedSpin()
137133
{
138-
$loop = Factory::create();
139-
140134
$this->client->expects($this->exactly(5))
141135
->method('__call')
142136
->with('set', ['resource', 'r4nd0m', 'NX', 'PX', 60000])
@@ -148,20 +142,20 @@ public function testFailedSpin()
148142
resolve(null)
149143
);
150144

151-
$custodian = new Custodian($loop, $this->client);
145+
$custodian = new Custodian($this->client);
152146

153147
$promise = $custodian->spin(5, 0.000001, 'resource', 60, 'r4nd0m');
154148

155149
$this->assertNotNull($promise);
156150
$this->assertInstanceOf(PromiseInterface::class, $promise);
157151

158-
$promise->then(function (?Lock $lock) use ($loop) {
152+
$promise->then(function (?Lock $lock) {
159153
$this->assertNull($lock);
160154

161-
$loop->stop();
155+
Loop::stop();
162156
});
163157

164-
$loop->run();
158+
Loop::run();
165159
}
166160

167161
public function testSuccessfulRelease()
@@ -172,7 +166,7 @@ public function testSuccessfulRelease()
172166
->willReturn(resolve('1'));
173167

174168
$lock = new Lock('release', 60, 'r4nd0m');
175-
$custodian = new Custodian($this->loop, $this->client);
169+
$custodian = new Custodian($this->client);
176170

177171
$promise = $custodian->release($lock);
178172

@@ -192,7 +186,7 @@ public function testFailedRelease()
192186
->willReturn(resolve('0'));
193187

194188
$lock = new Lock('release', 60, 'r4nd0m');
195-
$custodian = new Custodian($this->loop, $this->client);
189+
$custodian = new Custodian($this->client);
196190

197191
$promise = $custodian->release($lock);
198192

0 commit comments

Comments
 (0)