~launchpad-pqm/launchpad/devel

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
# Copyright 2011 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Test `TransactionPolicy`."""

__metaclass__ = type

from psycopg2 import InternalError
import transaction

from lp.registry.model.person import Person
from lp.services.database.isolation import (
    check_no_transaction,
    TransactionInProgress,
    )
from lp.services.database.lpstorm import IStore
from lp.services.database.transaction_policy import DatabaseTransactionPolicy
from lp.testing import TestCaseWithFactory
from lp.testing.layers import ZopelessDatabaseLayer


class TestTransactionPolicy(TestCaseWithFactory):
    layer = ZopelessDatabaseLayer

    def setUp(self):
        super(TestTransactionPolicy, self).setUp()
        # Start each test with a clean slate: no ongoing transaction
        # unless the test says so.
        transaction.abort()

    def readFromDatabase(self):
        """Perform a database read."""
        IStore(Person).find(Person, Person.name == "arbitrary").first()

    def writeToDatabase(self):
        """Write an object to the database.

        :return: A token that `hasDatabaseBeenWrittenTo` can look for.
        """
        name = self.factory.getUniqueString()
        self.factory.makePerson(name=name)
        return name

    def writeToDatabaseAndCommit(self):
        """Write an object to the database and commit.

        :return: A token that `hasDatabaseBeenWrittenTo` can look for.
        """
        name = self.writeToDatabase()
        transaction.commit()
        return name

    def hasDatabaseBeenWrittenTo(self, test_token):
        """Is the object made by `writeToDatabase` present in the database?

        :param test_token: The return value from `writeToDatabase`.
        :return: Has the change represented by `test_token` been made to the
            database?
        """
        query = IStore(Person).find(Person, Person.name == test_token)
        return query.one() is not None

    def test_can_be_empty(self):
        # An empty transaction policy works fine.
        with DatabaseTransactionPolicy():
            pass
        # You still end up with a clean slate, transaction-wise.
        check_no_transaction()

    def test_writable_permits_updates(self):
        # Writes to the database work just fine in a non-read-only
        # policy.
        with DatabaseTransactionPolicy(read_only=False):
            self.writeToDatabase()
            transaction.commit()
        # The test is that we get here without failure.
        pass

    def test_readonly_forbids_updates(self):
        # A read-only policy forbids writes to the database.
        def make_forbidden_update():
            with DatabaseTransactionPolicy(read_only=True):
                self.writeToDatabase()
                transaction.commit()

        self.assertRaises(InternalError, make_forbidden_update)

    def test_will_not_go_read_only_when_read_write_transaction_ongoing(self):
        # You cannot enter a read-only DatabaseTransactionPolicy while in an
        # active read-write transaction.
        def enter_policy():
            with DatabaseTransactionPolicy(read_only=True):
                pass

        self.writeToDatabase()
        self.assertRaises(TransactionInProgress, enter_policy)

    def test_will_go_read_write_when_read_write_transaction_ongoing(self):
        # You can enter a read-write DatabaseTransactionPolicy while in an
        # active read-write transaction.
        def enter_policy():
            with DatabaseTransactionPolicy(read_only=False):
                pass

        self.writeToDatabase()
        enter_policy()  # No exception.

    def test_successful_exit_requires_commit_or_abort(self):
        # If a read-write policy exits normally (which would probably
        # indicate successful completion of its code), it requires that
        # any ongoing transaction be committed or aborted first.
        test_token = None

        def leave_transaction_open():
            with DatabaseTransactionPolicy(read_only=False):
                self.writeToDatabase()

        self.assertRaises(TransactionInProgress, leave_transaction_open)
        # As a side effect of the error, the transaction is rolled back.
        self.assertFalse(self.hasDatabaseBeenWrittenTo(test_token))

    def test_successful_read_only_exit_commits_or_aborts_implicitly(self):
        # When a read-only policy exits normally, there is no need to
        # complete the transaction.  It aborts or commits implicitly.
        # (The choice is up to the implementation; the point of
        # read-only is that you can't tell the difference.)
        with DatabaseTransactionPolicy(read_only=True):
            self.readFromDatabase()

        # No transaction ongoing at this point.
        check_no_transaction()

    def test_aborts_on_failure(self):
        # If the context handler exits with an exception, it aborts the
        # transaction.
        class CompleteFailure(Exception):
            pass

        try:
            with DatabaseTransactionPolicy(read_only=False):
                test_token = self.writeToDatabase()
                raise CompleteFailure()
        except CompleteFailure:
            pass

        # No transaction ongoing at this point.
        check_no_transaction()
        # The change has rolled back.
        self.assertFalse(self.hasDatabaseBeenWrittenTo(test_token))

    def test_nested_policy_overrides_previous_policy(self):
        # When one policy is nested in another, the nested one
        # determines what is allowed.
        def allows_updates(read_only=True):
            """Does the given policy permit database updates?"""
            try:
                with DatabaseTransactionPolicy(read_only=read_only):
                    self.writeToDatabase()
                    transaction.commit()
                return True
            except InternalError:
                return False

        # Map (previous policy, nested policy) to whether the
        # combination makes the store read-only.
        effects = {}

        for previous_policy in [False, True]:
            for nested_policy in [False, True]:
                experiment = (previous_policy, nested_policy)
                with DatabaseTransactionPolicy(read_only=previous_policy):
                    is_read_only = not allows_updates(nested_policy)
                    effects[experiment] = is_read_only

        # Only the nested policy (the second element of the key tuple)
        # determines whether writes are allowed (the value associated
        # with the key).
        self.assertEqual({
            (False, False): False,
            (False, True): True,
            (True, False): False,
            (True, True): True,
            },
            effects)

    def test_policy_restores_previous_policy_on_success(self):
        # A transaction policy, once exited, restores the previously
        # applicable policy.
        with DatabaseTransactionPolicy(read_only=False):
            with DatabaseTransactionPolicy(read_only=True):
                self.readFromDatabase()
            self.assertTrue(
                self.hasDatabaseBeenWrittenTo(self.writeToDatabase()))
            transaction.commit()
        self.assertTrue(
            self.hasDatabaseBeenWrittenTo(self.writeToDatabase()))

    def test_propagates_failure(self):
        # Exceptions raised inside a transaction policy are not
        # swallowed.
        class Kaboom(Exception):
            pass

        def fail_within_policy():
            with DatabaseTransactionPolicy():
                raise Kaboom()

        self.assertRaises(Kaboom, fail_within_policy)

    def test_policy_restores_previous_policy_on_failure(self):
        # A transaction policy restores the previously applicable policy
        # even when it exits abnormally.
        class HorribleFailure(Exception):
            pass

        try:
            with DatabaseTransactionPolicy(read_only=True):
                raise HorribleFailure()
        except HorribleFailure:
            pass

        self.assertTrue(
            self.hasDatabaseBeenWrittenTo(self.writeToDatabase()))

    def test_policy_can_span_transactions(self):
        # It's okay to commit within a policy; the policy will still
        # apply to the next transaction inside the same policy.
        test_token = self.writeToDatabase()
        transaction.commit()

        with DatabaseTransactionPolicy(read_only=True):
            self.hasDatabaseBeenWrittenTo(test_token)
            transaction.commit()
            self.assertRaises(InternalError, self.writeToDatabaseAndCommit)
            transaction.abort()

    def test_policy_survives_abort(self):
        # Even after aborting the initial transaction, a transaction
        # policy still applies.
        with DatabaseTransactionPolicy(read_only=True):
            self.readFromDatabase()
            transaction.abort()
            self.assertRaises(InternalError, self.writeToDatabaseAndCommit)
            transaction.abort()

    def test_readOnly(self):
        # DatabaseTransactionPolicy.readOnly() is a function decorator that
        # applies a read-only policy for the duration of the function call.
        writeToDatabaseAndCommit = DatabaseTransactionPolicy.readOnly(
            self.writeToDatabaseAndCommit)
        with DatabaseTransactionPolicy(read_only=False):
            self.assertRaises(InternalError, writeToDatabaseAndCommit)

    def test_readWrite(self):
        # DatabaseTransactionPolicy.readWrite() is a function decorator that
        # applies a read-write policy for the duration of the function call.
        writeToDatabaseAndCommit = DatabaseTransactionPolicy.readWrite(
            self.writeToDatabaseAndCommit)
        with DatabaseTransactionPolicy(read_only=True):
            writeToDatabaseAndCommit()  # No exception.