1
0

PropagatorTest.php 4.3 KB

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