Projet

Général

Profil

Paste
Télécharger (2,56 ko) Statistiques
| Branche: | Révision:

root / drupal7 / modules / simpletest / tests / lock.test @ db2d93dd

1
<?php
2

    
3
/**
4
 * Tests for the lock system.
5
 */
6
class LockFunctionalTest extends DrupalWebTestCase {
7

    
8
  public static function getInfo() {
9
    return array(
10
      'name' => 'Locking framework tests',
11
      'description' => 'Confirm locking works between two separate requests.',
12
      'group' => 'System',
13
    );
14
  }
15

    
16
  function setUp() {
17
    parent::setUp('system_test');
18
  }
19

    
20
  /**
21
   * Confirm that we can acquire and release locks in two parallel requests.
22
   */
23
  function testLockAcquire() {
24
    $lock_acquired = 'TRUE: Lock successfully acquired in system_test_lock_acquire()';
25
    $lock_not_acquired = 'FALSE: Lock not acquired in system_test_lock_acquire()';
26
    $this->assertTrue(lock_acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock');
27
    $this->assertTrue(lock_acquire('system_test_lock_acquire'), 'Lock extended by this request.', 'Lock');
28
    lock_release('system_test_lock_acquire');
29

    
30
    // Cause another request to acquire the lock.
31
    $this->drupalGet('system-test/lock-acquire');
32
    $this->assertText($lock_acquired, 'Lock acquired by the other request.', 'Lock');
33
    // The other request has finished, thus it should have released its lock.
34
    $this->assertTrue(lock_acquire('system_test_lock_acquire'), 'Lock acquired by this request.', 'Lock');
35
    // This request holds the lock, so the other request cannot acquire it.
36
    $this->drupalGet('system-test/lock-acquire');
37
    $this->assertText($lock_not_acquired, 'Lock not acquired by the other request.', 'Lock');
38
    lock_release('system_test_lock_acquire');
39

    
40
    // Try a very short timeout and lock breaking.
41
    $this->assertTrue(lock_acquire('system_test_lock_acquire', 0.5), 'Lock acquired by this request.', 'Lock');
42
    sleep(1);
43
    // The other request should break our lock.
44
    $this->drupalGet('system-test/lock-acquire');
45
    $this->assertText($lock_acquired, 'Lock acquired by the other request, breaking our lock.', 'Lock');
46
    // We cannot renew it, since the other thread took it.
47
    $this->assertFalse(lock_acquire('system_test_lock_acquire'), 'Lock cannot be extended by this request.', 'Lock');
48

    
49
    // Check the shut-down function.
50
    $lock_acquired_exit = 'TRUE: Lock successfully acquired in system_test_lock_exit()';
51
    $lock_not_acquired_exit = 'FALSE: Lock not acquired in system_test_lock_exit()';
52
    $this->drupalGet('system-test/lock-exit');
53
    $this->assertText($lock_acquired_exit, 'Lock acquired by the other request before exit.', 'Lock');
54
    $this->assertTrue(lock_acquire('system_test_lock_exit'), 'Lock acquired by this request after the other request exits.', 'Lock');
55
  }
56
}
57