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_lock(self): # let's lock the file from collective.psc.mirroring.locker import with_lock filename = join(self.file_path, "file") def locked_state(file_): # let's try to notify here, we should get a conflict error self.proj.relfolder.rel.file.update_data("xx") wf.doActionFor(rel, "release-beta") self.assertRaises(ConflictError, notify, edited) with_lock(filename, "wb", locked_state)
def test_time_lock(self): # make sure the lock is removed after a while old = locker.MAX_LOCK_TIME locker.MAX_LOCK_TIME = 0.5 def my_process2(the_file): 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) # let's lock a file try: locker.with_lock(self.my_file, 'w', my_process) finally: locker.MAX_LOCK_TIME = old
def test_deletion(self): def my(f): f.write('wwww') locker.with_lock(self.my_file, 'w', my, self.index) content = open(self.index).read().split('#') self.assertEquals(content[-1].strip(), 'e34a8899ef6468b74f8a1048419ccc8b') # let's remove a file locker.remove_file(self.my_file, self.index) # should be gone assert not os.path.exists(self.my_file) # should be gone from index content = open(self.index).read() self.assertEquals(content, '')
def run(self): locker.with_lock(self.filename, 'w', self._write)
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)