'Test book file open in another process when changing metadata'

@unittest.skipUnless(iswindows, u'Windows    only')
def test_windows_atomic_move(self):

    cl = self.cloned_library
    cache = self.init_cache(cl)
    fpath = cache.format_abspath(1, u'FMT1')
    f = open(fpath, u'rb')
    with self.assertRaises(IOError):
        cache.set_field(u'title', {1: u'Moved'})
    with self.assertRaises(IOError):
        cache.remove_books({1})
    f.close()
    self.assertNotEqual(cache.field_for(u'title', 1), u'Moved', u'Title    was    changed    despite    file    lock')
    from calibre.ptempfile import TemporaryDirectory
    from calibre.utils.filenames import hardlink_file, WindowsAtomicFolderMove
    raw = 'xxx'
    with TemporaryDirectory() as tdir1:
        with TemporaryDirectory() as tdir2:
            (a, b) = (os.path.join(tdir1, u'a'), os.path.join(tdir1, u'b'))
            a = os.path.join(tdir1, u'a')
            with open(a, u'wb') as f:
                f.write(raw)
            hardlink_file(a, b)
            wam = WindowsAtomicFolderMove(tdir1)
            wam.copy_path_to(a, os.path.join(tdir2, u'a'))
            wam.copy_path_to(b, os.path.join(tdir2, u'b'))
            wam.delete_originals()
            self.assertEqual([], os.listdir(tdir1))
            self.assertEqual({u'a', u'b'}, set(os.listdir(tdir2)))
            self.assertEqual(raw, open(os.path.join(tdir2, u'a'), u'rb').read())
            self.assertEqual(raw, open(os.path.join(tdir2, u'b'), u'rb').read())
