PropagatorTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Robin Appelman <icewind@owncloud.com>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test\Files\Cache;
  9. use OC\Files\Storage\Temporary;
  10. use OCP\Files\Cache\ICacheEntry;
  11. use OCP\Files\Storage\IStorage;
  12. use Test\TestCase;
  13. /**
  14. * @group DB
  15. */
  16. class PropagatorTest extends TestCase {
  17. /** @var IStorage */
  18. private $storage;
  19. public function setUp() {
  20. parent::setUp();
  21. $this->storage = new Temporary();
  22. $this->storage->mkdir('foo/bar');
  23. $this->storage->file_put_contents('foo/bar/file.txt', 'bar');
  24. $this->storage->getScanner()->scan('');
  25. }
  26. /**
  27. * @param $paths
  28. * @return ICacheEntry[]
  29. */
  30. private function getFileInfos($paths) {
  31. $values = array_map(function ($path) {
  32. return $this->storage->getCache()->get($path);
  33. }, $paths);
  34. return array_combine($paths, $values);
  35. }
  36. public function testEtagPropagation() {
  37. $paths = ['', 'foo', 'foo/bar'];
  38. $oldInfos = $this->getFileInfos($paths);
  39. $this->storage->getPropagator()->propagateChange('foo/bar/file.txt', time());
  40. $newInfos = $this->getFileInfos($paths);
  41. foreach ($oldInfos as $i => $oldInfo) {
  42. $this->assertNotEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
  43. }
  44. }
  45. public function testTimePropagation() {
  46. $paths = ['', 'foo', 'foo/bar'];
  47. $oldTime = time() - 200;
  48. $targetTime = time() - 100;
  49. $now = time();
  50. $cache = $this->storage->getCache();
  51. $cache->put('', ['mtime' => $now]);
  52. $cache->put('foo', ['mtime' => $now]);
  53. $cache->put('foo/bar', ['mtime' => $oldTime]);
  54. $cache->put('foo/bar/file.txt', ['mtime' => $oldTime]);
  55. $this->storage->getPropagator()->propagateChange('foo/bar/file.txt', $targetTime);
  56. $newInfos = $this->getFileInfos($paths);
  57. $this->assertEquals($targetTime, $newInfos['foo/bar']->getMTime());
  58. // dont lower mtimes
  59. $this->assertEquals($now, $newInfos['foo']->getMTime());
  60. $this->assertEquals($now, $newInfos['']->getMTime());
  61. }
  62. public function testSizePropagation() {
  63. $paths = ['', 'foo', 'foo/bar'];
  64. $oldInfos = $this->getFileInfos($paths);
  65. $this->storage->getPropagator()->propagateChange('foo/bar/file.txt', time(), 10);
  66. $newInfos = $this->getFileInfos($paths);
  67. foreach ($oldInfos as $i => $oldInfo) {
  68. $this->assertEquals($oldInfo->getSize() + 10, $newInfos[$i]->getSize());
  69. }
  70. }
  71. public function testBatchedPropagation() {
  72. $this->storage->mkdir('foo/baz');
  73. $this->storage->mkdir('asd');
  74. $this->storage->file_put_contents('asd/file.txt', 'bar');
  75. $this->storage->file_put_contents('foo/baz/file.txt', 'bar');
  76. $this->storage->getScanner()->scan('');
  77. $paths = ['', 'foo', 'foo/bar', 'asd', 'foo/baz'];
  78. $oldInfos = $this->getFileInfos($paths);
  79. $propagator = $this->storage->getPropagator();
  80. $propagator->beginBatch();
  81. $propagator->propagateChange('asd/file.txt', time(), 10);
  82. $propagator->propagateChange('foo/bar/file.txt', time(), 2);
  83. $newInfos = $this->getFileInfos($paths);
  84. // no changes until we finish the batch
  85. foreach ($oldInfos as $i => $oldInfo) {
  86. $this->assertEquals($oldInfo->getSize(), $newInfos[$i]->getSize());
  87. $this->assertEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
  88. $this->assertEquals($oldInfo->getMTime(), $newInfos[$i]->getMTime());
  89. }
  90. $propagator->commitBatch();
  91. $newInfos = $this->getFileInfos($paths);
  92. foreach ($oldInfos as $i => $oldInfo) {
  93. if ($oldInfo->getPath() !== 'foo/baz') {
  94. $this->assertNotEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
  95. }
  96. }
  97. $this->assertEquals($oldInfos['']->getSize() + 12, $newInfos['']->getSize());
  98. $this->assertEquals($oldInfos['asd']->getSize() + 10, $newInfos['asd']->getSize());
  99. $this->assertEquals($oldInfos['foo']->getSize() + 2, $newInfos['foo']->getSize());
  100. $this->assertEquals($oldInfos['foo/bar']->getSize() + 2, $newInfos['foo/bar']->getSize());
  101. $this->assertEquals($oldInfos['foo/baz']->getSize(), $newInfos['foo/baz']->getSize());
  102. }
  103. }