PessimisticLockingTestCase.php 2.07 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
<?PHP

require_once("UnitTestCase.php");


class Doctrine_PessimisticLockingTestCase extends Doctrine_UnitTestCase
{
    private $lockingManager;
    
    /**
     * Sets up everything for the lock testing
     *
     * Creates a locking manager and a test record to work with.
     */
    public function setUp()
    {
        parent::setUp();
zYne's avatar
zYne committed
18
        $this->lockingManager = new Doctrine_Locking_Manager_Pessimistic($this->connection);
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
        
        // Create sample data to test on
        $entry1 = new Forum_Entry();
        $entry1->author = 'Bart Simpson';
        $entry1->topic  = 'I love donuts!';
        $entry1->save();
    }
    
    /**
     * Tests the basic locking mechanism
     * 
     * Currently tested: successful lock, failed lock, release lock 
     */
    public function testLock()
    {
zYne's avatar
zYne committed
34
        $entries = $this->connection->query("FROM Forum_Entry WHERE Forum_Entry.author = 'Bart Simpson'");
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
        
        // Test successful lock
        $gotLock = $this->lockingManager->getLock($entries[0], 'romanb');
        $this->assertTrue($gotLock);
        
        // Test failed lock (another user already got a lock on the entry)
        $gotLock = $this->lockingManager->getLock($entries[0], 'konstav');
        $this->assertFalse($gotLock);
        
        // Test release lock
        $released = $this->lockingManager->releaseLock($entries[0], 'romanb');
        $this->assertTrue($released);
    }
    
    /**
     * Tests the release mechanism of aged locks
     */
    public function testReleaseAgedLocks()
    {
zYne's avatar
zYne committed
54
        $entries = $this->connection->query("FROM Forum_Entry WHERE Forum_Entry.author = 'Bart Simpson'");
55 56 57 58 59 60 61 62 63 64 65 66
        $this->lockingManager->getLock($entries[0], 'romanb');
        $released = $this->lockingManager->releaseAgedLocks(-1); // age -1 seconds => release all
        $this->assertTrue($released);
        
        // A second call should return false (no locks left)
        $released = $this->lockingManager->releaseAgedLocks(-1);
        $this->assertFalse($released);
    }
}



zYne's avatar
zYne committed
67
?>