~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_index.py

  • Committer: Andrew Bennetts
  • Date: 2008-10-28 05:50:42 UTC
  • mfrom: (3789.1.9 pack_retry_153786)
  • mto: This revision was merged to the branch mainline in revision 3814.
  • Revision ID: andrew.bennetts@canonical.com-20081028055042-kr2z2vroqiljpse6
Merge John's pack-retry fix.

Show diffs side-by-side

added added

removed removed

Lines of Context:
934
934
        size = trans.put_file(name, stream)
935
935
        return GraphIndex(trans, name, size)
936
936
 
 
937
    def make_combined_index_with_missing(self, missing=['1', '2']):
 
938
        """Create a CombinedGraphIndex which will have missing indexes.
 
939
 
 
940
        This creates a CGI which thinks it has 2 indexes, however they have
 
941
        been deleted. If CGI._reload_func() is called, then it will repopulate
 
942
        with a new index.
 
943
 
 
944
        :param missing: The underlying indexes to delete
 
945
        :return: (CombinedGraphIndex, reload_counter)
 
946
        """
 
947
        index1 = self.make_index('1', nodes=[(('1',), '', ())])
 
948
        index2 = self.make_index('2', nodes=[(('2',), '', ())])
 
949
        index3 = self.make_index('3', nodes=[
 
950
            (('1',), '', ()),
 
951
            (('2',), '', ())])
 
952
 
 
953
        # total_reloads, num_changed, num_unchanged
 
954
        reload_counter = [0, 0, 0]
 
955
        def reload():
 
956
            reload_counter[0] += 1
 
957
            new_indices = [index3]
 
958
            if index._indices == new_indices:
 
959
                reload_counter[2] += 1
 
960
                return False
 
961
            reload_counter[1] += 1
 
962
            index._indices[:] = new_indices
 
963
            return True
 
964
        index = CombinedGraphIndex([index1, index2], reload_func=reload)
 
965
        trans = self.get_transport()
 
966
        for fname in missing:
 
967
            trans.delete(fname)
 
968
        return index, reload_counter
 
969
 
937
970
    def test_open_missing_index_no_error(self):
938
971
        trans = self.get_transport()
939
972
        index1 = GraphIndex(trans, 'missing', 100)
1077
1110
        index = CombinedGraphIndex([])
1078
1111
        index.validate()
1079
1112
 
 
1113
    def test_key_count_reloads(self):
 
1114
        index, reload_counter = self.make_combined_index_with_missing()
 
1115
        self.assertEqual(2, index.key_count())
 
1116
        self.assertEqual([1, 1, 0], reload_counter)
 
1117
 
 
1118
    def test_key_count_no_reload(self):
 
1119
        index, reload_counter = self.make_combined_index_with_missing()
 
1120
        index._reload_func = None
 
1121
        # Without a _reload_func we just raise the exception
 
1122
        self.assertRaises(errors.NoSuchFile, index.key_count)
 
1123
 
 
1124
    def test_key_count_reloads_and_fails(self):
 
1125
        # We have deleted all underlying indexes, so we will try to reload, but
 
1126
        # still fail. This is mostly to test we don't get stuck in an infinite
 
1127
        # loop trying to reload
 
1128
        index, reload_counter = self.make_combined_index_with_missing(
 
1129
                                    ['1', '2', '3'])
 
1130
        self.assertRaises(errors.NoSuchFile, index.key_count)
 
1131
        self.assertEqual([2, 1, 1], reload_counter)
 
1132
 
 
1133
    def test_iter_entries_reloads(self):
 
1134
        index, reload_counter = self.make_combined_index_with_missing()
 
1135
        result = list(index.iter_entries([('1',), ('2',), ('3',)]))
 
1136
        index3 = index._indices[0]
 
1137
        self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
 
1138
                         result)
 
1139
        self.assertEqual([1, 1, 0], reload_counter)
 
1140
 
 
1141
    def test_iter_entries_reloads_midway(self):
 
1142
        # The first index still looks present, so we get interrupted mid-way
 
1143
        # through
 
1144
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1145
        index1, index2 = index._indices
 
1146
        result = list(index.iter_entries([('1',), ('2',), ('3',)]))
 
1147
        index3 = index._indices[0]
 
1148
        # We had already yielded '1', so we just go on to the next, we should
 
1149
        # not yield '1' twice.
 
1150
        self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
 
1151
                         result)
 
1152
        self.assertEqual([1, 1, 0], reload_counter)
 
1153
 
 
1154
    def test_iter_entries_no_reload(self):
 
1155
        index, reload_counter = self.make_combined_index_with_missing()
 
1156
        index._reload_func = None
 
1157
        # Without a _reload_func we just raise the exception
 
1158
        self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
 
1159
 
 
1160
    def test_iter_entries_reloads_and_fails(self):
 
1161
        index, reload_counter = self.make_combined_index_with_missing(
 
1162
                                    ['1', '2', '3'])
 
1163
        self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
 
1164
        self.assertEqual([2, 1, 1], reload_counter)
 
1165
 
 
1166
    def test_iter_all_entries_reloads(self):
 
1167
        index, reload_counter = self.make_combined_index_with_missing()
 
1168
        result = list(index.iter_all_entries())
 
1169
        index3 = index._indices[0]
 
1170
        self.assertEqual([(index3, ('1',), ''), (index3, ('2',), '')],
 
1171
                         result)
 
1172
        self.assertEqual([1, 1, 0], reload_counter)
 
1173
 
 
1174
    def test_iter_all_entries_reloads_midway(self):
 
1175
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1176
        index1, index2 = index._indices
 
1177
        result = list(index.iter_all_entries())
 
1178
        index3 = index._indices[0]
 
1179
        # We had already yielded '1', so we just go on to the next, we should
 
1180
        # not yield '1' twice.
 
1181
        self.assertEqual([(index1, ('1',), ''), (index3, ('2',), '')],
 
1182
                         result)
 
1183
        self.assertEqual([1, 1, 0], reload_counter)
 
1184
 
 
1185
    def test_iter_all_entries_no_reload(self):
 
1186
        index, reload_counter = self.make_combined_index_with_missing()
 
1187
        index._reload_func = None
 
1188
        self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
 
1189
 
 
1190
    def test_iter_all_entries_reloads_and_fails(self):
 
1191
        index, reload_counter = self.make_combined_index_with_missing(
 
1192
                                    ['1', '2', '3'])
 
1193
        self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
 
1194
 
 
1195
    def test_iter_entries_prefix_reloads(self):
 
1196
        index, reload_counter = self.make_combined_index_with_missing()
 
1197
        result = list(index.iter_entries_prefix([('1',)]))
 
1198
        index3 = index._indices[0]
 
1199
        self.assertEqual([(index3, ('1',), '')], result)
 
1200
        self.assertEqual([1, 1, 0], reload_counter)
 
1201
 
 
1202
    def test_iter_entries_prefix_reloads_midway(self):
 
1203
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1204
        index1, index2 = index._indices
 
1205
        result = list(index.iter_entries_prefix([('1',)]))
 
1206
        index3 = index._indices[0]
 
1207
        # We had already yielded '1', so we just go on to the next, we should
 
1208
        # not yield '1' twice.
 
1209
        self.assertEqual([(index1, ('1',), '')], result)
 
1210
        self.assertEqual([1, 1, 0], reload_counter)
 
1211
 
 
1212
    def test_iter_entries_prefix_no_reload(self):
 
1213
        index, reload_counter = self.make_combined_index_with_missing()
 
1214
        index._reload_func = None
 
1215
        self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
 
1216
                                                 [('1',)])
 
1217
 
 
1218
    def test_iter_entries_prefix_reloads_and_fails(self):
 
1219
        index, reload_counter = self.make_combined_index_with_missing(
 
1220
                                    ['1', '2', '3'])
 
1221
        self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
 
1222
                                                 [('1',)])
 
1223
 
 
1224
    def test_validate_reloads(self):
 
1225
        index, reload_counter = self.make_combined_index_with_missing()
 
1226
        index.validate()
 
1227
        self.assertEqual([1, 1, 0], reload_counter)
 
1228
 
 
1229
    def test_validate_reloads_midway(self):
 
1230
        index, reload_counter = self.make_combined_index_with_missing(['2'])
 
1231
        index.validate()
 
1232
 
 
1233
    def test_validate_no_reload(self):
 
1234
        index, reload_counter = self.make_combined_index_with_missing()
 
1235
        index._reload_func = None
 
1236
        self.assertRaises(errors.NoSuchFile, index.validate)
 
1237
 
 
1238
    def test_validate_reloads_and_fails(self):
 
1239
        index, reload_counter = self.make_combined_index_with_missing(
 
1240
                                    ['1', '2', '3'])
 
1241
        self.assertRaises(errors.NoSuchFile, index.validate)
 
1242
 
1080
1243
 
1081
1244
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):
1082
1245