2353.4.3
by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process. |
1 |
# Copyright (C) 2007 Canonical Ltd
|
2 |
#
|
|
3 |
# This program is free software; you can redistribute it and/or modify
|
|
4 |
# it under the terms of the GNU General Public License as published by
|
|
5 |
# the Free Software Foundation; either version 2 of the License, or
|
|
6 |
# (at your option) any later version.
|
|
7 |
#
|
|
8 |
# This program is distributed in the hope that it will be useful,
|
|
9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
11 |
# GNU General Public License for more details.
|
|
12 |
#
|
|
13 |
# You should have received a copy of the GNU General Public License
|
|
14 |
# along with this program; if not, write to the Free Software
|
|
15 |
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
16 |
||
17 |
"""Tests for temporarily upgrading to a WriteLock."""
|
|
18 |
||
19 |
from bzrlib import ( |
|
20 |
errors, |
|
21 |
)
|
|
22 |
||
23 |
from bzrlib.tests.per_lock import TestCaseWithLock |
|
24 |
||
25 |
||
26 |
class TestTemporaryWriteLock(TestCaseWithLock): |
|
27 |
||
28 |
def setUp(self): |
|
29 |
super(TestTemporaryWriteLock, self).setUp() |
|
30 |
self.build_tree(['a-file']) |
|
31 |
||
32 |
def test_can_upgrade_and_write(self): |
|
33 |
"""With only one lock, we should be able to write lock and switch back."""
|
|
34 |
a_lock = self.read_lock('a-file') |
|
35 |
try: |
|
2353.4.7
by John Arbash Meinel
Change the temporary_write_lock api, so that it always returns a lock object, |
36 |
success, t_write_lock = a_lock.temporary_write_lock() |
37 |
self.assertTrue(success, "We failed to grab a write lock.") |
|
2353.4.3
by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process. |
38 |
try: |
39 |
self.assertEqual('contents of a-file\n', |
|
40 |
t_write_lock.f.read()) |
|
41 |
# We should be able to write to the file.
|
|
42 |
t_write_lock.f.seek(0) |
|
43 |
t_write_lock.f.write('new contents for a-file\n') |
|
44 |
t_write_lock.f.seek(0) |
|
45 |
self.assertEqual('new contents for a-file\n', |
|
46 |
t_write_lock.f.read()) |
|
47 |
finally: |
|
48 |
a_lock = t_write_lock.restore_read_lock() |
|
49 |
finally: |
|
50 |
a_lock.unlock() |
|
51 |
||
52 |
def test_is_write_locked(self): |
|
53 |
"""With a temporary write lock, we cannot grab another lock."""
|
|
54 |
a_lock = self.read_lock('a-file') |
|
55 |
try: |
|
2353.4.7
by John Arbash Meinel
Change the temporary_write_lock api, so that it always returns a lock object, |
56 |
success, t_write_lock = a_lock.temporary_write_lock() |
57 |
self.assertTrue(success, "We failed to grab a write lock.") |
|
2353.4.3
by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process. |
58 |
try: |
59 |
self.assertRaises(errors.LockContention, |
|
60 |
self.write_lock, 'a-file') |
|
2363.3.2
by John Arbash Meinel
Disable read locks blocking on write locks. |
61 |
# TODO: jam 20070319 fcntl read locks are not currently fully
|
62 |
# mutually exclusive with write locks. This will be fixed
|
|
63 |
# in the next release.
|
|
64 |
# self.assertRaises(errors.LockContention,
|
|
65 |
# self.read_lock, 'a-file')
|
|
2353.4.3
by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process. |
66 |
finally: |
67 |
a_lock = t_write_lock.restore_read_lock() |
|
68 |
# Now we only have a read lock, so we should be able to grab
|
|
69 |
# another read lock, but not a write lock
|
|
2363.3.3
by John Arbash Meinel
make Write locks not block on Read locks, so that revert tests don't fail |
70 |
# TODO: jam 20070319 fcntl write locks are not currently fully
|
71 |
# mutually exclusive with read locks. This will be fixed
|
|
72 |
# in the next release.
|
|
73 |
# self.assertRaises(errors.LockContention,
|
|
74 |
# self.write_lock, 'a-file')
|
|
2353.4.3
by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process. |
75 |
b_lock = self.read_lock('a-file') |
76 |
b_lock.unlock() |
|
77 |
finally: |
|
78 |
a_lock.unlock() |
|
79 |
||
2353.4.7
by John Arbash Meinel
Change the temporary_write_lock api, so that it always returns a lock object, |
80 |
def test_fails_when_locked(self): |
81 |
"""We can't upgrade to a write lock if something else locks."""
|
|
82 |
a_lock = self.read_lock('a-file') |
|
83 |
try: |
|
84 |
b_lock = self.read_lock('a-file') |
|
85 |
try: |
|
86 |
success, alt_lock = a_lock.temporary_write_lock() |
|
87 |
self.assertFalse(success) |
|
88 |
# Now, 'alt_lock' should be a read-lock on the file. It should
|
|
89 |
# either be the same object as a_lock, or a new object.
|
|
90 |
# If it is a new object, a_lock should be unlocked. (we don't
|
|
91 |
# want to end up with 2 locks on the file)
|
|
92 |
self.assertTrue((alt_lock is a_lock) or (a_lock.f is None)) |
|
93 |
# set a_lock = alt_lock so that cleanup works correctly
|
|
94 |
a_lock = alt_lock |
|
95 |
||
96 |
# We should not be able to grab a write lock
|
|
97 |
# but we should be able to grab another read lock
|
|
2363.3.3
by John Arbash Meinel
make Write locks not block on Read locks, so that revert tests don't fail |
98 |
# TODO: jam 20070319 fcntl write locks are not currently fully
|
99 |
# mutually exclusive with read locks. This will be fixed
|
|
100 |
# in the next release.
|
|
101 |
# self.assertRaises(errors.LockContention,
|
|
102 |
# self.write_lock, 'a-file')
|
|
2353.4.7
by John Arbash Meinel
Change the temporary_write_lock api, so that it always returns a lock object, |
103 |
c_lock = self.read_lock('a-file') |
104 |
c_lock.unlock() |
|
105 |
finally: |
|
106 |
b_lock.unlock() |
|
107 |
finally: |
|
108 |
a_lock.unlock() |
|
109 |
||
2353.4.3
by John Arbash Meinel
Implement a 'ReadLock.temporary_write_lock()' to upgrade to a write-lock in-process. |
110 |
# TODO: jam 20070314 to truly test these, we should be spawning an external
|
111 |
# process, and having it lock/unlock/try lock on request.
|
|
112 |
||
113 |
# TODO: jam 20070314 Test that the write lock can fail if another process
|
|
114 |
# holds a read lock. And that we recover properly.
|