3 namespace Doctrine\Tests\ORM\Functional\Locking;
5 require_once __DIR__ . "/../../../TestInit.php";
11 static public function run()
13 $lockAgent = new LockAgentWorker();
15 $worker = new \GearmanWorker();
17 $worker->addFunction("findWithLock", array($lockAgent, "findWithLock"));
18 $worker->addFunction("dqlWithLock", array($lockAgent, "dqlWithLock"));
19 $worker->addFunction('lock', array($lockAgent, 'lock'));
21 while($worker->work()) {
22 if ($worker->returnCode() != GEARMAN_SUCCESS) {
23 echo "return_code: " . $worker->returnCode() . "\n";
29 protected function process($job, \Closure $do)
31 $fixture = $this->processWorkload($job);
34 $this->em->beginTransaction();
35 $do($fixture, $this->em);
38 $this->em->rollback();
41 $this->em->getConnection()->close();
43 return (microtime(true) - $s);
46 public function findWithLock($job)
48 return $this->process($job, function($fixture, $em) {
49 $entity = $em->find($fixture['entityName'], $fixture['entityId'], $fixture['lockMode']);
53 public function dqlWithLock($job)
55 return $this->process($job, function($fixture, $em) {
56 /* @var $query Doctrine\ORM\Query */
57 $query = $em->createQuery($fixture['dql']);
58 $query->setLockMode($fixture['lockMode']);
59 $query->setParameters($fixture['dqlParams']);
60 $result = $query->getResult();
64 public function lock($job)
66 return $this->process($job, function($fixture, $em) {
67 $entity = $em->find($fixture['entityName'], $fixture['entityId']);
68 $em->lock($entity, $fixture['lockMode']);
72 protected function processWorkload($job)
74 echo "Received job: " . $job->handle() . " for function " . $job->functionName() . "\n";
76 $workload = $job->workload();
77 $workload = unserialize($workload);
79 if (!isset($workload['conn']) || !is_array($workload['conn'])) {
80 throw new \InvalidArgumentException("Missing Database parameters");
83 $this->em = $this->createEntityManager($workload['conn']);
85 if (!isset($workload['fixture'])) {
86 throw new \InvalidArgumentException("Missing Fixture parameters");
88 return $workload['fixture'];
91 protected function createEntityManager($conn)
93 $config = new \Doctrine\ORM\Configuration();
94 $config->setProxyDir(__DIR__ . '/../../../Proxies');
95 $config->setProxyNamespace('MyProject\Proxies');
96 $config->setAutoGenerateProxyClasses(true);
98 $annotDriver = $config->newDefaultAnnotationDriver(array(__DIR__ . '/../../../Models/'), true);
99 $config->setMetadataDriverImpl($annotDriver);
101 $cache = new \Doctrine\Common\Cache\ArrayCache();
102 $config->setMetadataCacheImpl($cache);
103 $config->setQueryCacheImpl($cache);
104 $config->setSQLLogger(new \Doctrine\DBAL\Logging\EchoSQLLogger());
106 $em = \Doctrine\ORM\EntityManager::create($conn, $config);
112 LockAgentWorker::run();