Best Atoum code snippet using builder.lock
Repository.php
Source:Repository.php
...123 throw $e;124 }125 }126 /**127 * Acquires a lock to an entity, provides the entity to a callback function128 * and relinquishes the lock by flushing the entity manager immediately129 * after.130 *131 * @param int $id132 * @param int $lockMode133 * @param callback($entity, Doctrine\ORM\EntityManager, Repository) $callback134 * @return mixed callback return type135 * @throws LockException136 */137 public function useWithLock($id, $lockMode, $callback)138 {139 $entityName = $this->getEntityName();140 return $this->transaction(function ($em, $self) use ($id, $lockMode, $callback, $entityName) {141 $entity = $self->find($id, $lockMode);142 if (empty($entity)) {143 $message = \sprintf("Could not lock %s entity by id %d: entity not found", $entityName, $id);144 throw new LockException($message);145 }146 $result = $callback($entity, $em, $self);147 return $result;148 });149 }150 /**151 * Calls useWithLock() with a pessimistic write lock mode152 *153 * @param int $id154 * @param callback($entity, Doctrine\ORM\EntityManager, Repository) $callback155 * @return mixed callback return type156 */157 public function useWithPessimisticWriteLock($id, $callback)158 {159 return $this->useWithLock($id, LockMode::PESSIMISTIC_WRITE, $callback);160 }161 /**162 * Acquires an optimistic lock within a pessimistic lock transaction. For163 * use in fail-fast scenarios; guaranteed to throw an exception on164 * concurrent modification attempts. The one to first acquire the write lock165 * will update the version field, leading subsequent acquisitions of the166 * optimistic lock to fail.167 *168 * FIXME: Only works on entities implementing VersionLockable and does not169 * work in conjunction with the Doctrine @Version column.170 *171 * @param int $id172 * @param mixed $lockVersion173 * @param callback($entity, Doctrine\ORM\EntityManager, Repository) $callback174 * @return mixed callback return type175 * @throws OptimisticLockException176 */177 public function useWithPessimisticVersionLock($id, $lockVersion, $callback)178 {179 return $this->useWithPessimisticWriteLock($id, function (VersionLockable $entity, EntityManager $em, $self) use ($lockVersion, $callback) {180 if ($entity->getVersion() !== $lockVersion) {181 // FIXME: This isn't the appropriate exception type.182 throw OptimisticLockException::lockFailedVersionMissmatch($entity, $lockVersion, $entity->getVersion());183 }184 $entity->incrementVersion();185 return $callback($entity, $em, $self);186 });187 }188 /**189 * @return string190 */191 public function getEntityName()192 {193 return parent::getEntityName();194 }195 /**196 * Attempt to acquire a table level lock in MySQL for the duration of the197 * given transaction. IS NOT IN ANY WAY GUARANTEED TO WORK.198 *199 * @see TableLock200 * @param int $lockMode a TableLockMode constant201 * @param callback $transaction202 * @return mixed203 * @throws LockException204 */205 public function transactionWithTableLock($lockMode, $transaction)206 {207 return $this->getTableLock()->transaction($lockMode, $transaction);208 }209 /**210 * @return TableLock211 */212 private function getTableLock()213 {214 return new TableLock($this);215 }216}...
LockTest.php
Source:LockTest.php
...18 * @author Jérémy Derussé <jeremy@derusse.com>19 */20class LockTest extends TestCase21{22 public function testAcquireNoBlocking()23 {24 $key = new Key(uniqid(__METHOD__, true));25 $store = $this->getMockBuilder(StoreInterface::class)->getMock();26 $lock = new Lock($key, $store);27 $store28 ->expects($this->once())29 ->method('save');30 $this->assertTrue($lock->acquire(false));31 }32 public function testAcquireReturnsFalse()33 {34 $key = new Key(uniqid(__METHOD__, true));35 $store = $this->getMockBuilder(StoreInterface::class)->getMock();36 $lock = new Lock($key, $store);37 $store38 ->expects($this->once())39 ->method('save')40 ->willThrowException(new LockConflictedException());41 $this->assertFalse($lock->acquire(false));42 }43 public function testAcquireBlocking()44 {45 $key = new Key(uniqid(__METHOD__, true));46 $store = $this->getMockBuilder(StoreInterface::class)->getMock();47 $lock = new Lock($key, $store);48 $store49 ->expects($this->never())50 ->method('save');51 $store52 ->expects($this->once())53 ->method('waitAndSave');54 $this->assertTrue($lock->acquire(true));55 }56 public function testAcquireSetsTtl()57 {58 $key = new Key(uniqid(__METHOD__, true));59 $store = $this->getMockBuilder(StoreInterface::class)->getMock();60 $lock = new Lock($key, $store, 10);61 $store62 ->expects($this->once())63 ->method('save');64 $store65 ->expects($this->once())66 ->method('putOffExpiration')67 ->with($key, 10);68 $lock->acquire();69 }70 public function testRefresh()71 {72 $key = new Key(uniqid(__METHOD__, true));73 $store = $this->getMockBuilder(StoreInterface::class)->getMock();74 $lock = new Lock($key, $store, 10);75 $store76 ->expects($this->once())77 ->method('putOffExpiration')78 ->with($key, 10);79 $lock->refresh();80 }81 public function testIsAquired()82 {83 $key = new Key(uniqid(__METHOD__, true));84 $store = $this->getMockBuilder(StoreInterface::class)->getMock();85 $lock = new Lock($key, $store, 10);86 $store87 ->expects($this->any())88 ->method('exists')89 ->with($key)90 ->will($this->onConsecutiveCalls(true, false));91 $this->assertTrue($lock->isAcquired());92 }93 public function testRelease()94 {95 $key = new Key(uniqid(__METHOD__, true));96 $store = $this->getMockBuilder(StoreInterface::class)->getMock();97 $lock = new Lock($key, $store, 10);98 $store99 ->expects($this->once())100 ->method('delete')101 ->with($key);102 $store103 ->expects($this->once())104 ->method('exists')105 ->with($key)106 ->willReturn(false);107 $lock->release();108 }109 public function testReleaseOnDestruction()110 {111 $key = new Key(uniqid(__METHOD__, true));112 $store = $this->getMockBuilder(StoreInterface::class)->getMock();113 $lock = new Lock($key, $store, 10);114 $store115 ->method('exists')116 ->willReturnOnConsecutiveCalls([true, false])117 ;118 $store119 ->expects($this->once())120 ->method('delete')121 ;122 $lock->acquire(false);123 unset($lock);124 }125 public function testNoAutoReleaseWhenNotConfigured()126 {127 $key = new Key(uniqid(__METHOD__, true));128 $store = $this->getMockBuilder(StoreInterface::class)->getMock();129 $lock = new Lock($key, $store, 10, false);130 $store131 ->method('exists')132 ->willReturnOnConsecutiveCalls([true, false])133 ;134 $store135 ->expects($this->never())136 ->method('delete')137 ;138 $lock->acquire(false);139 unset($lock);140 }141 /**142 * @expectedException \Symfony\Component\Lock\Exception\LockReleasingException143 */144 public function testReleaseThrowsExceptionIfNotWellDeleted()145 {146 $key = new Key(uniqid(__METHOD__, true));147 $store = $this->getMockBuilder(StoreInterface::class)->getMock();148 $lock = new Lock($key, $store, 10);149 $store150 ->expects($this->once())151 ->method('delete')152 ->with($key);153 $store154 ->expects($this->once())155 ->method('exists')156 ->with($key)157 ->willReturn(true);158 $lock->release();159 }160 /**161 * @expectedException \Symfony\Component\Lock\Exception\LockReleasingException162 */163 public function testReleaseThrowsAndLog()164 {165 $key = new Key(uniqid(__METHOD__, true));166 $store = $this->getMockBuilder(StoreInterface::class)->getMock();167 $logger = $this->getMockBuilder(LoggerInterface::class)->getMock();168 $lock = new Lock($key, $store, 10, true);169 $lock->setLogger($logger);170 $logger->expects($this->atLeastOnce())171 ->method('notice')172 ->with('Failed to release the "{resource}" lock.', ['resource' => $key]);173 $store174 ->expects($this->once())175 ->method('delete')176 ->with($key);177 $store178 ->expects($this->once())179 ->method('exists')180 ->with($key)181 ->willReturn(true);182 $lock->release();183 }184 /**185 * @dataProvider provideExpiredDates186 */187 public function testExpiration($ttls, $expected)188 {189 $key = new Key(uniqid(__METHOD__, true));190 $store = $this->getMockBuilder(StoreInterface::class)->getMock();191 $lock = new Lock($key, $store, 10);192 foreach ($ttls as $ttl) {193 if (null === $ttl) {194 $key->resetLifetime();195 } else {196 $key->reduceLifetime($ttl);197 }198 }199 $this->assertSame($expected, $lock->isExpired());200 }201 public function provideExpiredDates()202 {203 yield [[-0.1], true];204 yield [[0.1, -0.1], true];205 yield [[-0.1, 0.1], true];206 yield [[], false];207 yield [[0.1], false];208 yield [[-0.1, null], false];209 }210}...
RouterRebuildSubscriber.php
Source:RouterRebuildSubscriber.php
...22 protected $routeBuilder;23 /**24 * @var \Drupal\Core\Lock\LockBackendInterface25 */26 protected $lock;27 /**28 * Constructs the RouterRebuildSubscriber object.29 *30 * @param \Drupal\Core\Routing\RouteBuilderInterface $route_builder31 * The route builder.32 * @param \Drupal\Core\Lock\LockBackendInterface $lock33 * The lock backend.34 */35 public function __construct(RouteBuilderInterface $route_builder, LockBackendInterface $lock) {36 $this->routeBuilder = $route_builder;37 $this->lock = $lock;38 }39 /**40 * Rebuilds routers if necessary.41 *42 * @param \Symfony\Component\HttpKernel\Event\PostResponseEvent $event43 * The event object.44 */45 public function onKernelTerminate(PostResponseEvent $event) {46 $this->routeBuilder->rebuildIfNeeded();47 }48 /**49 * Rebuilds the menu links and deletes the local_task cache tag.50 *51 * @param \Symfony\Component\EventDispatcher\Event $event52 * The event object.53 */54 public function onRouterRebuild(Event $event) {55 $this->menuLinksRebuild();56 Cache::deleteTags(array('local_task' => 1));57 }58 /**59 * Perform menu-specific rebuilding.60 */61 protected function menuLinksRebuild() {62 if ($this->lock->acquire(__FUNCTION__)) {63 $transaction = db_transaction();64 try {65 // Ensure the menu links are up to date.66 menu_link_rebuild_defaults();67 // Clear the menu, page and block caches.68 menu_cache_clear_all();69 _menu_clear_page_cache();70 }71 catch (\Exception $e) {72 $transaction->rollback();73 watchdog_exception('menu', $e);74 }75 $this->lock->release(__FUNCTION__);76 }77 else {78 // Wait for another request that is already doing this work.79 // We choose to block here since otherwise the router item may not80 // be available during routing resulting in a 404.81 $this->lock->wait(__FUNCTION__);82 }83 }84 /**85 * Registers the methods in this class that should be listeners.86 *87 * @return array88 * An array of event listener definitions.89 */90 static function getSubscribedEvents() {91 $events[KernelEvents::TERMINATE][] = array('onKernelTerminate', 200);92 $events[RoutingEvents::FINISHED][] = array('onRouterRebuild', 200);93 return $events;94 }95}...
lock
Using AI Code Generation
1$builder = new Builder();2$builder->lock();3$builder = new Builder();4$builder->lock();5$builder = new Builder();6$builder->lock();7$builder = new Builder();8$builder->lock();9$builder = new Builder();10$builder->lock();11$builder = new Builder();12$builder->lock();13$builder = new Builder();14$builder->lock();15$builder = new Builder();16$builder->lock();17$builder = new Builder();18$builder->lock();19$builder = new Builder();20$builder->lock();21Fatal error: Uncaught exception 'Exception' with message 'Another process is using the builder' in /path/to/Builder.php:53 Stack trace: #0 /path/to/1.php(4): Builder->lock() #1 {main} thrown in /path/to/Builder.php on line 53
lock
Using AI Code Generation
1$builder = new Builder();2$builder->lock();3$builder->unlock();4$builder = new Builder();5$builder->lock();6$builder->unlock();7{8 public static function lock()9 {10 }11 public static function unlock()12 {13 }14}15Builder::lock();16Builder::unlock();17Builder::lock();18Builder::unlock();19Builder::lock();20Builder::unlock();21Builder::lock();22Builder::unlock();23Builder::lock();24Builder::unlock();25{26 public static function lock()27 {28 }29 public static function unlock()30 {
lock
Using AI Code Generation
1$builder = new Builder();2$builder->lock();3$builder->build();4$builder->unlock();5$builder = new Builder();6$builder->lock();7$builder->build();8$builder->unlock();9$builder = new Builder();10$builder->lock();11$builder->build();12$builder->unlock();13$builder = new Builder();14$builder->lock();15$builder->build();16$builder->unlock();17$builder = new Builder();18$builder->lock();19$builder->build();20$builder->unlock();21$builder = new Builder();22$builder->lock();23$builder->build();24$builder->unlock();25$builder = new Builder();26$builder->lock();27$builder->build();28$builder->unlock();29$builder = new Builder();30$builder->lock();31$builder->build();32$builder->unlock();33$builder = new Builder();34$builder->lock();35$builder->build();36$builder->unlock();37$builder = new Builder();38$builder->lock();39$builder->build();40$builder->unlock();41$builder = new Builder();42$builder->lock();43$builder->build();44$builder->unlock();45$builder = new Builder();46$builder->lock();47$builder->build();48$builder->unlock();49$builder = new Builder();50$builder->lock();51$builder->build();52$builder->unlock();
lock
Using AI Code Generation
1$builder = new Builder();2$builder->lock();3$builder->set();4$builder->get();5$builder->unlock();6$builder = new Builder();7$builder->lock();8$builder->set();9$builder->get();10$builder->unlock();11$builder = new Builder();12$builder->lock();13$builder->set();14$builder->get();15$builder->unlock();16$builder = new Builder();17$builder->lock();18$builder->set();19$builder->get();20$builder->unlock();21$builder = new Builder();22$builder->lock();23$builder->set();24$builder->get();25$builder->unlock();26$builder = new Builder();27$builder->lock();28$builder->set();29$builder->get();30$builder->unlock();31$builder = new Builder();32$builder->lock();33$builder->set();34$builder->get();35$builder->unlock();36$builder = new Builder();37$builder->lock();38$builder->set();39$builder->get();40$builder->unlock();41$builder = new Builder();42$builder->lock();43$builder->set();44$builder->get();45$builder->unlock();46$builder = new Builder();47$builder->lock();48$builder->set();49$builder->get();50$builder->unlock();51$builder = new Builder();52$builder->lock();53$builder->set();54$builder->get();55$builder->unlock();56$builder = new Builder();57$builder->lock();58$builder->set();59$builder->get();
lock
Using AI Code Generation
1$builder = new Builder();2$builder->lock();3$builder = new Builder();4$builder->lock();5{6 public static function lock()7 {8 echo 'Lock';9 }10}11Builder::lock();12Builder::lock();13Builder::lock();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Execute automation tests with lock on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.
Test now for FreeGet 100 minutes of automation test minutes FREE!!