Skip to content

Commit 167d3f5

Browse files
authored
test: reduce flakiness of time sensitive tests (#1460)
1 parent 48de253 commit 167d3f5

File tree

6 files changed

+36
-12
lines changed

6 files changed

+36
-12
lines changed

packages/cryptography/tests/Encryption/EncryptionTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,13 @@
1313
use Tempest\Cryptography\Signing\SigningAlgorithm;
1414
use Tempest\Cryptography\Signing\SigningConfig;
1515
use Tempest\Cryptography\Tests\CreatesSigner;
16+
use Tempest\Cryptography\Tests\HasMoreIntegerAssertions;
1617
use Tempest\DateTime\Duration;
1718

1819
final class EncryptionTest extends TestCase
1920
{
2021
use CreatesSigner;
22+
use HasMoreIntegerAssertions;
2123

2224
private function createEncrypter(?string $key = null, false|Duration $minimumExecutionDuration = false): GenericEncrypter
2325
{
@@ -60,8 +62,7 @@ public function test_time_protection(): void
6062
$this->assertSame('important data', $encrypter->decrypt($encrypted));
6163
$elapsed = microtime(true) - $start;
6264

63-
$this->assertGreaterThanOrEqual(0.29, $elapsed);
64-
$this->assertLessThanOrEqual(0.311, $elapsed);
65+
$this->assertEqualsToMoreOrLess(0.3, $elapsed, margin: 0.015);
6566
}
6667

6768
public function test_wrong_key(): void
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace Tempest\Cryptography\Tests;
4+
5+
use PHPUnit\Framework\ExpectationFailedException;
6+
7+
trait HasMoreIntegerAssertions
8+
{
9+
private function assertEqualsToMoreOrLess(int|float $expected, int|float $actual, int|float $margin): void
10+
{
11+
try {
12+
$this->assertGreaterThanOrEqual($expected - $margin, $actual);
13+
$this->assertLessThanOrEqual($expected + $margin, $actual);
14+
} catch (ExpectationFailedException $e) {
15+
throw new ExpectationFailedException(
16+
message: sprintf('Expected value to be within %s of %s, but got %s', $margin, $expected, $actual),
17+
comparisonFailure: $e->getComparisonFailure(),
18+
);
19+
}
20+
}
21+
}

packages/cryptography/tests/Signing/SignerTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,20 @@
22

33
namespace Tempest\Cryptography\Tests\Signing;
44

5+
use PHPUnit\Framework\ExpectationFailedException;
56
use PHPUnit\Framework\TestCase;
67
use Tempest\Clock\MockClock;
78
use Tempest\Cryptography\Signing\Exceptions\SigningKeyWasInvalid;
89
use Tempest\Cryptography\Signing\SigningAlgorithm;
910
use Tempest\Cryptography\Signing\SigningConfig;
1011
use Tempest\Cryptography\Tests\CreatesSigner;
12+
use Tempest\Cryptography\Tests\HasMoreIntegerAssertions;
1113
use Tempest\DateTime\Duration;
1214

1315
final class SignerTest extends TestCase
1416
{
1517
use CreatesSigner;
18+
use HasMoreIntegerAssertions;
1619

1720
public function test_good_signature(): void
1821
{
@@ -163,8 +166,7 @@ public function test_time_protection(): void
163166
$this->assertTrue($signer->verify($data, $signature));
164167
$elapsed = microtime(true) - $start;
165168

166-
$this->assertGreaterThanOrEqual(0.29, $elapsed);
167-
$this->assertLessThanOrEqual(0.311, $elapsed);
169+
$this->assertEqualsToMoreOrLess(0.3, $elapsed, margin: 0.015);
168170
}
169171

170172
public function test_time_protection_with_mock_clock(): void
@@ -182,7 +184,6 @@ public function test_time_protection_with_mock_clock(): void
182184
$this->assertTrue($signer->verify($data, $signature));
183185
$elapsed = $clock->timestamp()->getMilliseconds() - $ms;
184186

185-
$this->assertLessThanOrEqual(1_001, $elapsed);
186-
$this->assertGreaterThanOrEqual(999, $elapsed);
187+
$this->assertEqualsToMoreOrLess(1000, $elapsed, margin: 10);
187188
}
188189
}

packages/cryptography/tests/TimelockTest.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
final class TimelockTest extends TestCase
1313
{
14+
use HasMoreIntegerAssertions;
15+
1416
public function test_callback_is_executed(): void
1517
{
1618
$clock = new GenericClock();
@@ -29,8 +31,7 @@ public function test_locks_for_duration(): void
2931

3032
$elapsed = microtime(true) - $start;
3133

32-
$this->assertGreaterThanOrEqual(0.1, $elapsed, 'The timelock did not wait for the specified duration.');
33-
$this->assertLessThan(0.2, $elapsed, 'The timelock waited for too long.');
34+
$this->assertEqualsToMoreOrLess(0.1, $elapsed, margin: 0.015);
3435
}
3536

3637
public function test_return_early(): void
@@ -62,7 +63,7 @@ public function test_throws_exception_after_delay(): void
6263
);
6364
} catch (\RuntimeException) {
6465
$elapsed = microtime(true) - $start;
65-
$this->assertGreaterThanOrEqual(0.1, $elapsed, 'The exception was thrown before the timelock duration elapsed.');
66+
$this->assertEqualsToMoreOrLess(0.1, $elapsed, margin: 0.015);
6667
}
6768
}
6869

packages/datetime/tests/TimestampTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -571,8 +571,8 @@ public function test_nano_precision_temporal_comparisons(): void
571571
public function test_future_past_comprehensive(): void
572572
{
573573
$now = Timestamp::monotonic();
574-
$future = $now->plusMilliseconds(1);
575-
$past = $now->minusMilliseconds(1);
574+
$future = $now->plusMilliseconds(15);
575+
$past = $now->minusMilliseconds(15);
576576

577577
$this->assertTrue($future->isFuture());
578578
$this->assertFalse($future->isPast());

tests/Integration/Console/Components/TaskComponentTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public function test_process_task(): void
6262
$frames = iterator_to_array($component->render($terminal));
6363

6464
$this->assertStringContainsString('Task in progress', $frames[0]);
65-
$this->assertStringContainsString('Done in', $frames[1]);
65+
$this->assertStringContainsString('Done in', $frames[array_key_last($frames)]);
6666
});
6767
}
6868

0 commit comments

Comments
 (0)