You are here

public function MemcacheLockFunctionalTest::testLockAcquire in Memcache API and Integration 7

Confirm that we can acquire and release locks in two parallel requests.

File

tests/memcache-lock.test, line 29
Tests for the lock system.

Class

MemcacheLockFunctionalTest
@file Tests for the lock system.

Code

public function testLockAcquire() {

  // Confirm that locks are really acquired in memcache.
  lock_acquire($this->default_cid);
  $this
    ->assertTrue(dmemcache_get($this->default_cid, 'semaphore'), t('Memcache locking is configured correctly.'));
  lock_release($this->default_cid);

  // Nasty hack. There are two processes involved here - the process
  // calling the test itself, that calls lock_acquire() directly. And the
  // 'child' process that is requested over http by drupalGet().
  // In dmemcache.inc, we look for simpletest installs, to force a
  // simpletest prefix as part of the memcache key - this avoids the
  // actual Drupal site install being corrupted by entries from the tested
  // site. However, the child install apparently does not set the simpletest
  // global, so when making requests to that site, we pass the simpletest ID
  // as part of the URL, and mess around with the globals on that side.
  $test_run_id = $GLOBALS['drupal_test_info']['test_run_id'];
  $lock_acquired = 'TRUE: Lock successfully acquired in memcache_test_lock_acquire()';
  $lock_not_acquired = 'FALSE: Lock not acquired in memcache_test_lock_acquire()';
  $this
    ->assertTrue(lock_acquire('memcache_test_lock_acquire'), t('Lock acquired by this request.'), t('Lock'));
  $this
    ->assertTrue(lock_acquire('memcache_test_lock_acquire'), t('Lock extended by this request.'), t('Lock'));
  lock_release('memcache_test_lock_acquire');

  // Cause another request to acquire the lock.
  $this
    ->drupalGet('memcache-test/lock-acquire' . "/{$test_run_id}");
  $this
    ->assertText($lock_acquired, t('Lock acquired by the other request.'), t('Lock'));

  // The other request has finished, thus it should have released its lock.
  $this
    ->assertTrue(lock_acquire('memcache_test_lock_acquire'), t('Lock acquired by this request.'), t('Lock'));

  // This request holds the lock, so the other request cannot acquire it.
  $this
    ->drupalGet('memcache-test/lock-acquire' . "/{$test_run_id}");
  $this
    ->assertText($lock_not_acquired, t('Lock not acquired by the other request.'), t('Lock'));
  lock_release('memcache_test_lock_acquire');

  // Try a very short timeout and lock breaking.
  $this
    ->assertTrue(lock_acquire('memcache_test_lock_acquire', 1), t('Lock acquired by this request.'), t('Lock'));
  sleep(2);

  // The other request should break our lock.
  $this
    ->drupalGet('memcache-test/lock-acquire' . "/{$test_run_id}");
  $this
    ->assertText($lock_acquired, t('Lock acquired by the other request, breaking our lock.'), t('Lock'));

  // We cannot renew it, since the other thread took it.
  // @todo: this assertion currently fails - the lock_acquire() call returns
  // true. For now, commented out the assertion, uncomment when attempting to
  // fix.
  // $this->assertFalse(lock_acquire('memcache_test_lock_acquire'), t('Lock cannot be extended by this request.'), t('Lock'));
  // Check the shut-down function.
  $lock_acquired_exit = 'TRUE: Lock successfully acquired in memcache_test_lock_exit()';
  $lock_not_acquired_exit = 'FALSE: Lock not acquired in memcache_test_lock_exit()';
  $this
    ->drupalGet('memcache-test/lock-exit' . "/{$test_run_id}");
  $this
    ->assertText($lock_acquired_exit, t('Lock acquired by the other request before exit.'), t('Lock'));
  $this
    ->assertTrue(lock_acquire('memcache_test_lock_exit'), t('Lock acquired by this request after the other request exits.'), t('Lock'));
}