*/ class LockServiceTest extends TestCase { public function testLock() { $dir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . "generis_unittest_" . mt_rand() . DIRECTORY_SEPARATOR; mkdir($dir); $actionId1 = 'action_1'; $actionId2 = 'action_2'; $sleep = 3; $this->getInstance(FlockStore::class, $dir); $time = time(); $pipe1 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' FlockStore ' . $dir, 'w'); $pipe2 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' FlockStore ' . $dir, 'w'); $pipe3 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' FlockStore ' . $dir, 'w'); $pipe4 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId2 . ' ' . $sleep . ' FlockStore ' . $dir, 'w'); pclose($pipe1); pclose($pipe2); pclose($pipe3); pclose($pipe4); $consumedTime = (time() - $time); $this->assertTrue($consumedTime >= ($sleep * 3)); $this->assertTrue($consumedTime < ($sleep * 4)); } public function testNoLock() { $actionId1 = 'action_1'; $sleep = 3; $this->getInstance(NoLockStorage::class); $time = time(); $pipe1 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' NoLockStorage', 'w'); $pipe2 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' NoLockStorage', 'w'); $pipe3 = popen('php ' . __DIR__ . DIRECTORY_SEPARATOR . 'test_action.php ' . $actionId1 . ' ' . $sleep . ' NoLockStorage', 'w'); pclose($pipe1); pclose($pipe2); pclose($pipe3); $consumedTime = (time() - $time); $this->assertTrue($consumedTime >= $sleep); $this->assertTrue($consumedTime < ($sleep * 3)); } /** * @return LockService * @throws \common_Exception * @throws \common_exception_NotImplemented */ public function getInstance($class, $dir = null) { $config = new \common_persistence_KeyValuePersistence([], new \common_persistence_InMemoryKvDriver()); $config->set(\common_persistence_Manager::SERVICE_ID, new \common_persistence_Manager()); $serviceManager = new ServiceManager($config); $service = new LockService([ LockService::OPTION_PERSISTENCE_CLASS => $class, LockService::OPTION_PERSISTENCE_OPTIONS => $dir ]); $service->setServiceLocator($serviceManager); $service->install(); return $service; } }