summaryrefslogtreecommitdiffstats
path: root/tests/test_tpc_async.py
blob: a409a2e82b4cb6665a6297ac8df8d5e2fec7bef4 (plain)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import pytest

import psycopg
from psycopg.pq import TransactionStatus

pytestmark = [
    pytest.mark.asyncio,
    pytest.mark.crdb_skip("2-phase commit"),
]


async def test_tpc_disabled(aconn, apipeline):
    cur = await aconn.execute("show max_prepared_transactions")
    val = int((await cur.fetchone())[0])
    if val:
        pytest.skip("prepared transactions enabled")

    await aconn.rollback()
    await aconn.tpc_begin("x")
    with pytest.raises(psycopg.NotSupportedError):
        await aconn.tpc_prepare()


class TestTPC:
    async def test_tpc_commit(self, aconn, tpc):
        xid = aconn.xid(1, "gtrid", "bqual")
        assert aconn.info.transaction_status == TransactionStatus.IDLE

        await aconn.tpc_begin(xid)
        assert aconn.info.transaction_status == TransactionStatus.INTRANS

        cur = aconn.cursor()
        await cur.execute("insert into test_tpc values ('test_tpc_commit')")
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

        await aconn.tpc_prepare()
        assert aconn.info.transaction_status == TransactionStatus.IDLE
        assert tpc.count_xacts() == 1
        assert tpc.count_test_records() == 0

        await aconn.tpc_commit()
        assert aconn.info.transaction_status == TransactionStatus.IDLE
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 1

    async def test_tpc_commit_one_phase(self, aconn, tpc):
        xid = aconn.xid(1, "gtrid", "bqual")
        assert aconn.info.transaction_status == TransactionStatus.IDLE

        await aconn.tpc_begin(xid)
        assert aconn.info.transaction_status == TransactionStatus.INTRANS

        cur = aconn.cursor()
        await cur.execute("insert into test_tpc values ('test_tpc_commit_1p')")
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

        await aconn.tpc_commit()
        assert aconn.info.transaction_status == TransactionStatus.IDLE
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 1

    async def test_tpc_commit_recovered(self, aconn_cls, aconn, dsn, tpc):
        xid = aconn.xid(1, "gtrid", "bqual")
        assert aconn.info.transaction_status == TransactionStatus.IDLE

        await aconn.tpc_begin(xid)
        assert aconn.info.transaction_status == TransactionStatus.INTRANS

        cur = aconn.cursor()
        await cur.execute("insert into test_tpc values ('test_tpc_commit_rec')")
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

        await aconn.tpc_prepare()
        await aconn.close()
        assert tpc.count_xacts() == 1
        assert tpc.count_test_records() == 0

        async with await aconn_cls.connect(dsn) as aconn:
            xid = aconn.xid(1, "gtrid", "bqual")
            await aconn.tpc_commit(xid)
            assert aconn.info.transaction_status == TransactionStatus.IDLE

        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 1

    async def test_tpc_rollback(self, aconn, tpc):
        xid = aconn.xid(1, "gtrid", "bqual")
        assert aconn.info.transaction_status == TransactionStatus.IDLE

        await aconn.tpc_begin(xid)
        assert aconn.info.transaction_status == TransactionStatus.INTRANS

        cur = aconn.cursor()
        await cur.execute("insert into test_tpc values ('test_tpc_rollback')")
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

        await aconn.tpc_prepare()
        assert aconn.info.transaction_status == TransactionStatus.IDLE
        assert tpc.count_xacts() == 1
        assert tpc.count_test_records() == 0

        await aconn.tpc_rollback()
        assert aconn.info.transaction_status == TransactionStatus.IDLE
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

    async def test_tpc_rollback_one_phase(self, aconn, tpc):
        xid = aconn.xid(1, "gtrid", "bqual")
        assert aconn.info.transaction_status == TransactionStatus.IDLE

        await aconn.tpc_begin(xid)
        assert aconn.info.transaction_status == TransactionStatus.INTRANS

        cur = aconn.cursor()
        await cur.execute("insert into test_tpc values ('test_tpc_rollback_1p')")
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

        await aconn.tpc_rollback()
        assert aconn.info.transaction_status == TransactionStatus.IDLE
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

    async def test_tpc_rollback_recovered(self, aconn_cls, aconn, dsn, tpc):
        xid = aconn.xid(1, "gtrid", "bqual")
        assert aconn.info.transaction_status == TransactionStatus.IDLE

        await aconn.tpc_begin(xid)
        assert aconn.info.transaction_status == TransactionStatus.INTRANS

        cur = aconn.cursor()
        await cur.execute("insert into test_tpc values ('test_tpc_commit_rec')")
        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

        await aconn.tpc_prepare()
        await aconn.close()
        assert tpc.count_xacts() == 1
        assert tpc.count_test_records() == 0

        async with await aconn_cls.connect(dsn) as aconn:
            xid = aconn.xid(1, "gtrid", "bqual")
            await aconn.tpc_rollback(xid)
            assert aconn.info.transaction_status == TransactionStatus.IDLE

        assert tpc.count_xacts() == 0
        assert tpc.count_test_records() == 0

    async def test_status_after_recover(self, aconn, tpc):
        assert aconn.info.transaction_status == TransactionStatus.IDLE
        await aconn.tpc_recover()
        assert aconn.info.transaction_status == TransactionStatus.IDLE

        cur = aconn.cursor()
        await cur.execute("select 1")
        assert aconn.info.transaction_status == TransactionStatus.INTRANS
        await aconn.tpc_recover()
        assert aconn.info.transaction_status == TransactionStatus.INTRANS

    async def test_recovered_xids(self, aconn, tpc):
        # insert a few test xns
        await aconn.set_autocommit(True)
        cur = aconn.cursor()
        await cur.execute("begin; prepare transaction '1-foo'")
        await cur.execute("begin; prepare transaction '2-bar'")

        # read the values to return
        await cur.execute(
            """
            select gid, prepared, owner, database from pg_prepared_xacts
            where database = %s
            """,
            (aconn.info.dbname,),
        )
        okvals = await cur.fetchall()
        okvals.sort()

        xids = await aconn.tpc_recover()
        xids = [xid for xid in xids if xid.database == aconn.info.dbname]
        xids.sort(key=lambda x: x.gtrid)

        # check the values returned
        assert len(okvals) == len(xids)
        for (xid, (gid, prepared, owner, database)) in zip(xids, okvals):
            assert xid.gtrid == gid
            assert xid.prepared == prepared
            assert xid.owner == owner
            assert xid.database == database

    async def test_xid_encoding(self, aconn, tpc):
        xid = aconn.xid(42, "gtrid", "bqual")
        await aconn.tpc_begin(xid)
        await aconn.tpc_prepare()

        cur = aconn.cursor()
        await cur.execute(
            "select gid from pg_prepared_xacts where database = %s",
            (aconn.info.dbname,),
        )
        assert "42_Z3RyaWQ=_YnF1YWw=" == (await cur.fetchone())[0]

    @pytest.mark.parametrize(
        "fid, gtrid, bqual",
        [
            (0, "", ""),
            (42, "gtrid", "bqual"),
            (0x7FFFFFFF, "x" * 64, "y" * 64),
        ],
    )
    async def test_xid_roundtrip(self, aconn_cls, aconn, dsn, tpc, fid, gtrid, bqual):
        xid = aconn.xid(fid, gtrid, bqual)
        await aconn.tpc_begin(xid)
        await aconn.tpc_prepare()
        await aconn.close()

        async with await aconn_cls.connect(dsn) as aconn:
            xids = [
                x for x in await aconn.tpc_recover() if x.database == aconn.info.dbname
            ]
            assert len(xids) == 1
            xid = xids[0]
            await aconn.tpc_rollback(xid)

        assert xid.format_id == fid
        assert xid.gtrid == gtrid
        assert xid.bqual == bqual

    @pytest.mark.parametrize(
        "tid",
        [
            "",
            "hello, world!",
            "x" * 199,  # PostgreSQL's limit in transaction id length
        ],
    )
    async def test_unparsed_roundtrip(self, aconn_cls, aconn, dsn, tpc, tid):
        await aconn.tpc_begin(tid)
        await aconn.tpc_prepare()
        await aconn.close()

        async with await aconn_cls.connect(dsn) as aconn:
            xids = [
                x for x in await aconn.tpc_recover() if x.database == aconn.info.dbname
            ]
            assert len(xids) == 1
            xid = xids[0]
            await aconn.tpc_rollback(xid)

        assert xid.format_id is None
        assert xid.gtrid == tid
        assert xid.bqual is None

    async def test_xid_unicode(self, aconn_cls, aconn, dsn, tpc):
        x1 = aconn.xid(10, "uni", "code")
        await aconn.tpc_begin(x1)
        await aconn.tpc_prepare()
        await aconn.close()

        async with await aconn_cls.connect(dsn) as aconn:
            xid = [
                x for x in await aconn.tpc_recover() if x.database == aconn.info.dbname
            ][0]

        assert 10 == xid.format_id
        assert "uni" == xid.gtrid
        assert "code" == xid.bqual

    async def test_xid_unicode_unparsed(self, aconn_cls, aconn, dsn, tpc):
        # We don't expect people shooting snowmen as transaction ids,
        # so if something explodes in an encode error I don't mind.
        # Let's just check unicode is accepted as type.
        await aconn.execute("set client_encoding to utf8")
        await aconn.commit()

        await aconn.tpc_begin("transaction-id")
        await aconn.tpc_prepare()
        await aconn.close()

        async with await aconn_cls.connect(dsn) as aconn:
            xid = [
                x for x in await aconn.tpc_recover() if x.database == aconn.info.dbname
            ][0]

        assert xid.format_id is None
        assert xid.gtrid == "transaction-id"
        assert xid.bqual is None

    async def test_cancel_fails_prepared(self, aconn, tpc):
        await aconn.tpc_begin("cancel")
        await aconn.tpc_prepare()
        with pytest.raises(psycopg.ProgrammingError):
            aconn.cancel()

    async def test_tpc_recover_non_dbapi_connection(self, aconn_cls, aconn, dsn, tpc):
        aconn.row_factory = psycopg.rows.dict_row
        await aconn.tpc_begin("dict-connection")
        await aconn.tpc_prepare()
        await aconn.close()

        async with await aconn_cls.connect(dsn) as aconn:
            xids = await aconn.tpc_recover()
            xid = [x for x in xids if x.database == aconn.info.dbname][0]

        assert xid.format_id is None
        assert xid.gtrid == "dict-connection"
        assert xid.bqual is None