Browse Source

Fetch in three hours if caching is via etag

microsub
J. King 5 years ago
parent
commit
eb120b4629
  1. 9
      lib/Feed.php
  2. 65
      tests/cases/Feed/TestFeed.php
  3. 6
      tests/docroot/Feed/NextFetch/NotModifiedEtag.php

9
lib/Feed.php

@ -331,8 +331,13 @@ class Feed {
protected function computeNextFetch(): \DateTimeImmutable { protected function computeNextFetch(): \DateTimeImmutable {
$now = Date::normalize(time()); $now = Date::normalize(time());
if (!$this->modified) { if (!$this->modified) {
$diff = $now->getTimestamp() - $this->lastModified->getTimestamp(); if ($this->lastModified) {
$offset = $this->normalizeDateDiff($diff); $diff = $now->getTimestamp() - $this->lastModified->getTimestamp();
$offset = $this->normalizeDateDiff($diff);
} else {
// if no timestamp is available, fall back to three hours
$offset = "3 hours";
}
return $now->modify("+".$offset); return $now->modify("+".$offset);
} else { } else {
// the algorithm for updated feeds (returning 200 rather than 304) uses the same parameters as for 304, // the algorithm for updated feeds (returning 200 rather than 304) uses the same parameters as for 304,

65
tests/cases/Feed/TestFeed.php

@ -258,50 +258,31 @@ class TestFeed extends \JKingWeb\Arsse\Test\AbstractTest {
} }
} }
public function testComputeNextFetchFrom304() { /** @dataProvider provide304Timestamps */
// if less than half an hour, check in 15 minutes public function testComputeNextFetchFrom304(string $t, string $exp) {
$t = strtotime("now"); $t = $t ? strtotime($t) : "";
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 15 minutes");
$this->assertTime($exp, $f->nextFetch);
$t = strtotime("now - 29 minutes");
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 15 minutes");
$this->assertTime($exp, $f->nextFetch);
// if less than an hour, check in 30 minutes
$t = strtotime("now - 30 minutes");
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 30 minutes");
$this->assertTime($exp, $f->nextFetch);
$t = strtotime("now - 59 minutes");
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 30 minutes");
$this->assertTime($exp, $f->nextFetch);
// if less than three hours, check in an hour
$t = strtotime("now - 1 hour");
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 1 hour");
$this->assertTime($exp, $f->nextFetch);
$t = strtotime("now - 2 hours 59 minutes");
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 1 hour");
$this->assertTime($exp, $f->nextFetch);
// if more than 36 hours, check in 24 hours
$t = strtotime("now - 36 hours");
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http")); $f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 1 day"); $exp = strtotime($exp);
$this->assertTime($exp, $f->nextFetch); $this->assertTime($exp, $f->nextFetch);
$t = strtotime("now - 2 years"); }
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http"));
$exp = strtotime("now + 1 day"); public function provide304Timestamps() {
$this->assertTime($exp, $f->nextFetch); return [
// otherwise check in three hours 'less than half an hour 1' => ["now", "now + 15 minutes"],
$t = strtotime("now - 3 hours"); 'less than half an hour 2' => ["now - 29 minutes", "now + 15 minutes"],
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http")); 'less than one hour 1' => ["now - 30 minutes", "now + 30 minutes"],
$exp = strtotime("now + 3 hours"); 'less than one hour 2' => ["now - 59 minutes", "now + 30 minutes"],
$this->assertTime($exp, $f->nextFetch); 'less than three hours 1' => ["now - 1 hour", "now + 1 hour"],
$t = strtotime("now - 35 hours"); 'less than three hours 2' => ["now - 2 hours 59 minutes", "now + 1 hour"],
$f = new Feed(null, $this->base."NextFetch/NotModified?t=$t", Date::transform($t, "http")); 'more than thirty-six hours 1' => ["now - 36 hours", "now + 1 day"],
'more than thirty-six hours 2' => ["now - 2 years", "now + 1 day"],
'fallback 1' => ["now - 3 hours", "now + 3 hours"],
'fallback 2' => ["now - 35 hours", "now + 3 hours"],
];
}
public function testComputeNextFetchFrom304WithoutDate() {
$f = new Feed(null, $this->base."NextFetch/NotModifiedEtag");
$exp = strtotime("now + 3 hours"); $exp = strtotime("now + 3 hours");
$this->assertTime($exp, $f->nextFetch); $this->assertTime($exp, $f->nextFetch);
} }

6
tests/docroot/Feed/NextFetch/NotModifiedEtag.php

@ -0,0 +1,6 @@
<?php
return [
'code' => 304,
'cache' => false,
'fields' => ['ETag: "some-etag"'],
];
Loading…
Cancel
Save