lock.test 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. <?php
  2. /**
  3. * Tests for the lock system.
  4. */
  5. class LockFunctionalTest extends DrupalWebTestCase {
  6. public static function getInfo() {
  7. return array(
  8. 'name' => 'Locking framework tests',
  9. 'description' => 'Confirm locking works between two separate requests.',
  10. 'group' => 'System',
  11. );
  12. }
  13. function setUp() {
  14. parent::setUp('system_test');
  15. }
  16. /**
  17. * Confirm that we can acquire and release locks in two parallel requests.
  18. */
  19. function testLockAcquire() {
  20. $lock_acquired = 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
  21. $lock_not_acquired = 'FALSE: Lock not acquired in system_test_lock_acquire()';
  22. $this->assertTrue(lock_acquire('system_test_lock_acquire'), t('Lock acquired by this request.'), t('Lock'));
  23. $this->assertTrue(lock_acquire('system_test_lock_acquire'), t('Lock extended by this request.'), t('Lock'));
  24. lock_release('system_test_lock_acquire');
  25. // Cause another request to acquire the lock.
  26. $this->drupalGet('system-test/lock-acquire');
  27. $this->assertText($lock_acquired, t('Lock acquired by the other request.'), t('Lock'));
  28. // The other request has finished, thus it should have released its lock.
  29. $this->assertTrue(lock_acquire('system_test_lock_acquire'), t('Lock acquired by this request.'), t('Lock'));
  30. // This request holds the lock, so the other request cannot acquire it.
  31. $this->drupalGet('system-test/lock-acquire');
  32. $this->assertText($lock_not_acquired, t('Lock not acquired by the other request.'), t('Lock'));
  33. lock_release('system_test_lock_acquire');
  34. // Try a very short timeout and lock breaking.
  35. $this->assertTrue(lock_acquire('system_test_lock_acquire', 0.5), t('Lock acquired by this request.'), t('Lock'));
  36. sleep(1);
  37. // The other request should break our lock.
  38. $this->drupalGet('system-test/lock-acquire');
  39. $this->assertText($lock_acquired, t('Lock acquired by the other request, breaking our lock.'), t('Lock'));
  40. // We cannot renew it, since the other thread took it.
  41. $this->assertFalse(lock_acquire('system_test_lock_acquire'), t('Lock cannot be extended by this request.'), t('Lock'));
  42. // Check the shut-down function.
  43. $lock_acquired_exit = 'TRUE: Lock successfully acquired in system_test_lock_exit()';
  44. $lock_not_acquired_exit = 'FALSE: Lock not acquired in system_test_lock_exit()';
  45. $this->drupalGet('system-test/lock-exit');
  46. $this->assertText($lock_acquired_exit, t('Lock acquired by the other request before exit.'), t('Lock'));
  47. $this->assertTrue(lock_acquire('system_test_lock_exit'), t('Lock acquired by this request after the other request exits.'), t('Lock'));
  48. }
  49. }