~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/tests/test_index.py

CombinedGraphIndex can now reload when calling key_count().

Show diffs side-by-side

added added

removed removed

Lines of Context:
927
927
        size = trans.put_file(name, stream)
928
928
        return GraphIndex(trans, name, size)
929
929
 
 
930
    def make_index_with_missing_children(self):
 
931
        """Create a CombinedGraphIndex which will have missing indexes.
 
932
 
 
933
        This creates a CGI which thinks it has 2 indexes, however they have
 
934
        been deleted. If CGI._reload_func() is called, then it will repopulate
 
935
        with a new index.
 
936
 
 
937
        :return: (CombinedGraphIndex, reload_counter)
 
938
        """
 
939
        index1 = self.make_index('1', nodes=[(('1',), '', ())])
 
940
        index2 = self.make_index('2', nodes=[(('2',), '', ())])
 
941
        index3 = self.make_index('3', nodes=[
 
942
            (('1',), '', ()),
 
943
            (('2',), '', ())])
 
944
 
 
945
        # total_reloads, num_changed, num_unchanged
 
946
        reload_counter = [0, 0, 0]
 
947
        def reload():
 
948
            reload_counter[0] += 1
 
949
            new_indices = [index3]
 
950
            if index._indices == new_indices:
 
951
                reload_counter[2] += 1
 
952
                return False
 
953
            reload_counter[1] += 1
 
954
            index._indices[:] = new_indices
 
955
            return True
 
956
        index = CombinedGraphIndex([index1, index2], reload_func=reload)
 
957
        trans = self.get_transport()
 
958
        trans.delete('1')
 
959
        trans.delete('2')
 
960
        return index, reload_counter
 
961
 
930
962
    def test_open_missing_index_no_error(self):
931
963
        trans = self.get_transport()
932
964
        index1 = GraphIndex(trans, 'missing', 100)
1070
1102
        index = CombinedGraphIndex([])
1071
1103
        index.validate()
1072
1104
 
 
1105
    def test_key_count_reloads(self):
 
1106
        index, reload_counter = self.make_index_with_missing_children()
 
1107
        self.assertEqual(2, index.key_count())
 
1108
        self.assertEqual([1, 1, 0], reload_counter)
 
1109
 
 
1110
    def test_key_count_reloads_and_fails(self):
 
1111
        index, reload_counter = self.make_index_with_missing_children()
 
1112
        # We have deleted the underlying index, so we will try to reload, but
 
1113
        # still fail. This is mostly to test we don't get stuck in an infinite
 
1114
        # loop trying to reload
 
1115
        self.get_transport().delete('3')
 
1116
        self.assertRaises(errors.NoSuchFile, index.key_count)
 
1117
        self.assertEqual([2, 1, 1], reload_counter)
 
1118
 
1073
1119
 
1074
1120
class TestInMemoryGraphIndex(TestCaseWithMemoryTransport):
1075
1121