/** * @runInSeparateProcess * @preserveGlobalState disabled */ public function testPubSub() { $process = CM_Process::getInstance(); $process->fork(function () { $redisClient = CM_Service_Manager::getInstance()->getRedis(); return $redisClient->subscribe('foo', function ($channel, $message) { if ($message === 'test') { return null; } return [$channel, $message]; }); }); $clientCount = 0; // use a timeout because there's no easy way to know when the forked process will subscribe to the channel... for ($loopCount = 0; 0 === $clientCount; $loopCount++) { $clientCount = $this->_client->publish('foo', 'test'); if ($loopCount > 40) { $process->killChildren(); $this->fail('Failed to publish on a Redis subpub channel.'); } usleep(50 * 1000); } $this->_client->publish('foo', 'bar'); $resultList = $process->waitForChildren(); $this->assertCount(1, $resultList); foreach ($resultList as $result) { $this->assertSame(['foo', 'bar'], $result->getResult()); } }