fileglobalgc.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Robin Appelman
  6. * @copyright 2012 Robin Appelman icewind@owncloud.com
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. */
  22. namespace Test\Cache;
  23. use Test\TestCase;
  24. class FileGlobalGC extends TestCase {
  25. /**
  26. * @var string
  27. */
  28. private $cacheDir;
  29. /**
  30. * @var \OC\Cache\FileGlobalGC
  31. */
  32. private $gc;
  33. public function setUp() {
  34. $this->cacheDir = \OC::$server->getTempManager()->getTemporaryFolder();
  35. $this->gc = new \OC\Cache\FileGlobalGC();
  36. }
  37. private function addCacheFile($name, $expire) {
  38. file_put_contents($this->cacheDir . $name, 'foo');
  39. touch($this->cacheDir . $name, $expire);
  40. }
  41. public function testGetExpiredEmpty() {
  42. $this->assertEquals([], $this->gc->getExpiredPaths($this->cacheDir, time()));
  43. }
  44. public function testGetExpiredNone() {
  45. $time = time();
  46. $this->addCacheFile('foo', $time + 10);
  47. $this->assertEquals([], $this->gc->getExpiredPaths($this->cacheDir, $time));
  48. }
  49. public function testGetExpired() {
  50. $time = time();
  51. $this->addCacheFile('foo', $time + 10);
  52. $this->addCacheFile('bar', $time);
  53. $this->addCacheFile('bar2', $time - 10);
  54. $this->addCacheFile('asd', $time - 100);
  55. $this->assertEquals([$this->cacheDir . 'asd', $this->cacheDir . 'bar2'], $this->gc->getExpiredPaths($this->cacheDir, $time));
  56. }
  57. public function testGetExpiredDirectory() {
  58. $time = time();
  59. $this->addCacheFile('foo', $time - 10);
  60. mkdir($this->cacheDir . 'asd');
  61. $this->assertEquals([$this->cacheDir . 'foo'], $this->gc->getExpiredPaths($this->cacheDir, $time));
  62. }
  63. public function testGcUnlink() {
  64. $time = time();
  65. $this->addCacheFile('foo', $time - 10);
  66. $this->addCacheFile('bar', $time - 10);
  67. $this->addCacheFile('asd', $time + 10);
  68. $config = $this->getMock('\OCP\IConfig');
  69. $config->expects($this->once())
  70. ->method('getAppValue')
  71. ->with('core', 'global_cache_gc_lastrun', 0)
  72. ->willReturn($time - \OC\Cache\FileGlobalGC::CLEANUP_TTL_SEC - 1);
  73. $config->expects($this->once())
  74. ->method('setAppValue');
  75. $this->gc->gc($config, $this->cacheDir);
  76. $this->assertFileNotExists($this->cacheDir . 'foo');
  77. $this->assertFileNotExists($this->cacheDir . 'bar');
  78. $this->assertFileExists($this->cacheDir . 'asd');
  79. }
  80. public function testGcLastRun() {
  81. $time = time();
  82. $config = $this->getMock('\OCP\IConfig');
  83. $config->expects($this->once())
  84. ->method('getAppValue')
  85. ->with('core', 'global_cache_gc_lastrun', 0)
  86. ->willReturn($time);
  87. $config->expects($this->never())
  88. ->method('setAppValue');
  89. $this->gc->gc($config, $this->cacheDir);
  90. }
  91. }