Skip to content

Commit

Permalink
updated test
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Jan 3, 2022
1 parent 43e8df4 commit 8bbbeda
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions tests/SafeStream/SafeStream.stress.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ use Tester\Assert;
require __DIR__ . '/../bootstrap.php';


function randomStr()
function randomStr(): string
{
$s = str_repeat('LaTrine', rand(100, 20000));
return md5($s, true) . $s;
}


function checkStr($s)
function checkStr(string $s): bool
{
return substr($s, 0, 16) === md5(substr($s, 16), true);
}
Expand All @@ -32,42 +32,38 @@ set_time_limit(0);

// clear playground
for ($i = 0; $i <= COUNT_FILES; $i++) {
file_put_contents('nette.safe://' . TEMP_DIR . '/testfile' . $i, randomStr());
@unlink(TEMP_DIR . '/testfile' . $i);
}

// test loop
$hits = ['ok' => 0, 'notfound' => 0, 'error' => 0, 'cantwrite' => 0, 'cantdelete' => 0];
$hits = ['ok' => 0, 'notfound' => 0, 'notsame' => 0, 'empty' => 0, 'cantwrite' => 0];

for ($counter = 0; $counter < 300; $counter++) {
for ($counter = 0; $counter < 3000; $counter++) {
// write
$ok = @file_put_contents('nette.safe://' . TEMP_DIR . '/testfile' . rand(0, COUNT_FILES), randomStr());
if ($ok === false) {
$hits['cantwrite']++;
}

// delete
/*$ok = @unlink('nette.safe://' . TEMP_DIR . '/testfile' . rand(0, COUNT_FILES));
if (!$ok) {
$hits['cantdelete']++;
}*/
@unlink('nette.safe://' . TEMP_DIR . '/testfile' . rand(0, COUNT_FILES));

// read
$res = @file_get_contents('nette.safe://' . TEMP_DIR . '/testfile' . rand(0, COUNT_FILES));

// compare
if ($res === false) {
$hits['notfound']++;
} elseif ($res === '') {
$hits['empty']++;
} elseif (checkStr($res)) {
$hits['ok']++;
} else {
$hits['error']++;
$hits['notsame']++;
}
}

Assert::same([
'ok' => $counter, // should be 1000. If unlink() is used, sum [ok] + [notfound] should be 1000
'notfound' => 0, // means 'file not found', should be 0 if unlink() is not used
'error' => 0, // means 'file contents is damaged', MUST be 0
'cantwrite' => 0, // means 'somebody else is writing this file'
'cantdelete' => 0, // means 'unlink() has timeout', should be 0
], $hits);
var_export($hits);
Assert::same($counter, $hits['ok'] + $hits['notfound']);
Assert::same(0, $hits['notsame'], 'file contents is damaged');
Assert::same(0, $hits['empty'], 'file hasn\'t been written yet');

0 comments on commit 8bbbeda

Please sign in to comment.