]> source.dussan.org Git - nextcloud-server.git/commitdiff
Add test for task cleanup
authorMarcel Klehr <mklehr@gmx.net>
Fri, 30 Jun 2023 11:19:38 +0000 (13:19 +0200)
committerMarcel Klehr <mklehr@gmx.net>
Wed, 9 Aug 2023 08:01:23 +0000 (10:01 +0200)
Signed-off-by: Marcel Klehr <mklehr@gmx.net>
(cherry picked from commit c568c4a4640e79134eaa59625a546915bb706f28)

tests/lib/LanguageModel/LanguageModelManagerTest.php

index 39580fa3cb829d29c3490edefe90ec189bd05d98..423eb525ba270caed28d1bdb2124eb711ed7a872 100644 (file)
@@ -15,6 +15,7 @@ use OC\EventDispatcher\EventDispatcher;
 use OC\LanguageModel\Db\Task;
 use OC\LanguageModel\Db\TaskMapper;
 use OC\LanguageModel\LanguageModelManager;
+use OC\LanguageModel\RemoveOldTasksBackgroundJob;
 use OC\LanguageModel\TaskBackgroundJob;
 use OCP\AppFramework\Db\DoesNotExistException;
 use OCP\AppFramework\Utility\ITimeFactory;
@@ -108,6 +109,8 @@ class LanguageModelManagerTest extends \Test\TestCase {
                $this->coordinator = $this->createMock(Coordinator::class);
                $this->coordinator->expects($this->any())->method('getRegistrationContext')->willReturn($this->registrationContext);
 
+               $this->currentTime = new \DateTimeImmutable('now');
+
                $this->taskMapper = $this->createMock(TaskMapper::class);
                $this->tasksDb = [];
                $this->taskMapper
@@ -115,6 +118,7 @@ class LanguageModelManagerTest extends \Test\TestCase {
                        ->method('insert')
                        ->willReturnCallback(function (Task $task) {
                                $task->setId(count($this->tasksDb) ? max(array_keys($this->tasksDb)) : 1);
+                               $task->setLastUpdated($this->currentTime->getTimestamp());
                                $this->tasksDb[$task->getId()] = $task->toRow();
                                return $task;
                        });
@@ -122,6 +126,7 @@ class LanguageModelManagerTest extends \Test\TestCase {
                        ->expects($this->any())
                        ->method('update')
                        ->willReturnCallback(function (Task $task) {
+                               $task->setLastUpdated($this->currentTime->getTimestamp());
                                $this->tasksDb[$task->getId()] = $task->toRow();
                                return $task;
                        });
@@ -134,6 +139,14 @@ class LanguageModelManagerTest extends \Test\TestCase {
                                }
                                return Task::fromRow($this->tasksDb[$id]);
                        });
+               $this->taskMapper
+                       ->expects($this->any())
+                       ->method('deleteOlderThan')
+                       ->willReturnCallback(function (int $timeout) {
+                               $this->tasksDb = array_filter($this->tasksDb, function (array $task) use ($timeout) {
+                                       return $task['last_updated'] >= $this->currentTime->getTimestamp() - $timeout;
+                               });
+                       });
 
                $this->jobList = $this->createPartialMock(DummyJobList::class, ['add']);
                $this->jobList->expects($this->any())->method('add')->willReturnCallback(function () {
@@ -336,4 +349,28 @@ class LanguageModelManagerTest extends \Test\TestCase {
                $this->assertNull($task3->getOutput());
                $this->assertEquals(ILanguageModelTask::STATUS_FAILED, $task3->getStatus());
        }
+
+       public function testOldTasksShouldBeCleanedUp() {
+               $this->registrationContext->expects($this->any())->method('getLanguageModelProviders')->willReturn([
+                       new ServiceRegistration('test', TestVanillaLanguageModelProvider::class)
+               ]);
+               $this->assertCount(1, $this->languageModelManager->getAvailableTasks());
+               $this->assertCount(1, $this->languageModelManager->getAvailableTaskTypes());
+               $this->assertTrue($this->languageModelManager->hasProviders());
+               $task = new FreePromptTask('Hello', 'test', null);
+               $this->assertEquals('Hello Free Prompt', $this->languageModelManager->runTask($task));
+
+               $this->currentTime = $this->currentTime->add(new \DateInterval('P1Y'));
+               // run background job
+               $bgJob = new RemoveOldTasksBackgroundJob(
+                       \OC::$server->get(ITimeFactory::class),
+                       $this->taskMapper,
+                       \OC::$server->get(LoggerInterface::class),
+               );
+               $bgJob->setArgument([]);
+               $bgJob->start($this->jobList);
+
+               $this->expectException(NotFoundException::class);
+               $this->languageModelManager->getTask($task->getId());
+       }
 }