~bzr-pqm/bzr/bzr.dev

5218.2.2 by John Arbash Meinel
Bring in the global chk change, which includes some more bzr.dev code.
1
# Copyright (C) 2009, 2010 Canonical Ltd
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
2
#
3
# This program is free software; you can redistribute it and/or modify
4
# it under the terms of the GNU General Public License as published by
5
# the Free Software Foundation; either version 2 of the License, or
6
# (at your option) any later version.
7
#
8
# This program is distributed in the hope that it will be useful,
9
# but WITHOUT ANY WARRANTY; without even the implied warranty of
10
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11
# GNU General Public License for more details.
12
#
13
# You should have received a copy of the GNU General Public License
14
# along with this program; if not, write to the Free Software
4183.7.1 by Sabin Iacob
update FSF mailing address
15
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
16
17
"""Tests for Repository.refresh_data."""
18
19
from bzrlib import (
20
    errors,
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
21
    repository,
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
22
    )
23
from bzrlib.tests import TestSkipped
24
from bzrlib.tests.per_repository import TestCaseWithRepository
25
26
27
class TestRefreshData(TestCaseWithRepository):
28
29
    def test_refresh_data_unlocked(self):
30
        # While not interesting, it should not error.
31
        repo = self.make_repository('.')
32
        repo.refresh_data()
33
34
    def test_refresh_data_read_locked(self):
35
        # While not interesting, it should not error.
36
        repo = self.make_repository('.')
37
        repo.lock_read()
38
        self.addCleanup(repo.unlock)
39
        repo.refresh_data()
40
41
    def test_refresh_data_write_locked(self):
42
        # While not interesting, it should not error.
43
        repo = self.make_repository('.')
44
        repo.lock_write()
45
        self.addCleanup(repo.unlock)
46
        repo.refresh_data()
47
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
48
    def test_refresh_data_in_write_group(self):
49
        # refresh_data may either succeed or raise IsInWriteGroupError during a
50
        # write group.
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
51
        repo = self.make_repository('.')
52
        repo.lock_write()
53
        self.addCleanup(repo.unlock)
54
        repo.start_write_group()
55
        self.addCleanup(repo.abort_write_group)
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
56
        try:
57
            repo.refresh_data()
58
        except repository.IsInWriteGroupError:
59
            # This is ok.
60
            pass
61
        else:
62
            # This is ok too.
63
            pass
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
64
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
65
    def fetch_new_revision_into_concurrent_instance(self, repo, token):
66
        """Create a new revision (revid 'new-rev') and fetch it into a
67
        concurrent instance of repo.
68
        """
5199.1.2 by Andrew Bennetts
Expand test to use repo.all_revision_ids(), and use make_branch_and_memory_tree instead of make_branch_and_tree.
69
        source = self.make_branch_and_memory_tree('source')
70
        source.lock_write()
71
        self.addCleanup(source.unlock)
72
        source.add([''], ['root-id'])
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
73
        revid = source.commit('foo', rev_id='new-rev')
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
74
        # Force data reading on weaves/knits
5199.1.2 by Andrew Bennetts
Expand test to use repo.all_revision_ids(), and use make_branch_and_memory_tree instead of make_branch_and_tree.
75
        repo.all_revision_ids()
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
76
        repo.revisions.keys()
77
        repo.inventories.keys()
78
        # server repo is the instance a smart server might hold for this
79
        # repository.
80
        server_repo = repo.bzrdir.open_repository()
81
        try:
82
            server_repo.lock_write(token)
83
        except errors.TokenLockingNotSupported:
84
            raise TestSkipped('Cannot concurrently insert into repo format %r'
85
                % self.repository_format)
86
        try:
87
            server_repo.fetch(source.branch.repository, revid)
88
        finally:
89
            server_repo.unlock()
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
90
91
    def test_refresh_data_after_fetch_new_data_visible(self):
92
        repo = self.make_repository('target')
5200.3.7 by Robert Collins
Merge trunk - resolve conflicts.
93
        token = repo.lock_write().repository_token
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
94
        self.addCleanup(repo.unlock)
95
        self.fetch_new_revision_into_concurrent_instance(repo, token)
4145.1.2 by Robert Collins
Add a refresh_data method on Repository allowing cleaner handling of insertions into RemoteRepository objects with _real_repository instances.
96
        repo.refresh_data()
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
97
        self.assertNotEqual({}, repo.get_graph().get_parent_map(['new-rev']))
98
99
    def test_refresh_data_after_fetch_new_data_visible_in_write_group(self):
5199.1.2 by Andrew Bennetts
Expand test to use repo.all_revision_ids(), and use make_branch_and_memory_tree instead of make_branch_and_tree.
100
        tree = self.make_branch_and_memory_tree('target')
101
        tree.lock_write()
102
        self.addCleanup(tree.unlock)
103
        tree.add([''], ['root-id'])
104
        tree.commit('foo', rev_id='commit-in-target')
105
        repo = tree.branch.repository
5200.3.8 by Robert Collins
Update one new test.
106
        token = repo.lock_write().repository_token
5199.1.1 by Andrew Bennetts
Allow repositories to support refresh_data during a write group.
107
        self.addCleanup(repo.unlock)
108
        repo.start_write_group()
109
        self.addCleanup(repo.abort_write_group)
110
        self.fetch_new_revision_into_concurrent_instance(repo, token)
111
        # Call refresh_data.  It either fails with IsInWriteGroupError, or it
112
        # succeeds and the new revisions are visible.
113
        try:
114
            repo.refresh_data()
115
        except repository.IsInWriteGroupError:
116
            pass
117
        else:
5199.1.2 by Andrew Bennetts
Expand test to use repo.all_revision_ids(), and use make_branch_and_memory_tree instead of make_branch_and_tree.
118
            self.assertEqual(
119
                ['commit-in-target', 'new-rev'],
120
                sorted(repo.all_revision_ids()))