Skip to content

Commit d13582e

Browse files
committed
improve tests
1 parent 3a0203b commit d13582e

File tree

5 files changed

+44
-30
lines changed

5 files changed

+44
-30
lines changed

tests/mutex/MutexConcurrencyTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,18 @@ function ($timeout = 3) use ($dsn, $user, $password) {
197197
*/
198198
public function testExecutionIsSerializedWhenLocked(callable $mutexFactory)
199199
{
200-
$timestamp = hrtime(true);
200+
$time = \microtime(true);
201201

202-
$this->fork(5, function () use ($mutexFactory): void {
202+
$this->fork(6, function () use ($mutexFactory): void {
203203
/** @var Mutex $mutex */
204204
$mutex = $mutexFactory();
205205
$mutex->synchronized(function (): void {
206-
\usleep(200000);
206+
\usleep(200 * 1000);
207207
});
208208
});
209209

210-
$delta = \hrtime(true) - $timestamp;
211-
$this->assertGreaterThan(1e9, $delta);
210+
$delta = \microtime(true) - $time;
211+
$this->assertGreaterThan(1201 * 1000, $delta);
212212
}
213213

214214
/**

tests/mutex/PredisMutexTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ protected function setUp(): void
4040
->setMethods(array_merge(get_class_methods(ClientInterface::class), ['set', 'eval']))
4141
->getMock();
4242

43-
$this->mutex = new PredisMutex([$this->client], 'test');
43+
$this->mutex = new PredisMutex([$this->client], 'test', 2.5);
4444

4545
$this->logger = $this->createMock(LoggerInterface::class);
4646
$this->mutex->setLogger($this->logger);
@@ -53,7 +53,7 @@ public function testAddFailsToSetKey()
5353
{
5454
$this->client->expects($this->atLeastOnce())
5555
->method('set')
56-
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
56+
->with('lock_test', $this->isType('string'), 'PX', 3500, 'NX')
5757
->willReturn(null);
5858

5959
$this->logger->expects($this->never())
@@ -75,7 +75,7 @@ public function testAddErrors()
7575
{
7676
$this->client->expects($this->atLeastOnce())
7777
->method('set')
78-
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
78+
->with('lock_test', $this->isType('string'), 'PX', 3500, 'NX')
7979
->willThrowException($this->createMock(PredisException::class));
8080

8181
$this->logger->expects($this->once())
@@ -95,7 +95,7 @@ public function testWorksNormally()
9595
{
9696
$this->client->expects($this->atLeastOnce())
9797
->method('set')
98-
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
98+
->with('lock_test', $this->isType('string'), 'PX', 3500, 'NX')
9999
->willReturnSelf();
100100

101101
$this->client->expects($this->once())
@@ -119,7 +119,7 @@ public function testEvalScriptFails()
119119
{
120120
$this->client->expects($this->atLeastOnce())
121121
->method('set')
122-
->with('lock_test', $this->isType('string'), 'PX', 4000, 'NX')
122+
->with('lock_test', $this->isType('string'), 'PX', 3500, 'NX')
123123
->willReturnSelf();
124124

125125
$this->client->expects($this->once())

tests/mutex/RedisMutexTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ public function provideTestTimingOut()
196196
{
197197
// count, timeout, delay
198198
return [
199-
[1, 1, 2.001],
200-
[2, 1, 1.001],
199+
[1, 1.2 - 1, 1.201],
200+
[2, 1.2 - 1, 1.401],
201201
];
202202
}
203203

tests/mutex/SpinlockMutexTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ public function testAcquireTimesOut()
7272
public function testExecuteTooLong()
7373
{
7474
/** @var SpinlockMutex|\PHPUnit\Framework\MockObject\MockObject $mutex */
75-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 1]);
75+
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.5]);
7676
$mutex->expects($this->any())
7777
->method('acquire')
7878
->willReturn(true);
@@ -82,13 +82,13 @@ public function testExecuteTooLong()
8282
->willReturn(true);
8383

8484
$this->expectException(ExecutionOutsideLockException::class);
85-
// $this->expectExceptionMessageRegExp(
86-
// '/The code executed for \d+\.\d+ seconds. But the timeout is 1 ' .
87-
// 'seconds. The last \d+\.\d+ seconds were executed outside of the lock./'
88-
// );
85+
$this->expectExceptionMessageMatches(
86+
'/The code executed for 0\.5\d+ seconds. But the timeout is 0\.5 ' .
87+
'seconds. The last 0\.0\d+ seconds were executed outside of the lock./'
88+
);
8989

9090
$mutex->synchronized(function () {
91-
usleep(1100 * 1000);
91+
usleep(501 * 1000);
9292
});
9393
}
9494

@@ -98,12 +98,12 @@ public function testExecuteTooLong()
9898
*/
9999
public function testExecuteBarelySucceeds()
100100
{
101-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 1]);
101+
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.5]);
102102
$mutex->expects($this->any())->method('acquire')->willReturn(true);
103103
$mutex->expects($this->once())->method('release')->willReturn(true);
104104

105105
$mutex->synchronized(function () {
106-
usleep(999999);
106+
usleep(499 * 1000);
107107
});
108108
}
109109

@@ -127,16 +127,16 @@ public function testFailReleasingLock()
127127
*/
128128
public function testExecuteTimeoutLeavesOneSecondForKeyToExpire()
129129
{
130-
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 3]);
130+
$mutex = $this->getMockForAbstractClass(SpinlockMutex::class, ['test', 0.2]);
131131
$mutex->expects($this->once())
132132
->method('acquire')
133-
->with($this->anything(), 4)
133+
->with($this->anything(), 1.2)
134134
->willReturn(true);
135135

136136
$mutex->expects($this->once())->method('release')->willReturn(true);
137137

138138
$mutex->synchronized(function () {
139-
usleep(2999999);
139+
usleep(199 * 1000);
140140
});
141141
}
142142
}

tests/util/LoopTest.php

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,23 +43,37 @@ public function testExecutionWithinTimeout()
4343
{
4444
$this->expectNotToPerformAssertions();
4545

46-
$loop = new Loop(1);
46+
$loop = new Loop(0.5);
4747
$loop->execute(function () use ($loop): void {
48-
usleep(999999);
48+
usleep(499 * 1000);
4949
$loop->end();
5050
});
5151
}
5252

53+
/**
54+
* Tests execution within the timeout without calling end().
55+
*/
56+
public function testExecutionWithinTimeoutWithoutExplicitEnd()
57+
{
58+
$this->expectException(TimeoutException::class);
59+
$this->expectExceptionMessage('Timeout of 0.5 seconds exceeded.');
60+
61+
$loop = new Loop(0.5);
62+
$loop->execute(function (): void {
63+
usleep(10 * 1000);
64+
});
65+
}
66+
5367
/**
5468
* Tests exceeding the execution timeout.
5569
*/
5670
public function testExceedTimeoutIsAcceptableIfEndWasCalled()
5771
{
5872
$this->expectNotToPerformAssertions();
5973

60-
$loop = new Loop(1);
74+
$loop = new Loop(0.5);
6175
$loop->execute(function () use ($loop): void {
62-
sleep(1);
76+
usleep(501 * 1000);
6377
$loop->end();
6478
});
6579
}
@@ -70,11 +84,11 @@ public function testExceedTimeoutIsAcceptableIfEndWasCalled()
7084
public function testExceedTimeoutWithoutExplicitEnd()
7185
{
7286
$this->expectException(TimeoutException::class);
73-
$this->expectExceptionMessage('Timeout of 1.0 seconds exceeded.');
87+
$this->expectExceptionMessage('Timeout of 0.5 seconds exceeded.');
7488

75-
$loop = new Loop(1);
89+
$loop = new Loop(0.5);
7690
$loop->execute(function (): void {
77-
sleep(1);
91+
usleep(501 * 1000);
7892
});
7993
}
8094

0 commit comments

Comments
 (0)