mirror of
https://github.com/NNTmux/newznab-tmux.git
synced 2026-08-29 02:01:33 +00:00
Update additional PP
This commit is contained in:
@@ -99,19 +99,86 @@ class AdditionalCandidateQueryTest extends TestCase
|
||||
$this->assertSame(['0', '9', 'a', 'f'], $chars);
|
||||
}
|
||||
|
||||
public function test_bucket_chars_skip_active_claims_but_include_stale_claims(): void
|
||||
{
|
||||
DB::table('categories')->insert([
|
||||
['id' => 1, 'disablepreview' => 0],
|
||||
]);
|
||||
|
||||
DB::table('releases')->insert([
|
||||
$this->releaseRow(1, 'a', claimedAt: now()),
|
||||
$this->releaseRow(2, 'b', claimedAt: now()->subSeconds(301)),
|
||||
$this->releaseRow(3, 'c'),
|
||||
]);
|
||||
|
||||
$chars = AdditionalCandidateQuery::bucketChars();
|
||||
sort($chars);
|
||||
|
||||
$this->assertSame(['b', 'c'], $chars);
|
||||
}
|
||||
|
||||
public function test_monitor_builder_can_include_claimed_releases_while_available_builder_excludes_them(): void
|
||||
{
|
||||
DB::table('categories')->insert([
|
||||
['id' => 1, 'disablepreview' => 0],
|
||||
]);
|
||||
|
||||
DB::table('releases')->insert([
|
||||
$this->releaseRow(1, 'a', claimedAt: now()),
|
||||
$this->releaseRow(2, 'b'),
|
||||
]);
|
||||
|
||||
$this->assertSame(1, AdditionalCandidateQuery::baseBuilder()->count());
|
||||
$this->assertSame(2, AdditionalCandidateQuery::baseBuilder(includeClaimed: true)->count());
|
||||
}
|
||||
|
||||
public function test_claim_batch_excludes_active_claims_and_recovers_stale_claims(): void
|
||||
{
|
||||
DB::table('categories')->insert([
|
||||
['id' => 1, 'disablepreview' => 0],
|
||||
]);
|
||||
|
||||
DB::table('releases')->insert([
|
||||
$this->releaseRow(1, 'a', postdate: '2026-07-12 10:00:00'),
|
||||
$this->releaseRow(2, 'a', postdate: '2026-07-12 09:00:00'),
|
||||
]);
|
||||
|
||||
$first = AdditionalCandidateQuery::claimBatch('a', 1, 'token-one', columns: ['id']);
|
||||
$this->assertSame([1], $first->pluck('id')->all());
|
||||
|
||||
$second = AdditionalCandidateQuery::claimBatch('a', 10, 'token-two', columns: ['id']);
|
||||
$this->assertSame([2], $second->pluck('id')->all());
|
||||
|
||||
DB::table('releases')
|
||||
->where('id', 1)
|
||||
->update(['additional_pp_claimed_at' => now()->subSeconds(301)]);
|
||||
|
||||
$third = AdditionalCandidateQuery::claimBatch('a', 10, 'token-three', columns: ['id']);
|
||||
$this->assertSame([1], $third->pluck('id')->all());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function releaseRow(int $id, string $leftguid, int $categoriesId = 1): array
|
||||
{
|
||||
private function releaseRow(
|
||||
int $id,
|
||||
string $leftguid,
|
||||
int $categoriesId = 1,
|
||||
?\DateTimeInterface $claimedAt = null,
|
||||
string $postdate = '2026-07-12 00:00:00'
|
||||
): array {
|
||||
return [
|
||||
'id' => $id,
|
||||
'guid' => $leftguid.'-guid-'.$id,
|
||||
'leftguid' => $leftguid,
|
||||
'passwordstatus' => -1,
|
||||
'haspreview' => -1,
|
||||
'nzbstatus' => 1,
|
||||
'categories_id' => $categoriesId,
|
||||
'size' => 2 * 1048576,
|
||||
'postdate' => $postdate,
|
||||
'additional_pp_claimed_at' => $claimedAt?->format('Y-m-d H:i:s'),
|
||||
'additional_pp_claim_token' => $claimedAt === null ? null : 'claimed',
|
||||
];
|
||||
}
|
||||
|
||||
@@ -141,6 +208,7 @@ class AdditionalCandidateQueryTest extends TestCase
|
||||
DB::table('settings')->upsert([
|
||||
['name' => 'categorizeforeign', 'value' => '0'],
|
||||
['name' => 'catwebdl', 'value' => '0'],
|
||||
['name' => 'releaseprocessingtimeout', 'value' => '120'],
|
||||
], ['name'], ['value']);
|
||||
|
||||
Schema::dropIfExists('releases');
|
||||
@@ -153,12 +221,16 @@ class AdditionalCandidateQueryTest extends TestCase
|
||||
|
||||
Schema::create('releases', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('id')->primary();
|
||||
$table->string('guid');
|
||||
$table->char('leftguid', 1);
|
||||
$table->integer('passwordstatus');
|
||||
$table->integer('haspreview');
|
||||
$table->integer('nzbstatus');
|
||||
$table->unsignedInteger('categories_id');
|
||||
$table->unsignedBigInteger('size');
|
||||
$table->dateTime('postdate')->nullable();
|
||||
$table->timestamp('additional_pp_claimed_at')->nullable();
|
||||
$table->string('additional_pp_claim_token', 64)->nullable();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,311 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Models\Release;
|
||||
use App\Services\AdditionalProcessing\AdditionalProcessingOrchestrator;
|
||||
use App\Services\AdditionalProcessing\ConsoleOutputService;
|
||||
use App\Services\AdditionalProcessing\ReleaseProcessor;
|
||||
use App\Services\AdditionalProcessing\State\ReleaseProcessingContext;
|
||||
use App\Services\TempWorkspaceService;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use PDO;
|
||||
use Tests\TestCase;
|
||||
use Tests\Unit\AdditionalProcessing\CreatesProcessingConfiguration;
|
||||
|
||||
class AdditionalProcessingOrchestratorClaimTest extends TestCase
|
||||
{
|
||||
use CreatesProcessingConfiguration;
|
||||
|
||||
private string $databasePath;
|
||||
|
||||
/**
|
||||
* @var array<string, string|false>
|
||||
*/
|
||||
private array $originalEnvironment = [];
|
||||
|
||||
public function createApplication()
|
||||
{
|
||||
$this->databasePath = sys_get_temp_dir().'/nntmux-orchestrator-claim-test.sqlite';
|
||||
|
||||
$this->originalEnvironment = [
|
||||
'APP_ENV' => getenv('APP_ENV'),
|
||||
'DB_CONNECTION' => getenv('DB_CONNECTION'),
|
||||
'DB_DATABASE' => getenv('DB_DATABASE'),
|
||||
];
|
||||
|
||||
if (file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
|
||||
$pdo = new PDO('sqlite:'.$this->databasePath);
|
||||
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
|
||||
$pdo->exec("INSERT INTO settings (name, value) VALUES ('categorizeforeign', '0'), ('catwebdl', '0'), ('releaseprocessingtimeout', '120')");
|
||||
|
||||
$this->setEnvironmentValue('APP_ENV', 'testing');
|
||||
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
|
||||
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
|
||||
|
||||
$app = require __DIR__.'/../../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
config([
|
||||
'database.default' => 'sqlite',
|
||||
'database.connections.sqlite.database' => $this->databasePath,
|
||||
]);
|
||||
|
||||
DB::purge();
|
||||
DB::reconnect();
|
||||
|
||||
$this->createSchema();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
|
||||
foreach ($this->originalEnvironment as $key => $value) {
|
||||
$this->setEnvironmentValue($key, $value === false ? null : $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_handled_release_exception_clears_claim(): void
|
||||
{
|
||||
DB::table('categories')->insert(['id' => 1, 'disablepreview' => 0]);
|
||||
DB::table('releases')->insert([
|
||||
'id' => 1,
|
||||
'guid' => 'a-guid-1',
|
||||
'leftguid' => 'a',
|
||||
'name' => 'Example',
|
||||
'searchname' => 'Example',
|
||||
'size' => 2 * 1048576,
|
||||
'groups_id' => 1,
|
||||
'nfostatus' => -1,
|
||||
'fromname' => 'poster@example.test',
|
||||
'completion' => 100,
|
||||
'categories_id' => 1,
|
||||
'predb_id' => 0,
|
||||
'pp_timeout_count' => 0,
|
||||
'passwordstatus' => -1,
|
||||
'haspreview' => -1,
|
||||
'nzbstatus' => 1,
|
||||
'postdate' => '2026-07-12 10:00:00',
|
||||
'additional_pp_claimed_at' => null,
|
||||
'additional_pp_claim_token' => null,
|
||||
]);
|
||||
|
||||
$processor = new FailingAdditionalReleaseProcessor;
|
||||
$tempWorkspace = new RecordingTempWorkspaceService;
|
||||
$output = new RecordingConsoleOutputService;
|
||||
|
||||
$orchestrator = new AdditionalProcessingOrchestrator(
|
||||
$this->makeConfig(['queryLimit' => 25, 'minSizeMB' => 0, 'maxSizeGB' => 100]),
|
||||
$processor,
|
||||
$tempWorkspace,
|
||||
$output
|
||||
);
|
||||
|
||||
$orchestrator->start('', 'a');
|
||||
|
||||
$this->assertSame(1, $processor->processCalls);
|
||||
$this->assertSame(1, $tempWorkspace->ensureMainTempPathCalls);
|
||||
$this->assertSame(1, $tempWorkspace->clearDirectoryCalls);
|
||||
$this->assertSame(1, $output->echoDescriptionCalls);
|
||||
$this->assertSame(1, $output->endOutputCalls);
|
||||
$this->assertNull(Release::query()->where('id', 1)->value('additional_pp_claimed_at'));
|
||||
$this->assertNull(Release::query()->where('id', 1)->value('additional_pp_claim_token'));
|
||||
}
|
||||
|
||||
public function test_temp_setup_failure_does_not_claim_releases(): void
|
||||
{
|
||||
DB::table('categories')->insert(['id' => 1, 'disablepreview' => 0]);
|
||||
DB::table('releases')->insert([
|
||||
'id' => 1,
|
||||
'guid' => 'a-guid-1',
|
||||
'leftguid' => 'a',
|
||||
'name' => 'Example',
|
||||
'searchname' => 'Example',
|
||||
'size' => 2 * 1048576,
|
||||
'groups_id' => 1,
|
||||
'nfostatus' => -1,
|
||||
'fromname' => 'poster@example.test',
|
||||
'completion' => 100,
|
||||
'categories_id' => 1,
|
||||
'predb_id' => 0,
|
||||
'pp_timeout_count' => 0,
|
||||
'passwordstatus' => -1,
|
||||
'haspreview' => -1,
|
||||
'nzbstatus' => 1,
|
||||
'postdate' => '2026-07-12 10:00:00',
|
||||
'additional_pp_claimed_at' => null,
|
||||
'additional_pp_claim_token' => null,
|
||||
]);
|
||||
|
||||
$processor = new FailingAdditionalReleaseProcessor;
|
||||
$tempWorkspace = new FailingTempWorkspaceService;
|
||||
$output = new RecordingConsoleOutputService;
|
||||
|
||||
$orchestrator = new AdditionalProcessingOrchestrator(
|
||||
$this->makeConfig(['queryLimit' => 25, 'minSizeMB' => 0, 'maxSizeGB' => 100]),
|
||||
$processor,
|
||||
$tempWorkspace,
|
||||
$output
|
||||
);
|
||||
|
||||
$orchestrator->start('', 'a');
|
||||
|
||||
$this->assertSame(0, $processor->processCalls);
|
||||
$this->assertSame(1, $tempWorkspace->ensureMainTempPathCalls);
|
||||
$this->assertSame(0, $output->echoDescriptionCalls);
|
||||
$this->assertSame(0, $output->endOutputCalls);
|
||||
$this->assertStringContainsString('Additional post-processing skipped', $output->warnings[0] ?? '');
|
||||
$this->assertNull(Release::query()->where('id', 1)->value('additional_pp_claimed_at'));
|
||||
$this->assertNull(Release::query()->where('id', 1)->value('additional_pp_claim_token'));
|
||||
}
|
||||
|
||||
private function setEnvironmentValue(string $key, ?string $value): void
|
||||
{
|
||||
if ($value === null) {
|
||||
putenv($key);
|
||||
unset($_ENV[$key], $_SERVER[$key]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
putenv($key.'='.$value);
|
||||
$_ENV[$key] = $value;
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
|
||||
private function createSchema(): void
|
||||
{
|
||||
if (! Schema::hasTable('settings')) {
|
||||
Schema::create('settings', function (Blueprint $table): void {
|
||||
$table->string('name')->primary();
|
||||
$table->text('value')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
DB::table('settings')->upsert([
|
||||
['name' => 'categorizeforeign', 'value' => '0'],
|
||||
['name' => 'catwebdl', 'value' => '0'],
|
||||
['name' => 'releaseprocessingtimeout', 'value' => '120'],
|
||||
], ['name'], ['value']);
|
||||
|
||||
Schema::dropIfExists('releases');
|
||||
Schema::dropIfExists('categories');
|
||||
|
||||
Schema::create('categories', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('id')->primary();
|
||||
$table->boolean('disablepreview')->default(false);
|
||||
});
|
||||
|
||||
Schema::create('releases', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('id')->primary();
|
||||
$table->string('guid');
|
||||
$table->char('leftguid', 1);
|
||||
$table->string('name')->default('');
|
||||
$table->string('searchname')->default('');
|
||||
$table->unsignedBigInteger('size')->default(0);
|
||||
$table->unsignedInteger('groups_id')->default(0);
|
||||
$table->integer('nfostatus')->default(0);
|
||||
$table->string('fromname')->nullable();
|
||||
$table->float('completion')->default(0);
|
||||
$table->unsignedInteger('categories_id');
|
||||
$table->unsignedInteger('predb_id')->default(0);
|
||||
$table->integer('pp_timeout_count')->default(0);
|
||||
$table->integer('passwordstatus');
|
||||
$table->integer('haspreview');
|
||||
$table->integer('nzbstatus');
|
||||
$table->dateTime('postdate')->nullable();
|
||||
$table->timestamp('additional_pp_claimed_at')->nullable();
|
||||
$table->string('additional_pp_claim_token', 64)->nullable();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class FailingAdditionalReleaseProcessor extends ReleaseProcessor
|
||||
{
|
||||
public int $processCalls = 0;
|
||||
|
||||
public function __construct() {}
|
||||
|
||||
public function process(ReleaseProcessingContext $context, string $mainTmpPath): void
|
||||
{
|
||||
$this->processCalls++;
|
||||
|
||||
throw new \RuntimeException('boom');
|
||||
}
|
||||
}
|
||||
|
||||
class RecordingTempWorkspaceService extends TempWorkspaceService
|
||||
{
|
||||
public int $ensureMainTempPathCalls = 0;
|
||||
|
||||
public int $clearDirectoryCalls = 0;
|
||||
|
||||
public function ensureMainTempPath(string $basePath, string $guidChar = '', string $groupID = ''): string
|
||||
{
|
||||
$this->ensureMainTempPathCalls++;
|
||||
|
||||
return '/tmp/additional/';
|
||||
}
|
||||
|
||||
public function clearDirectory(string $path, bool $preserveRoot = true): void
|
||||
{
|
||||
$this->clearDirectoryCalls++;
|
||||
}
|
||||
}
|
||||
|
||||
class FailingTempWorkspaceService extends RecordingTempWorkspaceService
|
||||
{
|
||||
public function ensureMainTempPath(string $basePath, string $guidChar = '', string $groupID = ''): string
|
||||
{
|
||||
$this->ensureMainTempPathCalls++;
|
||||
|
||||
throw new \RuntimeException('Additional post-processing temp path "/root/nope/a/" is not writable');
|
||||
}
|
||||
}
|
||||
|
||||
class RecordingConsoleOutputService extends ConsoleOutputService
|
||||
{
|
||||
public int $echoDescriptionCalls = 0;
|
||||
|
||||
public int $endOutputCalls = 0;
|
||||
|
||||
/**
|
||||
* @var list<string>
|
||||
*/
|
||||
public array $warnings = [];
|
||||
|
||||
public function echoDescription(int $totalReleases): void
|
||||
{
|
||||
$this->echoDescriptionCalls++;
|
||||
}
|
||||
|
||||
public function warning(string $message): void
|
||||
{
|
||||
$this->warnings[] = $message;
|
||||
}
|
||||
|
||||
public function endOutput(): void
|
||||
{
|
||||
$this->endOutputCalls++;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,279 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Facades\Search;
|
||||
use App\Models\Release;
|
||||
use App\Services\AdditionalProcessing\ReleaseFileManager;
|
||||
use App\Services\AdditionalProcessing\State\ReleaseProcessingContext;
|
||||
use App\Services\CollectionCleanupService;
|
||||
use App\Services\NameFixing\NameFixingService;
|
||||
use App\Services\NfoService;
|
||||
use App\Services\Nzb\NzbService;
|
||||
use App\Services\ReleaseImageService;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use Mockery;
|
||||
use PDO;
|
||||
use ReflectionMethod;
|
||||
use Tests\TestCase;
|
||||
use Tests\Unit\AdditionalProcessing\CreatesProcessingConfiguration;
|
||||
|
||||
class AdditionalProcessingReleaseFileManagerTest extends TestCase
|
||||
{
|
||||
use CreatesProcessingConfiguration;
|
||||
|
||||
private string $databasePath;
|
||||
|
||||
/**
|
||||
* @var array<string, string|false>
|
||||
*/
|
||||
private array $originalEnvironment = [];
|
||||
|
||||
public function createApplication()
|
||||
{
|
||||
$this->databasePath = sys_get_temp_dir().'/nntmux-release-file-manager-test.sqlite';
|
||||
|
||||
$this->originalEnvironment = [
|
||||
'APP_ENV' => getenv('APP_ENV'),
|
||||
'DB_CONNECTION' => getenv('DB_CONNECTION'),
|
||||
'DB_DATABASE' => getenv('DB_DATABASE'),
|
||||
];
|
||||
|
||||
if (file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
|
||||
$pdo = new PDO('sqlite:'.$this->databasePath);
|
||||
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
|
||||
$pdo->exec("INSERT INTO settings (name, value) VALUES ('categorizeforeign', '0'), ('catwebdl', '0')");
|
||||
|
||||
$this->setEnvironmentValue('APP_ENV', 'testing');
|
||||
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
|
||||
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
|
||||
|
||||
$app = require __DIR__.'/../../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
config([
|
||||
'database.default' => 'sqlite',
|
||||
'database.connections.sqlite.database' => $this->databasePath,
|
||||
]);
|
||||
|
||||
DB::purge();
|
||||
DB::reconnect();
|
||||
|
||||
$this->createSchema();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Mockery::close();
|
||||
|
||||
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
|
||||
foreach ($this->originalEnvironment as $key => $value) {
|
||||
$this->setEnvironmentValue($key, $value === false ? null : $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_release_file_rows_are_deduped_and_flushed_once_at_finalize(): void
|
||||
{
|
||||
DB::table('releases')->insert($this->releaseRow());
|
||||
|
||||
Search::shouldReceive('updateRelease')->once()->with(1);
|
||||
|
||||
$nameFixing = new CountingNameFixingService;
|
||||
|
||||
$manager = $this->makeManager($nameFixing);
|
||||
$context = new ReleaseProcessingContext(Release::query()->findOrFail(1));
|
||||
|
||||
$this->assertTrue($manager->addFileInfo([
|
||||
'name' => 'Example.Movie.2026.mkv',
|
||||
'size' => 1024,
|
||||
'date' => 1_788_600_000,
|
||||
'pass' => 0,
|
||||
'crc32' => 'ABC123',
|
||||
], $context, '\\.(?:par2|sfv|nzb)'));
|
||||
$this->assertFalse($manager->addFileInfo([
|
||||
'name' => 'Example.Movie.2026.mkv',
|
||||
'size' => 1024,
|
||||
'date' => 1_788_600_000,
|
||||
'pass' => 0,
|
||||
'crc32' => 'ABC123',
|
||||
], $context, '\\.(?:par2|sfv|nzb)'));
|
||||
|
||||
$manager->finalizeRelease($context, false);
|
||||
|
||||
$this->assertSame(1, $nameFixing->matchPreDbFilesCalls);
|
||||
$this->assertSame(1, DB::table('release_files')->count());
|
||||
$this->assertSame(1, DB::table('releases')->where('id', 1)->value('rarinnerfilecount'));
|
||||
$this->assertNull(DB::table('releases')->where('id', 1)->value('additional_pp_claimed_at'));
|
||||
$this->assertNull(DB::table('releases')->where('id', 1)->value('additional_pp_claim_token'));
|
||||
}
|
||||
|
||||
public function test_queued_par_hashes_flush_with_release_files(): void
|
||||
{
|
||||
DB::table('releases')->insert($this->releaseRow());
|
||||
|
||||
Search::shouldReceive('updateRelease')->once()->with(1);
|
||||
|
||||
$manager = $this->makeManager();
|
||||
$context = new ReleaseProcessingContext(Release::query()->findOrFail(1));
|
||||
|
||||
$queue = new ReflectionMethod(ReleaseFileManager::class, 'queueReleaseFile');
|
||||
$queue->invoke(
|
||||
$manager,
|
||||
$context,
|
||||
1,
|
||||
'Example.Movie.2026.par2',
|
||||
512,
|
||||
1_788_600_000,
|
||||
0,
|
||||
'1234567890abcdef1234567890abcdef'
|
||||
);
|
||||
|
||||
$manager->finalizeRelease($context, false);
|
||||
|
||||
$this->assertSame(1, DB::table('release_files')->count());
|
||||
$this->assertSame(1, DB::table('par_hashes')->count());
|
||||
}
|
||||
|
||||
private function makeManager(?NameFixingService $nameFixing = null): ReleaseFileManager
|
||||
{
|
||||
return new ReleaseFileManager(
|
||||
$this->makeConfig(),
|
||||
new ReleaseImageService,
|
||||
new NfoService,
|
||||
new TestNzbService,
|
||||
$nameFixing ?? new CountingNameFixingService
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function releaseRow(): array
|
||||
{
|
||||
return [
|
||||
'id' => 1,
|
||||
'guid' => 'guid-1',
|
||||
'name' => 'Example',
|
||||
'searchname' => 'Example',
|
||||
'size' => 1024,
|
||||
'groups_id' => 1,
|
||||
'nfostatus' => -1,
|
||||
'categories_id' => 10,
|
||||
'passwordstatus' => -1,
|
||||
'haspreview' => -1,
|
||||
'nzbstatus' => 1,
|
||||
'rarinnerfilecount' => 0,
|
||||
'pp_timeout_count' => 0,
|
||||
'additional_pp_claimed_at' => now(),
|
||||
'additional_pp_claim_token' => 'token',
|
||||
];
|
||||
}
|
||||
|
||||
private function setEnvironmentValue(string $key, ?string $value): void
|
||||
{
|
||||
if ($value === null) {
|
||||
putenv($key);
|
||||
unset($_ENV[$key], $_SERVER[$key]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
putenv($key.'='.$value);
|
||||
$_ENV[$key] = $value;
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
|
||||
private function createSchema(): void
|
||||
{
|
||||
if (! Schema::hasTable('settings')) {
|
||||
Schema::create('settings', function (Blueprint $table): void {
|
||||
$table->string('name')->primary();
|
||||
$table->text('value')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
DB::table('settings')->upsert([
|
||||
['name' => 'categorizeforeign', 'value' => '0'],
|
||||
['name' => 'catwebdl', 'value' => '0'],
|
||||
], ['name'], ['value']);
|
||||
|
||||
Schema::dropIfExists('par_hashes');
|
||||
Schema::dropIfExists('release_files');
|
||||
Schema::dropIfExists('releases');
|
||||
|
||||
Schema::create('releases', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('id')->primary();
|
||||
$table->string('guid');
|
||||
$table->string('name')->default('');
|
||||
$table->string('searchname')->default('');
|
||||
$table->unsignedBigInteger('size')->default(0);
|
||||
$table->unsignedInteger('groups_id')->default(0);
|
||||
$table->integer('nfostatus')->default(0);
|
||||
$table->integer('categories_id')->default(10);
|
||||
$table->integer('passwordstatus')->default(-1);
|
||||
$table->integer('haspreview')->default(-1);
|
||||
$table->integer('nzbstatus')->default(1);
|
||||
$table->integer('rarinnerfilecount')->default(0);
|
||||
$table->integer('pp_timeout_count')->default(0);
|
||||
$table->timestamp('additional_pp_claimed_at')->nullable();
|
||||
$table->string('additional_pp_claim_token', 64)->nullable();
|
||||
});
|
||||
|
||||
Schema::create('release_files', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('releases_id');
|
||||
$table->string('name');
|
||||
$table->unsignedBigInteger('size')->default(0);
|
||||
$table->string('crc32')->default('');
|
||||
$table->timestamp('created_at')->nullable();
|
||||
$table->timestamp('updated_at')->nullable();
|
||||
$table->boolean('passworded')->default(false);
|
||||
$table->primary(['releases_id', 'name']);
|
||||
});
|
||||
|
||||
Schema::create('par_hashes', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('releases_id');
|
||||
$table->string('hash', 32);
|
||||
$table->primary(['releases_id', 'hash']);
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
class CountingNameFixingService extends NameFixingService
|
||||
{
|
||||
public int $matchPreDbFilesCalls = 0;
|
||||
|
||||
public function matchPreDbFiles(object $release, bool $echo, bool $nameStatus, bool $show): int
|
||||
{
|
||||
$this->matchPreDbFilesCalls++;
|
||||
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
|
||||
class TestNzbService extends NzbService
|
||||
{
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct(new CollectionCleanupService);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,224 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Feature;
|
||||
|
||||
use App\Services\Runners\PostProcessRunner;
|
||||
use Illuminate\Contracts\Console\Kernel;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
use PDO;
|
||||
use Tests\TestCase;
|
||||
|
||||
class PostProcessRunnerAdditionalThreadsTest extends TestCase
|
||||
{
|
||||
private string $databasePath;
|
||||
|
||||
private string $tmpUnrarPath;
|
||||
|
||||
/**
|
||||
* @var array<string, string|false>
|
||||
*/
|
||||
private array $originalEnvironment = [];
|
||||
|
||||
public function createApplication()
|
||||
{
|
||||
$this->databasePath = sys_get_temp_dir().'/nntmux-postprocess-additional-threads.sqlite';
|
||||
$this->tmpUnrarPath = sys_get_temp_dir().'/nntmux-additional-threads-'.uniqid('', true);
|
||||
|
||||
$this->originalEnvironment = [
|
||||
'APP_ENV' => getenv('APP_ENV'),
|
||||
'DB_CONNECTION' => getenv('DB_CONNECTION'),
|
||||
'DB_DATABASE' => getenv('DB_DATABASE'),
|
||||
];
|
||||
|
||||
if (file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
|
||||
$pdo = new PDO('sqlite:'.$this->databasePath);
|
||||
$pdo->exec('CREATE TABLE settings (name VARCHAR PRIMARY KEY, value TEXT NULL)');
|
||||
$pdo->exec("INSERT INTO settings (name, value) VALUES ('categorizeforeign', '0'), ('catwebdl', '0'), ('postthreads', '5'), ('releaseprocessingtimeout', '120')");
|
||||
|
||||
$this->setEnvironmentValue('APP_ENV', 'testing');
|
||||
$this->setEnvironmentValue('DB_CONNECTION', 'sqlite');
|
||||
$this->setEnvironmentValue('DB_DATABASE', $this->databasePath);
|
||||
|
||||
$app = require __DIR__.'/../../bootstrap/app.php';
|
||||
$app->make(Kernel::class)->bootstrap();
|
||||
|
||||
return $app;
|
||||
}
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
config([
|
||||
'database.default' => 'sqlite',
|
||||
'database.connections.sqlite.database' => $this->databasePath,
|
||||
'nntmux.echocli' => false,
|
||||
'nntmux.tmp_unrar_path' => $this->tmpUnrarPath,
|
||||
]);
|
||||
|
||||
DB::purge();
|
||||
DB::reconnect();
|
||||
|
||||
$this->createSchema();
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->databasePath !== '' && file_exists($this->databasePath)) {
|
||||
unlink($this->databasePath);
|
||||
}
|
||||
if ($this->tmpUnrarPath !== '' && is_dir($this->tmpUnrarPath)) {
|
||||
app('files')->deleteDirectory($this->tmpUnrarPath);
|
||||
}
|
||||
|
||||
parent::tearDown();
|
||||
|
||||
foreach ($this->originalEnvironment as $key => $value) {
|
||||
$this->setEnvironmentValue($key, $value === false ? null : $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function test_process_additional_uses_configured_postthreads_for_streaming_process_pool(): void
|
||||
{
|
||||
DB::table('categories')->insert(['id' => 1, 'disablepreview' => 0]);
|
||||
foreach (['0', '1', '2', '3', '4'] as $index => $leftguid) {
|
||||
DB::table('releases')->insert($this->releaseRow($index + 1, $leftguid));
|
||||
}
|
||||
|
||||
$runner = new class extends PostProcessRunner
|
||||
{
|
||||
public int $capturedMaxProcesses = 0;
|
||||
|
||||
/**
|
||||
* @var array<string|int, string>
|
||||
*/
|
||||
public array $capturedCommands = [];
|
||||
|
||||
protected function runStreamingCommands(array $commands, int $maxProcesses, string $desc): void
|
||||
{
|
||||
$this->capturedCommands = $commands;
|
||||
$this->capturedMaxProcesses = $maxProcesses;
|
||||
}
|
||||
};
|
||||
|
||||
$runner->processAdditional();
|
||||
|
||||
$this->assertSame(5, $runner->capturedMaxProcesses);
|
||||
$this->assertCount(5, $runner->capturedCommands);
|
||||
$this->assertContains(PHP_BINARY.' artisan postprocess:guid additional 0', $runner->capturedCommands);
|
||||
$this->assertContains(PHP_BINARY.' artisan postprocess:guid additional 4', $runner->capturedCommands);
|
||||
}
|
||||
|
||||
public function test_process_additional_repeats_hot_bucket_to_fill_configured_threads(): void
|
||||
{
|
||||
DB::table('categories')->insert(['id' => 1, 'disablepreview' => 0]);
|
||||
foreach (range(1, 5) as $id) {
|
||||
DB::table('releases')->insert($this->releaseRow($id, 'a'));
|
||||
}
|
||||
|
||||
$runner = new class extends PostProcessRunner
|
||||
{
|
||||
public int $capturedMaxProcesses = 0;
|
||||
|
||||
/**
|
||||
* @var array<string|int, string>
|
||||
*/
|
||||
public array $capturedCommands = [];
|
||||
|
||||
protected function runStreamingCommands(array $commands, int $maxProcesses, string $desc): void
|
||||
{
|
||||
$this->capturedCommands = $commands;
|
||||
$this->capturedMaxProcesses = $maxProcesses;
|
||||
}
|
||||
};
|
||||
|
||||
$runner->processAdditional();
|
||||
|
||||
$this->assertSame(5, $runner->capturedMaxProcesses);
|
||||
$this->assertCount(5, $runner->capturedCommands);
|
||||
$this->assertSame(
|
||||
array_fill(0, 5, PHP_BINARY.' artisan postprocess:guid additional a'),
|
||||
array_values($runner->capturedCommands)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function releaseRow(int $id, string $leftguid): array
|
||||
{
|
||||
return [
|
||||
'id' => $id,
|
||||
'guid' => $leftguid.'-guid-'.$id,
|
||||
'leftguid' => $leftguid,
|
||||
'passwordstatus' => -1,
|
||||
'haspreview' => -1,
|
||||
'nzbstatus' => 1,
|
||||
'categories_id' => 1,
|
||||
'size' => 2 * 1048576,
|
||||
'postdate' => '2026-07-12 10:00:00',
|
||||
'additional_pp_claimed_at' => null,
|
||||
'additional_pp_claim_token' => null,
|
||||
];
|
||||
}
|
||||
|
||||
private function setEnvironmentValue(string $key, ?string $value): void
|
||||
{
|
||||
if ($value === null) {
|
||||
putenv($key);
|
||||
unset($_ENV[$key], $_SERVER[$key]);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
putenv($key.'='.$value);
|
||||
$_ENV[$key] = $value;
|
||||
$_SERVER[$key] = $value;
|
||||
}
|
||||
|
||||
private function createSchema(): void
|
||||
{
|
||||
if (! Schema::hasTable('settings')) {
|
||||
Schema::create('settings', function (Blueprint $table): void {
|
||||
$table->string('name')->primary();
|
||||
$table->text('value')->nullable();
|
||||
});
|
||||
}
|
||||
|
||||
DB::table('settings')->upsert([
|
||||
['name' => 'categorizeforeign', 'value' => '0'],
|
||||
['name' => 'catwebdl', 'value' => '0'],
|
||||
['name' => 'postthreads', 'value' => '5'],
|
||||
['name' => 'releaseprocessingtimeout', 'value' => '120'],
|
||||
], ['name'], ['value']);
|
||||
|
||||
Schema::dropIfExists('releases');
|
||||
Schema::dropIfExists('categories');
|
||||
|
||||
Schema::create('categories', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('id')->primary();
|
||||
$table->boolean('disablepreview')->default(false);
|
||||
});
|
||||
|
||||
Schema::create('releases', function (Blueprint $table): void {
|
||||
$table->unsignedInteger('id')->primary();
|
||||
$table->string('guid');
|
||||
$table->char('leftguid', 1);
|
||||
$table->integer('passwordstatus');
|
||||
$table->integer('haspreview');
|
||||
$table->integer('nzbstatus');
|
||||
$table->unsignedInteger('categories_id');
|
||||
$table->unsignedBigInteger('size');
|
||||
$table->dateTime('postdate')->nullable();
|
||||
$table->timestamp('additional_pp_claimed_at')->nullable();
|
||||
$table->string('additional_pp_claim_token', 64)->nullable();
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,13 @@
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Services\TempWorkspaceService;
|
||||
use Illuminate\Container\Container;
|
||||
use Illuminate\Filesystem\Filesystem;
|
||||
use Illuminate\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Facade;
|
||||
use Illuminate\Support\Facades\File;
|
||||
use PHPUnit\Framework\Attributes\WithoutErrorHandler;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use RuntimeException;
|
||||
|
||||
class TempWorkspaceServiceTest extends TestCase
|
||||
{
|
||||
@@ -20,9 +21,9 @@ class TempWorkspaceServiceTest extends TestCase
|
||||
{
|
||||
parent::setUp();
|
||||
// Minimal Facade container for File facade
|
||||
$container = new Container;
|
||||
$container->instance('files', new Filesystem);
|
||||
Facade::setFacadeApplication($container);
|
||||
$app = new Application(dirname(__DIR__, 2));
|
||||
$app->instance('files', new Filesystem);
|
||||
Facade::setFacadeApplication($app);
|
||||
|
||||
$this->svc = new TempWorkspaceService;
|
||||
// Unique base path under system temp
|
||||
@@ -56,6 +57,32 @@ class TempWorkspaceServiceTest extends TestCase
|
||||
$this->assertStringEndsWith('/group42/guid-123/', str_replace('\\', '/', $tmp));
|
||||
}
|
||||
|
||||
#[WithoutErrorHandler]
|
||||
public function test_ensure_main_temp_path_repairs_existing_unwritable_bucket_directory(): void
|
||||
{
|
||||
$bucket = $this->base.DIRECTORY_SEPARATOR.'a';
|
||||
File::makeDirectory($bucket, 0555, true, true);
|
||||
chmod($bucket, 0555);
|
||||
|
||||
$resolved = $this->svc->ensureMainTempPath($this->base, 'a', '');
|
||||
|
||||
$this->assertTrue(File::isDirectory($resolved));
|
||||
$this->assertTrue(is_writable($resolved));
|
||||
}
|
||||
|
||||
#[WithoutErrorHandler]
|
||||
public function test_ensure_main_temp_path_reports_path_when_base_cannot_be_created(): void
|
||||
{
|
||||
$fileBase = $this->base.DIRECTORY_SEPARATOR.'not-a-directory';
|
||||
File::put($fileBase, 'x');
|
||||
|
||||
$this->expectException(RuntimeException::class);
|
||||
$this->expectExceptionMessage('Additional post-processing temp path');
|
||||
$this->expectExceptionMessage('not-a-directory');
|
||||
|
||||
$this->svc->ensureMainTempPath($fileBase, 'a', '');
|
||||
}
|
||||
|
||||
#[WithoutErrorHandler]
|
||||
public function test_list_files_with_and_without_pattern(): void
|
||||
{
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Tests\Unit;
|
||||
|
||||
use App\Services\Tmux\TmuxMonitorService;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
use ReflectionProperty;
|
||||
|
||||
class TmuxMonitorServiceTest extends TestCase
|
||||
{
|
||||
public function test_calculate_statistics_backfills_missing_work_count_defaults(): void
|
||||
{
|
||||
$reflection = new ReflectionClass(TmuxMonitorService::class);
|
||||
/** @var TmuxMonitorService $monitor */
|
||||
$monitor = $reflection->newInstanceWithoutConstructor();
|
||||
|
||||
$runVar = [
|
||||
'counts' => [
|
||||
'now' => [
|
||||
'processnfo' => 2,
|
||||
'tv' => 1,
|
||||
],
|
||||
'start' => [],
|
||||
'diff' => [],
|
||||
'percent' => [],
|
||||
],
|
||||
];
|
||||
|
||||
$runVarProperty = new ReflectionProperty(TmuxMonitorService::class, 'runVar');
|
||||
$runVarProperty->setValue($monitor, $runVar);
|
||||
|
||||
$iterationsProperty = new ReflectionProperty(TmuxMonitorService::class, 'iterations');
|
||||
$iterationsProperty->setValue($monitor, 1);
|
||||
|
||||
$calculate = new ReflectionMethod(TmuxMonitorService::class, 'calculateStatistics');
|
||||
$calculate->invoke($monitor);
|
||||
|
||||
$updatedRunVar = $runVarProperty->getValue($monitor);
|
||||
|
||||
$this->assertSame(0, $updatedRunVar['counts']['now']['work']);
|
||||
$this->assertSame(0, $updatedRunVar['counts']['now']['work_available']);
|
||||
$this->assertSame('0', $updatedRunVar['counts']['diff']['work']);
|
||||
$this->assertSame('0', $updatedRunVar['counts']['diff']['work_available']);
|
||||
$this->assertSame(2, $updatedRunVar['counts']['now']['total_work']);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user