|
25 | 25 | use OCP\Files\Mount\IMountPoint; |
26 | 26 | use OCP\Files\Storage\IStorage; |
27 | 27 | use OCP\Files\StorageNotAvailableException; |
| 28 | +use OCP\Lock\ILockingProvider; |
28 | 29 | use PHPUnit\Framework\MockObject\MockObject; |
29 | 30 | use Sabre\DAV\Exception\NotFound; |
30 | 31 | use Test\Traits\UserTrait; |
@@ -173,6 +174,47 @@ public function testDeleteFolderThrowsWhenDeletionFailed(): void { |
173 | 174 | $dir->delete(); |
174 | 175 | } |
175 | 176 |
|
| 177 | + /** |
| 178 | + * A failed or interrupted upload must not leave the exclusive part-file |
| 179 | + * lock behind. Otherwise every later upload to the same path keeps getting |
| 180 | + * rejected with "423 Locked" until the lock TTL expires (up to an hour), |
| 181 | + * createFile() therefore releases the locks in a finally block. |
| 182 | + */ |
| 183 | + public function testCreateFileReleasesPartFileLockOnFailure(): void { |
| 184 | + $name = 'foo.txt'; |
| 185 | + |
| 186 | + $this->view->method('getRelativePath')->willReturnArgument(0); |
| 187 | + $this->view->method('getAbsolutePath')->willReturnArgument(0); |
| 188 | + $this->view->method('isCreatable')->willReturn(true); |
| 189 | + // the target does not exist yet |
| 190 | + $this->view->method('getFileInfo')->willReturn(false); |
| 191 | + // make File::put() fail right after the locks have been acquired |
| 192 | + $this->view->method('resolvePath')->willReturn([null, null]); |
| 193 | + |
| 194 | + $released = []; |
| 195 | + $this->view->method('unlockFile') |
| 196 | + ->willReturnCallback(function (string $path, int $type) use (&$released): bool { |
| 197 | + $released[] = [$path, $type]; |
| 198 | + return true; |
| 199 | + }); |
| 200 | + |
| 201 | + $dir = new Directory($this->view, $this->info); |
| 202 | + $partLockPath = $dir->getPath() . '/' . $name . '.upload.part'; |
| 203 | + |
| 204 | + try { |
| 205 | + $dir->createFile($name, 'test data'); |
| 206 | + $this->fail('Expected the failing upload to throw'); |
| 207 | + } catch (\Sabre\DAV\Exception\ServiceUnavailable) { |
| 208 | + // expected: File::put() cannot resolve the storage |
| 209 | + } |
| 210 | + |
| 211 | + $this->assertContains( |
| 212 | + [$partLockPath, ILockingProvider::LOCK_EXCLUSIVE], |
| 213 | + $released, |
| 214 | + 'The exclusive .upload.part lock must be released after a failed upload', |
| 215 | + ); |
| 216 | + } |
| 217 | + |
176 | 218 | public function testGetChildren(): void { |
177 | 219 | $info1 = $this->createMock(FileInfo::class); |
178 | 220 | $info2 = $this->createMock(FileInfo::class); |
|
0 commit comments