|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Tests\Unit\Config; |
| 6 | + |
| 7 | +use Illuminate\Support\Facades\Artisan; |
| 8 | +use Illuminate\Support\Facades\Config; |
| 9 | +use Illuminate\Support\Facades\DB; |
| 10 | +use ProcessMaker\Jobs\TestStatusJob; |
| 11 | +use Tests\TestCase; |
| 12 | + |
| 13 | +class HorizonConfigTest extends TestCase |
| 14 | +{ |
| 15 | + private const ENVIRONMENTS = ['production', 'local', 'staging']; |
| 16 | + |
| 17 | + private const SUPERVISORS = ['supervisor-bpmn', 'supervisor-1']; |
| 18 | + |
| 19 | + protected function tearDown(): void |
| 20 | + { |
| 21 | + $this->unsetMaxJobsEnvironmentVariables(); |
| 22 | + |
| 23 | + parent::tearDown(); |
| 24 | + } |
| 25 | + |
| 26 | + public function testMaxJobsUsesRecommendedDefaultsForAllSupervisors(): void |
| 27 | + { |
| 28 | + $this->unsetMaxJobsEnvironmentVariables(); |
| 29 | + |
| 30 | + $configuration = include base_path('config/horizon.php'); // NOSONAR: load fresh config after changing environment variables. |
| 31 | + |
| 32 | + foreach (self::ENVIRONMENTS as $environment) { |
| 33 | + $this->assertSame(10, $configuration['environments'][$environment]['supervisor-bpmn']['maxJobs']); |
| 34 | + $this->assertSame(100, $configuration['environments'][$environment]['supervisor-1']['maxJobs']); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + public function testMaxJobsCanBeDisabledWithZero(): void |
| 39 | + { |
| 40 | + putenv('PM4_HORIZON_SUPERVISOR_BPMN_MAX_JOBS=0'); |
| 41 | + putenv('PM4_HORIZON_SUPERVISOR_1_MAX_JOBS=0'); |
| 42 | + |
| 43 | + $configuration = include base_path('config/horizon.php'); // NOSONAR: load fresh config after changing environment variables. |
| 44 | + |
| 45 | + foreach (self::ENVIRONMENTS as $environment) { |
| 46 | + foreach (self::SUPERVISORS as $supervisor) { |
| 47 | + $this->assertSame( |
| 48 | + 0, |
| 49 | + $configuration['environments'][$environment][$supervisor]['maxJobs'], |
| 50 | + "{$environment}.{$supervisor} should allow maxJobs to be disabled" |
| 51 | + ); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public function testMaxJobsCanBeConfiguredAsTwoForBpmnAndDefaultSupervisors(): void |
| 57 | + { |
| 58 | + putenv('PM4_HORIZON_SUPERVISOR_BPMN_MAX_JOBS=2'); |
| 59 | + putenv('PM4_HORIZON_SUPERVISOR_1_MAX_JOBS=2'); |
| 60 | + |
| 61 | + $configuration = include base_path('config/horizon.php'); // NOSONAR: load fresh config after changing environment variables. |
| 62 | + |
| 63 | + foreach (self::ENVIRONMENTS as $environment) { |
| 64 | + foreach (self::SUPERVISORS as $supervisor) { |
| 65 | + $this->assertSame( |
| 66 | + 2, |
| 67 | + $configuration['environments'][$environment][$supervisor]['maxJobs'], |
| 68 | + "{$environment}.{$supervisor} should use the configured maxJobs value" |
| 69 | + ); |
| 70 | + } |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + public function testFiveJobsContinueProcessingAcrossWorkersWithMaxJobsTwo(): void |
| 75 | + { |
| 76 | + $queue = 'maxjobs-test-' . bin2hex(random_bytes(4)); |
| 77 | + $jobPrefix = 'maxjobs-test-' . bin2hex(random_bytes(4)); |
| 78 | + $maxJobs = 2; |
| 79 | + |
| 80 | + Config::set('queue.default', 'redis'); |
| 81 | + Config::set('horizon.environments.local.supervisor-bpmn.maxJobs', $maxJobs); |
| 82 | + |
| 83 | + foreach (range(1, 5) as $number) { |
| 84 | + TestStatusJob::dispatch( |
| 85 | + "{$jobPrefix}-{$number}", |
| 86 | + 'Horizon maxJobs integration test' |
| 87 | + )->onQueue($queue); |
| 88 | + } |
| 89 | + |
| 90 | + foreach ([2, 4, 5] as $expectedCount) { |
| 91 | + $exitCode = Artisan::call('queue:work', [ |
| 92 | + 'connection' => 'redis', |
| 93 | + '--queue' => $queue, |
| 94 | + '--max-jobs' => $maxJobs, |
| 95 | + '--stop-when-empty' => true, |
| 96 | + '--tries' => 1, |
| 97 | + '--memory' => 1024, |
| 98 | + ]); |
| 99 | + |
| 100 | + $this->assertSame(0, $exitCode); |
| 101 | + $this->assertSame( |
| 102 | + $expectedCount, |
| 103 | + DB::table('test_status') |
| 104 | + ->where('name', 'like', "{$jobPrefix}-%") |
| 105 | + ->count() |
| 106 | + ); |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + private function unsetMaxJobsEnvironmentVariables(): void |
| 111 | + { |
| 112 | + putenv('PM4_HORIZON_SUPERVISOR_BPMN_MAX_JOBS'); |
| 113 | + putenv('PM4_HORIZON_SUPERVISOR_1_MAX_JOBS'); |
| 114 | + } |
| 115 | +} |
0 commit comments