Back to index

moin  1.9.0~rc2
test_lock.py
Go to the documentation of this file.
00001 # -*- coding: utf-8 -*-
00002 """
00003     MoinMoin - MoinMoin.module_tested Tests
00004 
00005     @copyright: 2005 by Florian Festi,
00006                 2007 by MoinMoin:ThomasWaldmann
00007     @license: GNU GPL, see COPYING for details.
00008 """
00009 
00010 import tempfile, os, time, shutil
00011 
00012 import py
00013 
00014 from MoinMoin.util.lock import ExclusiveLock
00015 
00016 
00017 class TestExclusiveLock(object):
00018 
00019     def setup_method(self, method):
00020         self.test_dir = tempfile.mkdtemp('', 'lock_')
00021         self.test_dir_mtime_goal = time.time()
00022         self.test_dir_mtime_reported = os.stat(self.test_dir).st_mtime
00023         self.lock_dir = os.path.join(self.test_dir, "lock")
00024 
00025     def teardown_method(self, method):
00026         shutil.rmtree(self.test_dir)
00027 
00028     def testBrokenTimeAPI(self):
00029         """ util.lock: os.stat().mtime consistency with time.time()
00030 
00031             the timestamp os.stat reports as st_mtime on a fresh file should
00032             be the same (or at least almost the same) as the time time.time()
00033             reported at this time.
00034             Differences of n*3600s are usually operating system bugs / limitations,
00035             Win32 (as of Win XP SP2 + hotfixes 2006-04-30) is broken if you set
00036             TZ to a different value than the rest of the system uses.
00037             E.g. if you set "TZ=GMT1EDT" (and the rest of the system is setup
00038             on german/berlin timezone), it will report 7200s difference in the
00039             summer.
00040         """
00041         diff = self.test_dir_mtime_reported - self.test_dir_mtime_goal # diff should be 0 or near 0
00042         assert abs(diff) <= 2
00043 
00044     def testTimeout(self):
00045         """ util.lock: ExclusiveLock: raise ValueError for timeout < 2.0 """
00046         py.test.raises(ValueError, ExclusiveLock, self.lock_dir, timeout=1.0)
00047 
00048     def testAcquire(self):
00049         """ util.lock: ExclusiveLock: acquire """
00050         lock = ExclusiveLock(self.lock_dir)
00051         assert lock.acquire(0.1)
00052 
00053     def testRelease(self):
00054         """ util.lock: ExclusiveLock: release
00055 
00056         After releasing a lock, new one could be acquired.
00057         """
00058         lock = ExclusiveLock(self.lock_dir)
00059         if not lock.acquire(0.1):
00060             py.test.skip("can't acquire lock")
00061         lock.release()
00062         assert lock.acquire(0.1)
00063 
00064     def testIsLocked(self):
00065         """ util.lock: ExclusiveLock: isLocked """
00066         lock = ExclusiveLock(self.lock_dir)
00067         if not lock.acquire(0.1):
00068             py.test.skip("can't acquire lock")
00069         assert lock.isLocked()
00070         lock.release()
00071         assert not lock.isLocked()
00072 
00073     def testExists(self):
00074         """ util.lock: ExclusiveLock: exists """
00075         lock = ExclusiveLock(self.lock_dir)
00076         if not lock.acquire(0.1):
00077             py.test.skip("can't acquire lock")
00078         assert lock.exists()
00079 
00080     def testIsExpired(self):
00081         """ util.lock: ExclusiveLock: isExpired """
00082         timeout = 2.0
00083         lock = ExclusiveLock(self.lock_dir, timeout=timeout)
00084         if not lock.acquire(0.1):
00085             py.test.skip("can't acquire lock")
00086         assert not lock.isExpired()
00087         time.sleep(timeout)
00088         assert lock.isExpired()
00089 
00090     def testExpire(self):
00091         """ util.lock: ExclusiveLock: expire """
00092         timeout = 2.0
00093         lock = ExclusiveLock(self.lock_dir, timeout=timeout)
00094         if not lock.acquire(0.1):
00095             py.test.skip("can't acquire lock")
00096         assert not lock.expire()
00097         time.sleep(timeout)
00098         assert lock.expire()
00099 
00100     def testExclusive(self):
00101         """ util.lock: ExclusiveLock: lock is exclusive """
00102         first = ExclusiveLock(self.lock_dir)
00103         second = ExclusiveLock(self.lock_dir)
00104         if not first.acquire(0.1):
00105             py.test.skip("can't acquire lock")
00106         assert not second.acquire(0.1)
00107 
00108     def testAcquireAfterTimeout(self):
00109         """ util.lock: ExclusiveLock: acquire after timeout
00110 
00111         Lock with one lock, try to acquire another after timeout.
00112         """
00113         timeout = 2.0 # minimal timout
00114         first = ExclusiveLock(self.lock_dir, timeout)
00115         second = ExclusiveLock(self.lock_dir, timeout)
00116         if not first.acquire(0.1):
00117             py.test.skip("can't acquire lock")
00118         if second.acquire(0.1):
00119             py.test.skip("first lock is not exclusive")
00120         # Second lock should be acquired after timeout
00121         assert second.acquire(timeout + 0.1)
00122 
00123     def unlock(self, lock, delay):
00124         time.sleep(delay)
00125         lock.release()
00126 
00127 coverage_modules = ['MoinMoin.util.lock']
00128