view MoinMoin/_tests/ @ 1294:85e56dced03b

add test for 'new locking' (avoid locking) method - please check this on windows *.* and OS X
author Thomas Waldmann <tw AT waldmann-edv DOT de>
date Thu, 17 Aug 2006 10:55:11 +0200
parents b38376f52c8f
line wrap: on
line source
# -*- coding: utf-8 -*-
    MoinMoin - MoinMoin test for new style "locking" (== mostly avoid locking)

    The idea is to not have to lock files when we just want to read them.
    When we never overwrite file content with new stuff, locking is not needed.
    We can just write the new content into a new file (with tmpfname) and then
    rename it to the original filename. Files that opened the original filename
    before the rename will still read old content after the rename (until they
    are closed).

    @copyright: 2006 by Thomas Waldmann (idea: Bastian Blank)
    @license: GNU GPL, see COPYING for details.

import unittest, tempfile, os, shutil
from MoinMoin._tests import TestConfig, TestSkipped

def rename(oldname, newname):
    """ Multiplatform rename

    Move to MoinMoin.util.filesys when done.

    Test/Fix win32 stuff.
    Check: MoveFileEx: If the new filename is None, it deletes the file (needs very recent pywin32 binding).
           This is documented for the "on reboot" stuff, does this also work when not doing it on next reboot?
           Maybe we can use this at another place.
    API doc:
    Windows 95/98/ME do not implement MoveFileEx().
    Either have some other working code or document we drop support for less-than-NT.
    Document pywin32 extension dependency.

    # this nt specific code should be replaced by better stuff
    if == 'nt':
        # uses mark hammond's pywin32 extension
        # there seems to be also stuff in win32api.MoveFileEx and win32con.MOVEFILE_REPLACE_EXISTING
        # what's the difference to them in win32file?
        from win32file import MoveFileEx, MOVEFILE_REPLACE_EXISTING
        ret = MoveFileEx(oldname, newname, MOVEFILE_REPLACE_EXISTING)
        # If the function succeeds, the return value is nonzero.
        # If the function fails, the return value is 0 (zero). To get extended error information, call GetLastError.
        if ret == 0:
            raise OSError # emulate os.rename behaviour
        os.rename(oldname, newname) # rename has no return value, but raises OSError in case of failure

class NewLockTests(unittest.TestCase):

    def setUp(self):
        self.test_dir = tempfile.mkdtemp('', 'lock_')

    def tearDown(self):

    def testNoLockingForReading(self):
        """ new locking: NoLockingForReading tests if files still work when filename is target of a rename """
        fname = os.path.join(self.test_dir, 'readtest')
        tmpfname = os.path.join(self.test_dir, '__readtest')
        origdata = "precious content"
        newdata = "new content"
        f = file(fname, "w") ; f.write(origdata) ; f.close()
        f = file(fname, "r")
        ftmp = file(tmpfname, "w") ; ftmp.write(newdata) ; ftmp.close()
        rename(tmpfname, fname)
        read1data = ; f.close() # we should still get origdata here!
        f = file(fname, "r") ; read2data = ; f.close() # we should newdata now.
        self.failUnless(origdata == read1data and newdata == read2data, "got wrong data when reading")