Skip to content

Commit

Permalink
Merge 4eda66e into 54dcef2
Browse files Browse the repository at this point in the history
  • Loading branch information
vajexal committed Oct 28, 2020
2 parents 54dcef2 + 4eda66e commit e0d16ac
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
7 changes: 6 additions & 1 deletion src/StatCache.php
Expand Up @@ -17,7 +17,7 @@ private static function init(): void

$watcher = Loop::repeat(1000, function () {
self::$now = $now = \time();
foreach (self::$cache as $path => $expiry) {
foreach (self::$timeouts as $path => $expiry) {
if ($now > $expiry) {
unset(
self::$cache[$path],
Expand Down Expand Up @@ -67,6 +67,11 @@ public static function set(string $path, array $stat): void
self::$timeouts[$path] = self::$now + self::$ttl;
}

public static function getTtl(): int
{
return self::$ttl;
}

public static function ttl(int $seconds): void
{
self::$ttl = $seconds;
Expand Down
46 changes: 46 additions & 0 deletions test/StatCacheTest.php
@@ -0,0 +1,46 @@
<?php

namespace Amp\File\Test;

use Amp\Delayed;
use Amp\File;
use Amp\File\StatCache;

class StatCacheTest extends FilesystemTest
{
private $ttlBackup;

protected function setUp(): void
{
parent::setUp();

$this->ttlBackup = StatCache::getTtl();

StatCache::ttl(1);
}

protected function tearDown(): void
{
parent::tearDown();

StatCache::ttl($this->ttlBackup);
}

public function testStatCacheExpiration()
{
File\filesystem(new File\ParallelDriver);

$fixtureDir = Fixture::path();
$path = "{$fixtureDir}/small.txt";

$changeTime = yield File\mtime($path);

yield new Delayed(1000);

yield File\put($path, 'smaller');

yield new Delayed(1000);

$this->assertNotEquals($changeTime, yield File\mtime($path));
}
}

0 comments on commit e0d16ac

Please sign in to comment.