PropagatorTest.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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. protected function setUp(): void {
  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 testSizePropagationNoNegative() {
  72. $paths = ['', 'foo', 'foo/bar'];
  73. $oldInfos = $this->getFileInfos($paths);
  74. $this->storage->getPropagator()->propagateChange('foo/bar/file.txt', time(), -100);
  75. $newInfos = $this->getFileInfos($paths);
  76. foreach ($oldInfos as $i => $oldInfo) {
  77. $this->assertEquals(-1, $newInfos[$i]->getSize());
  78. }
  79. }
  80. public function testBatchedPropagation() {
  81. $this->storage->mkdir('foo/baz');
  82. $this->storage->mkdir('asd');
  83. $this->storage->file_put_contents('asd/file.txt', 'bar');
  84. $this->storage->file_put_contents('foo/baz/file.txt', 'bar');
  85. $this->storage->getScanner()->scan('');
  86. $paths = ['', 'foo', 'foo/bar', 'asd', 'foo/baz'];
  87. $oldInfos = $this->getFileInfos($paths);
  88. $propagator = $this->storage->getPropagator();
  89. $propagator->beginBatch();
  90. $propagator->propagateChange('asd/file.txt', time(), 10);
  91. $propagator->propagateChange('foo/bar/file.txt', time(), 2);
  92. $newInfos = $this->getFileInfos($paths);
  93. // no changes until we finish the batch
  94. foreach ($oldInfos as $i => $oldInfo) {
  95. $this->assertEquals($oldInfo->getSize(), $newInfos[$i]->getSize());
  96. $this->assertEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
  97. $this->assertEquals($oldInfo->getMTime(), $newInfos[$i]->getMTime());
  98. }
  99. $propagator->commitBatch();
  100. $newInfos = $this->getFileInfos($paths);
  101. foreach ($oldInfos as $i => $oldInfo) {
  102. if ($oldInfo->getPath() !== 'foo/baz') {
  103. $this->assertNotEquals($oldInfo->getEtag(), $newInfos[$i]->getEtag());
  104. }
  105. }
  106. $this->assertEquals($oldInfos['']->getSize() + 12, $newInfos['']->getSize());
  107. $this->assertEquals($oldInfos['asd']->getSize() + 10, $newInfos['asd']->getSize());
  108. $this->assertEquals($oldInfos['foo']->getSize() + 2, $newInfos['foo']->getSize());
  109. $this->assertEquals($oldInfos['foo/bar']->getSize() + 2, $newInfos['foo/bar']->getSize());
  110. $this->assertEquals($oldInfos['foo/baz']->getSize(), $newInfos['foo/baz']->getSize());
  111. }
  112. }