Skip to content

Commit c5b187c

Browse files
committed
tests for withTtl
1 parent d97f929 commit c5b187c

File tree

2 files changed

+46
-1
lines changed

2 files changed

+46
-1
lines changed

src/RoadRunnerStore.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,12 @@ public function __construct(
3131
\assert($this->initialWaitTtl >= 0);
3232
}
3333

34-
public function withTtl(float $ttl, float $waitTtl = 0): self
34+
/**
35+
* Clone current instance with another values of ttl.
36+
*/
37+
public function withTtl(float $ttl, ?float $waitTtl = null): self
3538
{
39+
$waitTtl ??= $this->initialWaitTtl;
3640
return new self($this->lock, $this->tokens, $ttl, $waitTtl);
3741
}
3842

tests/RoadRunnerStoreTest.php

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,4 +190,45 @@ public function testWaitAndSaveFail(): void
190190
$store = new RoadRunnerStore($this->rrLock, $this->tokens);
191191
$store->waitAndSave(new Key('resource-name'));
192192
}
193+
194+
/**
195+
* @dataProvider dataWithTtl
196+
*/
197+
public function testWithTtl(float $ttl, ?float $waitTtl, float $ttlExp, float $waitTtlExp): void
198+
{
199+
$this->rrLock->expects($this->once())
200+
->method('lock')
201+
->with('resource-name', 'random-id', $ttlExp, $waitTtlExp)
202+
->willReturn('lock-id');
203+
204+
$s = new RoadRunnerStore($this->rrLock, $this->tokens);
205+
$s->withTtl($ttl, $waitTtl)->save(new Key('resource-name'));
206+
}
207+
208+
/**
209+
* @return iterable
210+
*/
211+
public static function dataWithTtl(): iterable
212+
{
213+
yield [
214+
100,
215+
null,
216+
100,
217+
0,
218+
];
219+
220+
yield [
221+
0.1,
222+
0.1,
223+
0.1,
224+
0.1,
225+
];
226+
227+
yield [
228+
0,
229+
0,
230+
0,
231+
0,
232+
];
233+
}
193234
}

0 commit comments

Comments
 (0)