934
934
size = trans.put_file(name, stream)
935
935
return GraphIndex(trans, name, size)
937
def make_combined_index_with_missing(self, missing=['1', '2']):
938
"""Create a CombinedGraphIndex which will have missing indexes.
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
944
:param missing: The underlying indexes to delete
945
:return: (CombinedGraphIndex, reload_counter)
947
index1 = self.make_index('1', nodes=[(('1',), '', ())])
948
index2 = self.make_index('2', nodes=[(('2',), '', ())])
949
index3 = self.make_index('3', nodes=[
953
# total_reloads, num_changed, num_unchanged
954
reload_counter = [0, 0, 0]
956
reload_counter[0] += 1
957
new_indices = [index3]
958
if index._indices == new_indices:
959
reload_counter[2] += 1
961
reload_counter[1] += 1
962
index._indices[:] = new_indices
964
index = CombinedGraphIndex([index1, index2], reload_func=reload)
965
trans = self.get_transport()
966
for fname in missing:
968
return index, reload_counter
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()
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)
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)
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(
1130
self.assertRaises(errors.NoSuchFile, index.key_count)
1131
self.assertEqual([2, 1, 1], reload_counter)
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',), '')],
1139
self.assertEqual([1, 1, 0], reload_counter)
1141
def test_iter_entries_reloads_midway(self):
1142
# The first index still looks present, so we get interrupted mid-way
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',), '')],
1152
self.assertEqual([1, 1, 0], reload_counter)
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',)])
1160
def test_iter_entries_reloads_and_fails(self):
1161
index, reload_counter = self.make_combined_index_with_missing(
1163
self.assertListRaises(errors.NoSuchFile, index.iter_entries, [('3',)])
1164
self.assertEqual([2, 1, 1], reload_counter)
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',), '')],
1172
self.assertEqual([1, 1, 0], reload_counter)
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',), '')],
1183
self.assertEqual([1, 1, 0], reload_counter)
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)
1190
def test_iter_all_entries_reloads_and_fails(self):
1191
index, reload_counter = self.make_combined_index_with_missing(
1193
self.assertListRaises(errors.NoSuchFile, index.iter_all_entries)
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)
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)
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,
1218
def test_iter_entries_prefix_reloads_and_fails(self):
1219
index, reload_counter = self.make_combined_index_with_missing(
1221
self.assertListRaises(errors.NoSuchFile, index.iter_entries_prefix,
1224
def test_validate_reloads(self):
1225
index, reload_counter = self.make_combined_index_with_missing()
1227
self.assertEqual([1, 1, 0], reload_counter)
1229
def test_validate_reloads_midway(self):
1230
index, reload_counter = self.make_combined_index_with_missing(['2'])
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)
1238
def test_validate_reloads_and_fails(self):
1239
index, reload_counter = self.make_combined_index_with_missing(
1241
self.assertRaises(errors.NoSuchFile, index.validate)
1081
1244
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):