Skip to content

Commit

Permalink
fixup: re-add the replay test
Browse files Browse the repository at this point in the history
  • Loading branch information
ThomasWaldmann committed Aug 3, 2016
1 parent a063a4d commit 9967cc4
Showing 1 changed file with 36 additions and 4 deletions.
40 changes: 36 additions & 4 deletions borg/testsuite/repository.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,17 @@
from . import BaseTestCase


UNSPECIFIED = object() # for default values where we can't use None


class RepositoryTestCaseBase(BaseTestCase):
key_size = 32
exclusive = True

def open(self, create=False):
return Repository(os.path.join(self.tmppath, 'repository'), exclusive=True, create=create)
def open(self, create=False, exclusive=UNSPECIFIED):
if exclusive is UNSPECIFIED:
exclusive = self.exclusive
return Repository(os.path.join(self.tmppath, 'repository'), exclusive=exclusive, create=create)

def setUp(self):
self.tmppath = tempfile.mkdtemp()
Expand All @@ -27,10 +33,10 @@ def tearDown(self):
self.repository.close()
shutil.rmtree(self.tmppath)

def reopen(self):
def reopen(self, exclusive=UNSPECIFIED):
if self.repository:
self.repository.close()
self.repository = self.open()
self.repository = self.open(exclusive=exclusive)


class RepositoryTestCase(RepositoryTestCaseBase):
Expand Down Expand Up @@ -168,6 +174,32 @@ def test_crash_before_write_index(self):
self.assert_equal(len(self.repository), 3)
self.assert_equal(self.repository.check(), True)

def test_replay_lock_upgrade_old(self):
self.add_keys()
for name in os.listdir(self.repository.path):
if name.startswith('index.'):
os.unlink(os.path.join(self.repository.path, name))
with patch.object(Lock, 'upgrade', side_effect=LockFailed) as upgrade:
self.reopen(exclusive=None) # simulate old client that always does lock upgrades
with self.repository:
# the repo is only locked by a shared read lock, but to replay segments,
# we need an exclusive write lock - check if the lock gets upgraded.
self.assert_raises(LockFailed, lambda: len(self.repository))
upgrade.assert_called_once_with()

def test_replay_lock_upgrade(self):
self.add_keys()
for name in os.listdir(self.repository.path):
if name.startswith('index.'):
os.unlink(os.path.join(self.repository.path, name))
with patch.object(Lock, 'upgrade', side_effect=LockFailed) as upgrade:
self.reopen(exclusive=False) # current client usually does not do lock upgrade, except for replay
with self.repository:
# the repo is only locked by a shared read lock, but to replay segments,
# we need an exclusive write lock - check if the lock gets upgraded.
self.assert_raises(LockFailed, lambda: len(self.repository))
upgrade.assert_called_once_with()

def test_crash_before_deleting_compacted_segments(self):
self.add_keys()
self.repository.io.delete_segment = None
Expand Down

0 comments on commit 9967cc4

Please sign in to comment.