From d52328660eadac6fcd2183ee523c51e49f656914 Mon Sep 17 00:00:00 2001 From: Robin Appelman Date: Tue, 22 Sep 2020 15:03:17 +0200 Subject: only run the background scanner for users that have unscanned files instead of looping trough all users and seeing if they have unscanned files, we do a single query to find all storages that need scanning and run trough the users for them Signed-off-by: Robin Appelman --- apps/files/tests/BackgroundJob/ScanFilesTest.php | 155 +++++++++-------------- 1 file changed, 60 insertions(+), 95 deletions(-) (limited to 'apps/files/tests') diff --git a/apps/files/tests/BackgroundJob/ScanFilesTest.php b/apps/files/tests/BackgroundJob/ScanFilesTest.php index 75557955d6a..1bc449c6c62 100644 --- a/apps/files/tests/BackgroundJob/ScanFilesTest.php +++ b/apps/files/tests/BackgroundJob/ScanFilesTest.php @@ -25,129 +25,94 @@ namespace OCA\Files\Tests\BackgroundJob; +use OC\Files\Mount\MountPoint; +use OC\Files\Storage\Temporary; use OCA\Files\BackgroundJob\ScanFiles; +use OCP\EventDispatcher\IEventDispatcher; use OCP\IConfig; +use OCP\ILogger; use OCP\IUser; use OCP\IUserManager; use Test\TestCase; +use Test\Traits\MountProviderTrait; +use Test\Traits\UserTrait; /** * Class ScanFilesTest * * @package OCA\Files\Tests\BackgroundJob + * @group DB */ class ScanFilesTest extends TestCase { - /** @var IConfig */ - private $config; - /** @var IUserManager */ - private $userManager; + use UserTrait; + use MountProviderTrait; + /** @var ScanFiles */ private $scanFiles; + /** @var \OCP\Files\Config\IUserMountCache */ + private $mountCache; protected function setUp(): void { parent::setUp(); - $this->config = $this->createMock(IConfig::class); - $this->userManager = $this->createMock(IUserManager::class); + $config = $this->createMock(IConfig::class); + $userManager = $this->createMock(IUserManager::class); + $dispatcher = $this->createMock(IEventDispatcher::class); + $logger = $this->createMock(ILogger::class); + $connection = \OC::$server->getDatabaseConnection(); + $this->mountCache = \OC::$server->getUserMountCache(); $this->scanFiles = $this->getMockBuilder('\OCA\Files\BackgroundJob\ScanFiles') - ->setConstructorArgs([ - $this->config, - $this->userManager, - ]) - ->setMethods(['runScanner']) - ->getMock(); + ->setConstructorArgs([ + $config, + $userManager, + $dispatcher, + $logger, + $connection, + ]) + ->setMethods(['runScanner']) + ->getMock(); } - public function testRunWithoutUsers() { - $this->config - ->expects($this->at(0)) - ->method('getSystemValueBool') - ->with('files_no_background_scan', false) - ->willReturn(false); - $this->config - ->expects($this->at(1)) - ->method('getAppValue') - ->with('files', 'cronjob_scan_files', 0) - ->willReturn(50); - $this->userManager - ->expects($this->at(0)) - ->method('search') - ->with('', 500, 50) - ->willReturn([]); - $this->userManager - ->expects($this->at(1)) - ->method('search') - ->with('', 500) - ->willReturn([]); - $this->config - ->expects($this->at(2)) - ->method('setAppValue') - ->with('files', 'cronjob_scan_files', 500); - + private function runJob() { $this->invokePrivate($this->scanFiles, 'run', [[]]); } - public function testRunWithUsers() { - $fakeUser = $this->createMock(IUser::class); - $this->config - ->expects($this->at(0)) - ->method('getSystemValueBool') - ->with('files_no_background_scan', false) - ->willReturn(false); - $this->config - ->expects($this->at(1)) - ->method('getAppValue') - ->with('files', 'cronjob_scan_files', 0) - ->willReturn(50); - $this->userManager - ->expects($this->at(0)) - ->method('search') - ->with('', 500, 50) - ->willReturn([ - $fakeUser - ]); - $this->config - ->expects($this->at(2)) - ->method('setAppValue') - ->with('files', 'cronjob_scan_files', 550); - $this->scanFiles - ->expects($this->once()) - ->method('runScanner') - ->with($fakeUser); + private function getUser(string $userId): IUser { + $user = $this->createMock(IUser::class); + $user->method('getUID') + ->willReturn($userId); + return $user; + } - $this->invokePrivate($this->scanFiles, 'run', [[]]); + private function setupStorage(string $user, string $mountPoint) { + $storage = new Temporary([]); + $storage->mkdir('foo'); + $storage->getScanner()->scan(''); + + $this->createUser($user, ''); + $this->mountCache->registerMounts($this->getUser($user), [ + new MountPoint($storage, $mountPoint) + ]); + + return $storage; } - public function testRunWithUsersAndOffsetAtEndOfUserList() { - $this->config - ->expects($this->at(0)) - ->method('getSystemValueBool') - ->with('files_no_background_scan', false) - ->willReturn(false); - $this->config - ->expects($this->at(1)) - ->method('getAppValue') - ->with('files', 'cronjob_scan_files', 0) - ->willReturn(50); - $this->userManager - ->expects($this->at(0)) - ->method('search') - ->with('', 500, 50) - ->willReturn([]); - $this->userManager - ->expects($this->at(1)) - ->method('search') - ->with('', 500) - ->willReturn([]); - $this->config - ->expects($this->at(2)) - ->method('setAppValue') - ->with('files', 'cronjob_scan_files', 500); - $this->scanFiles - ->expects($this->never()) - ->method('runScanner'); + public function testAllScanned() { + $this->setupStorage('foouser', '/foousers/files/foo'); - $this->invokePrivate($this->scanFiles, 'run', [[]]); + $this->scanFiles->expects($this->never()) + ->method('runScanner'); + $this->runJob(); + } + + public function testUnscanned() { + $storage = $this->setupStorage('foouser', '/foousers/files/foo'); + $storage->getCache()->put('foo', ['size' => -1]); + + $this->scanFiles->expects($this->once()) + ->method('runScanner') + ->with('foouser'); + $this->runJob(); } } -- cgit v1.2.3