~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_smart.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2010-05-11 11:47:36 UTC
  • mfrom: (5200.3.8 lock_return)
  • Revision ID: pqm@pqm.ubuntu.com-20100511114736-mc1sq9zyo3vufec7
(lifeless) Provide a consistent interface to Tree, Branch,
 Repository where lock methods return an object with an unlock method to
 unlock the lock. This breaks the API for Branch,
 Repository on their lock_write methods. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
767
767
class TestLockedBranch(tests.TestCaseWithMemoryTransport):
768
768
 
769
769
    def get_lock_tokens(self, branch):
770
 
        branch_token = branch.lock_write()
771
 
        repo_token = branch.repository.lock_write()
 
770
        branch_token = branch.lock_write().branch_token
 
771
        repo_token = branch.repository.lock_write().repository_token
772
772
        branch.repository.unlock()
773
773
        return branch_token, repo_token
774
774
 
1073
1073
            response)
1074
1074
 
1075
1075
 
1076
 
class TestSmartServerBranchRequestSetParent(tests.TestCaseWithMemoryTransport):
 
1076
class TestSmartServerBranchRequestSetParent(TestLockedBranch):
1077
1077
 
1078
1078
    def test_set_parent_none(self):
1079
1079
        branch = self.make_branch('base', format="1.9")
1082
1082
        branch.unlock()
1083
1083
        request = smart_branch.SmartServerBranchRequestSetParentLocation(
1084
1084
            self.get_transport())
1085
 
        branch_token = branch.lock_write()
1086
 
        repo_token = branch.repository.lock_write()
 
1085
        branch_token, repo_token = self.get_lock_tokens(branch)
1087
1086
        try:
1088
1087
            response = request.execute('base', branch_token, repo_token, '')
1089
1088
        finally:
1090
 
            branch.repository.unlock()
1091
1089
            branch.unlock()
1092
1090
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1093
1091
        self.assertEqual(None, branch.get_parent())
1096
1094
        branch = self.make_branch('base', format="1.9")
1097
1095
        request = smart_branch.SmartServerBranchRequestSetParentLocation(
1098
1096
            self.get_transport())
1099
 
        branch_token = branch.lock_write()
1100
 
        repo_token = branch.repository.lock_write()
 
1097
        branch_token, repo_token = self.get_lock_tokens(branch)
1101
1098
        try:
1102
1099
            response = request.execute('base', branch_token, repo_token,
1103
1100
            'http://bar/')
1104
1101
        finally:
1105
 
            branch.repository.unlock()
1106
1102
            branch.unlock()
1107
1103
        self.assertEqual(smart_req.SuccessfulSmartServerResponse(()), response)
1108
1104
        self.assertEqual('http://bar/', branch.get_parent())
1137
1133
            response)
1138
1134
 
1139
1135
 
1140
 
class TestSmartServerBranchRequestLockWrite(tests.TestCaseWithMemoryTransport):
 
1136
class TestSmartServerBranchRequestLockWrite(TestLockedBranch):
1141
1137
 
1142
1138
    def setUp(self):
1143
1139
        tests.TestCaseWithMemoryTransport.setUp(self)
1165
1161
        backing = self.get_transport()
1166
1162
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1167
1163
        branch = self.make_branch('.')
1168
 
        branch_token = branch.lock_write()
 
1164
        branch_token = branch.lock_write().branch_token
1169
1165
        branch.leave_lock_in_place()
1170
1166
        branch.unlock()
1171
1167
        response = request.execute('')
1180
1176
        backing = self.get_transport()
1181
1177
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1182
1178
        branch = self.make_branch('.', format='knit')
1183
 
        branch_token = branch.lock_write()
1184
 
        repo_token = branch.repository.lock_write()
1185
 
        branch.repository.unlock()
 
1179
        branch_token, repo_token = self.get_lock_tokens(branch)
1186
1180
        branch.leave_lock_in_place()
1187
1181
        branch.repository.leave_lock_in_place()
1188
1182
        branch.unlock()
1203
1197
        backing = self.get_transport()
1204
1198
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1205
1199
        branch = self.make_branch('.', format='knit')
1206
 
        branch_token = branch.lock_write()
1207
 
        repo_token = branch.repository.lock_write()
1208
 
        branch.repository.unlock()
 
1200
        branch_token, repo_token = self.get_lock_tokens(branch)
1209
1201
        branch.leave_lock_in_place()
1210
1202
        branch.repository.leave_lock_in_place()
1211
1203
        branch.unlock()
1226
1218
        request = smart_branch.SmartServerBranchRequestLockWrite(backing)
1227
1219
        branch = self.make_branch('.', format='knit')
1228
1220
        repo = branch.repository
1229
 
        repo_token = repo.lock_write()
 
1221
        repo_token = repo.lock_write().repository_token
1230
1222
        repo.leave_lock_in_place()
1231
1223
        repo.unlock()
1232
1224
        response = request.execute('')
1249
1241
        self.assertEqual('LockFailed', error_name)
1250
1242
 
1251
1243
 
1252
 
class TestSmartServerBranchRequestUnlock(tests.TestCaseWithMemoryTransport):
 
1244
class TestSmartServerBranchRequestUnlock(TestLockedBranch):
1253
1245
 
1254
1246
    def setUp(self):
1255
1247
        tests.TestCaseWithMemoryTransport.setUp(self)
1259
1251
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1260
1252
        branch = self.make_branch('.', format='knit')
1261
1253
        # Lock the branch
1262
 
        branch_token = branch.lock_write()
1263
 
        repo_token = branch.repository.lock_write()
1264
 
        branch.repository.unlock()
 
1254
        branch_token, repo_token = self.get_lock_tokens(branch)
1265
1255
        # Unlock the branch (and repo) object, leaving the physical locks
1266
1256
        # in place.
1267
1257
        branch.leave_lock_in_place()
1291
1281
        request = smart_branch.SmartServerBranchRequestUnlock(backing)
1292
1282
        branch = self.make_branch('.', format='knit')
1293
1283
        # Lock the repository.
1294
 
        repo_token = branch.repository.lock_write()
 
1284
        repo_token = branch.repository.lock_write().repository_token
1295
1285
        branch.repository.leave_lock_in_place()
1296
1286
        branch.repository.unlock()
1297
1287
        # Issue branch lock_write request on the unlocked branch (with locked
1298
1288
        # repo).
1299
 
        response = request.execute(
1300
 
            '', 'branch token', repo_token)
 
1289
        response = request.execute('', 'branch token', repo_token)
1301
1290
        self.assertEqual(
1302
1291
            smart_req.SmartServerResponse(('TokenMismatch',)), response)
1303
1292
        # Cleanup
1610
1599
        backing = self.get_transport()
1611
1600
        request = smart_repo.SmartServerRepositoryLockWrite(backing)
1612
1601
        repository = self.make_repository('.', format='knit')
1613
 
        repo_token = repository.lock_write()
 
1602
        repo_token = repository.lock_write().repository_token
1614
1603
        repository.leave_lock_in_place()
1615
1604
        repository.unlock()
1616
1605
        response = request.execute('')
1658
1647
        request = smart_repo.SmartServerRepositoryInsertStreamLocked(
1659
1648
            backing)
1660
1649
        repository = self.make_repository('.', format='knit')
1661
 
        lock_token = repository.lock_write()
 
1650
        lock_token = repository.lock_write().repository_token
1662
1651
        response = request.execute('', '', lock_token)
1663
1652
        self.assertEqual(None, response)
1664
1653
        response = request.do_chunk(self.make_empty_byte_stream(repository))
1672
1661
        request = smart_repo.SmartServerRepositoryInsertStreamLocked(
1673
1662
            backing)
1674
1663
        repository = self.make_repository('.', format='knit')
1675
 
        lock_token = repository.lock_write()
 
1664
        lock_token = repository.lock_write().repository_token
1676
1665
        self.assertRaises(
1677
1666
            errors.TokenMismatch, request.execute, '', '', 'wrong-token')
1678
1667
        repository.unlock()
1687
1676
        backing = self.get_transport()
1688
1677
        request = smart_repo.SmartServerRepositoryUnlock(backing)
1689
1678
        repository = self.make_repository('.', format='knit')
1690
 
        token = repository.lock_write()
 
1679
        token = repository.lock_write().repository_token
1691
1680
        repository.leave_lock_in_place()
1692
1681
        repository.unlock()
1693
1682
        response = request.execute('', token)