the pids of the programs started */ private array $pids = []; protected function setUp(): void { if (PHP_OS_FAMILY === 'Windows' || !is_dir('/proc/self')) { self::markTestSkipped('/proc and sleep needed'); } $this->dir = sys_get_temp_dir() . '/wz-stop-' . bin2hex(random_bytes(4)); @mkdir($this->dir . '/bin', 0777, true); file_put_contents($this->dir . '/bin/ffmpeg', "#!/bin/sh\n"); chmod($this->dir . '/bin/ffmpeg', 0755); file_put_contents($this->dir . '/song.mp3', 'x'); } protected function tearDown(): void { foreach ($this->pids as $pid) { if (is_dir("/proc/$pid")) { @posix_kill($pid, 9); } } Tools::remove($this->dir); } /** a harmless long program in place of ffmpeg / whisper */ private function sleeper(): Process { $p = Process::start(['sleep', '30'], $this->dir . '/logs'); $pid = $p->pid(); self::assertGreaterThan(0, $pid); $this->pids[] = $pid; return $p; } private function tools(): Tools { $tools = new Tools($this->dir . '/bin', $this->dir . '/tmp'); $tools->look(); self::assertNotNull($tools->program('ffmpeg')); return $tools; } private function assertAllEnded(string $what): void { self::assertNotEmpty($this->pids, "$what: something was started"); foreach ($this->pids as $pid) { $state = is_file("/proc/$pid/stat") ? (string)@file_get_contents("/proc/$pid/stat") : ''; self::assertFalse($state !== '' && !preg_match('/^\d+ \(sleep\) Z/', $state) && str_contains($state, '(sleep)'), "$what: the program $pid still runs"); self::assertSame('', $state, "$what: the program $pid is reaped (no zombie)"); } } private static function set(object $o, string $property, mixed $value): void { $r = new \ReflectionProperty($o, $property); $r->setValue($o, $value); } public function testAProcessStoppedEndsAtOnceAndAgainIsNothing(): void { $p = $this->sleeper(); $start = microtime(true); $p->stop(); self::assertLessThan(2.0, microtime(true) - $start); self::assertTrue($p->done()); self::assertFalse($p->ok()); $p->stop(); $this->assertAllEnded('Process'); } public function testTheSoundCacheEndsItsFfmpeg(): void { $cache = new SoundCache($this->dir . '/sounds', $this->tools(), fn (array $cmd): Process => $this->sleeper()); self::assertNull($cache->plain($this->dir . '/song.mp3')); self::assertTrue($cache->busy()); $cache->stop(); self::assertFalse($cache->busy()); $cache->stop(); $this->assertAllEnded('SoundCache'); } public function testTheVideoCacheEndsTheFfmpegOfEachFilm(): void { $cache = new VideoCache($this->dir . '/video', $this->tools(), fn (array $cmd): Process => $this->sleeper()); $cache->audio($this->dir . '/song.mp3'); self::assertTrue($cache->making($this->dir . '/song.mp3')); self::assertCount(2, $this->pids, 'its sound and its pictures'); $cache->stop(); self::assertFalse($cache->making($this->dir . '/song.mp3')); $cache->stop(); $this->assertAllEnded('VideoCache'); } public function testTheTranscriberEndsWhisper(): void { $t = new Transcriber($this->tools(), $this->dir, $this->dir . '/lyr', fn (array $cmd): Process => $this->sleeper(), false); self::assertTrue($t->start($this->dir . '/song.mp3')); self::assertTrue($t->busy()); $t->stop(); self::assertFalse($t->busy()); $t->stop(); $this->assertAllEnded('Transcriber'); } public function testTheToolsEndTheirUpdateOfYtdlp(): void { $tools = $this->tools(); self::set($tools, 'job', $this->sleeper()); $tools->stop(); $tools->stop(); $this->assertAllEnded('Tools'); } public function testTheVolumeOfTheComputerEndsItsCommand(): void { $volume = new SystemVolume($this->dir . '/logs'); self::set($volume, 'job', $this->sleeper()); $volume->set(40); $volume->stop(); $volume->poll(); self::assertNull((new \ReflectionProperty($volume, 'job'))->getValue($volume), 'the value not written is forgotten'); $volume->stop(); $this->assertAllEnded('SystemVolume'); } public function testTheWatcherOfTheWindowInFrontEnds(): void { $fg = new Foreground($this->dir . '/logs'); (new \ReflectionProperty($fg, 'process'))->getValue($fg)?->stop(); self::set($fg, 'process', $this->sleeper()); $fg->stop(); self::assertFalse($fg->works()); $fg->stop(); $this->assertAllEnded('Foreground'); } }