def test_basic_locking_with_index(self):
        
        def my_process2(the_file):
            self.assert_(locker.is_locked(self.my_file))
            the_file.write('to it 2')

        def my_process(the_file):
            self.assert_(locker.is_locked(self.my_file))
            the_file.write('to it')
        
        # let's lock a file
        locker.with_lock(self.my_file, 'w', my_process, self.index)

        # we should have some content
        content = open(self.my_file).read()
        self.assertEquals(content, 'to it')
        self.assert_(not locker.is_locked(self.my_file))  
        
        # let's check the index file
        index_file = open(self.index).readlines()
        indexes = [f.split('#') for f in index_file]
        index = indexes[0]
        self.assertEquals(index[0], self.my_file)
        self.assertEquals(index[1].strip(), 
                          '6a94cc3e2b33c83937018b3ce365643c')

        locker.with_lock(self.my_file, 'w', my_process2, self.index)

        # the md5 should have changed
        index_file = open(self.index).readlines()
        indexes = [f.split('#') for f in index_file]
        index = indexes[0] 
        self.assertEquals(index[1].strip(), 
                          '39d03242196d6cbf4389bfcfe2f2b989') 
 def test_basic_locking(self):
     def my_process(the_file):
         self.assert_(locker.is_locked(self.my_file))
         the_file.write('to it')
     
     # let's lock a file
     locker.with_lock(self.my_file, 'w', my_process)
     # we should have some content
     content = open(self.my_file).read()
     self.assertEquals(content, 'to it')
     self.assert_(not locker.is_locked(self.my_file))  
    def test_thread_access(self):
        # make sure the lock is really preventing several
        # thread to conflict
        class Worker(threading.Thread):
            def __init__(self, filename, name):
                threading.Thread.__init__(self, name=name)
                self.filename = filename

            def _write(self, file_):
                for i in range(10):  
                    file_.write('%s\n' % self.getName())
                    time.sleep(0.4)

            def run(self):
                locker.with_lock(self.filename, 'w', self._write)
        
        w1 = Worker(self.my_file, 'worker1')
        w1.start()
        self.assert_(locker.is_locked(self.my_file))
        self.assertRaises(locker.AlreadyLocked, 
                          locker.with_lock,
                          self.my_file, 'w', lambda f: f.write('.'))
        w1.join()
 def my_process(the_file):
     self.assert_(locker.is_locked(self.my_file))
     the_file.write('to it')
 def something_done():
     self.assert_(locker.is_locked(self.my_file))
 def my_process(the_file):
     self.assert_(locker.is_locked(self.my_file))
     time.sleep(1)
     locker.with_lock(self.my_file, 'w', my_process2)
 def my_process2(the_file):
     self.assert_(locker.is_locked(self.my_file))