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