*/ class LockServiceTest extends TestCase { public function testLock() { $service = $this->getInstance(); if ($this->isNoLockConfigured($service)) { $this->markTestSkipped('No lock storage configured for lock service. Skip integration test.'); } $actionId1 = 'action_1'; $actionId2 = 'action_2'; $sleep = 3; $time = time(); $pipe1 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' 0', 'w'); $pipe2 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' 0', 'w'); $pipe3 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' 0', 'w'); $pipe4 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId2 . ' ' . $sleep . ' 0', 'w'); pclose($pipe1); pclose($pipe2); pclose($pipe3); pclose($pipe4); $this->assertTrue((time() - $time) >= ($sleep * 3)); $this->assertTrue((time() - $time) < ($sleep * 4)); } public function testLockTimeout() { $service = $this->getInstance(); if ($this->isNoLockConfigured($service)) { $this->markTestSkipped('No lock storage configured for lock service. Skip integration test.'); } $actionId1 = 'action_1'; $sleep = 5; $timeout = 2; $time = time(); $pipe1 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' ' . $timeout, 'w'); $pipe2 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' ' . $timeout, 'w'); pclose($pipe1); pclose($pipe2); /** * Process A: * acquireLock timeout close * | 2 sec | 3 sec | * ------------------------------------ * * Process B: * acquireLock start close * | wait | 5 sec | * ------------------------------------ * * Total time: * | 2 + 5 = 7 seconds | * -------------------------------------------------------- */ $consumedTime = time() - $time; $this->assertTrue($consumedTime > $sleep); $this->assertTrue($consumedTime < ($sleep * 2)); } /** * @return LockService|\oat\oatbox\service\ConfigurableService */ public function getInstance() { return ServiceManager::getServiceManager()->get(LockService::class); } /** * @param $service * @return bool * @throws \ReflectionException */ private function isNoLockConfigured($service) { $reflectionClass = new \ReflectionClass($service->getOption($service::OPTION_PERSISTENCE_CLASS)); return $reflectionClass->getName() === NoLockStorage::class; } }