Back to index

obnam  1.1
lockmgr_tests.py
Go to the documentation of this file.
00001 # Copyright 2012  Lars Wirzenius
00002 # 
00003 # This program is free software: you can redistribute it and/or modify
00004 # it under the terms of the GNU General Public License as published by
00005 # the Free Software Foundation, either version 3 of the License, or
00006 # (at your option) any later version.
00007 # 
00008 # This program is distributed in the hope that it will be useful,
00009 # but WITHOUT ANY WARRANTY; without even the implied warranty of
00010 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011 # GNU General Public License for more details.
00012 # 
00013 # You should have received a copy of the GNU General Public License
00014 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
00015 
00016 
00017 import os
00018 import shutil
00019 import tempfile
00020 import unittest
00021 
00022 import obnamlib
00023 
00024 
00025 class LockManagerTests(unittest.TestCase):
00026 
00027     def locked(self, dirname):
00028         return os.path.exists(os.path.join(dirname, 'lock'))
00029 
00030     def fake_time(self):
00031         self.now += 1
00032         return self.now
00033 
00034     def setUp(self):
00035         self.tempdir = tempfile.mkdtemp()
00036         self.dirnames = []
00037         for x in ['a', 'b', 'c']:
00038             dirname = os.path.join(self.tempdir, x)
00039             os.mkdir(dirname)
00040             self.dirnames.append(dirname)
00041         self.fs = obnamlib.LocalFS(self.tempdir)
00042         self.timeout = 10
00043         self.now = 0
00044         self.lm = obnamlib.LockManager(self.fs, self.timeout, '')
00045         self.lm._time = self.fake_time
00046         self.lm._sleep = lambda: None
00047         
00048     def tearDown(self):
00049         shutil.rmtree(self.tempdir)
00050         
00051     def test_has_nothing_locked_initially(self):
00052         for dirname in self.dirnames:
00053             self.assertFalse(self.locked(dirname))
00054 
00055     def test_locks_single_directory(self):
00056         self.lm.lock([self.dirnames[0]])
00057         self.assertTrue(self.locked(self.dirnames[0]))
00058 
00059     def test_unlocks_single_directory(self):
00060         self.lm.lock([self.dirnames[0]])
00061         self.lm.unlock([self.dirnames[0]])
00062         self.assertFalse(self.locked(self.dirnames[0]))
00063 
00064     def test_waits_until_timeout_for_locked_directory(self):
00065         self.lm.lock([self.dirnames[0]])
00066         self.assertRaises(obnamlib.LockFail,
00067                           self.lm.lock, [self.dirnames[0]])
00068         self.assertTrue(self.now >= self.timeout)
00069 
00070     def test_notices_when_preexisting_lock_goes_away(self):
00071         self.lm.lock([self.dirnames[0]])
00072         self.lm._sleep = lambda: os.remove(self.lm._lockname(self.dirnames[0]))
00073         self.lm.lock([self.dirnames[0]])
00074         self.assertTrue(True)
00075 
00076     def test_locks_all_directories(self):
00077         self.lm.lock(self.dirnames)
00078         for dirname in self.dirnames:
00079             self.assertTrue(self.locked(dirname))
00080 
00081     def test_unlocks_all_directories(self):
00082         self.lm.lock(self.dirnames)
00083         self.lm.unlock(self.dirnames)
00084         for dirname in self.dirnames:
00085             self.assertFalse(self.locked(dirname))
00086 
00087     def test_does_not_lock_anything_if_one_lock_fails(self):
00088         self.lm.lock([self.dirnames[-1]])
00089         self.assertRaises(obnamlib.LockFail, self.lm.lock, self.dirnames)
00090         for dirname in self.dirnames[:-1]:
00091             self.assertFalse(self.locked(dirname))
00092         self.assertTrue(self.locked(self.dirnames[-1]))
00093 
00094