-
Notifications
You must be signed in to change notification settings - Fork 526
/
session_fail_test.go
115 lines (92 loc) · 4.13 KB
/
session_fail_test.go
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
// Copyright 2018 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package session_test
import (
"context"
. "github.com/pingcap/check"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/util/testkit"
)
func (s *testSessionSuite2) TestFailStatementCommit(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t (id int)")
tk.MustExec("begin")
tk.MustExec("insert into t values (1)")
c.Assert(failpoint.Enable("github.com/pingcap/tidb/session/mockStmtCommitError", `return(true)`), IsNil)
_, err := tk.Exec("insert into t values (2),(3),(4),(5)")
c.Assert(err, NotNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/session/mockStmtCommitError"), IsNil)
_, err = tk.Exec("select * from t")
c.Assert(err, NotNil)
_, err = tk.Exec("insert into t values (3)")
c.Assert(err, NotNil)
_, err = tk.Exec("insert into t values (4)")
c.Assert(err, NotNil)
_, err = tk.Exec("commit")
c.Assert(err, NotNil)
tk.MustQuery(`select * from t`).Check(testkit.Rows())
tk.MustExec("insert into t values (1)")
tk.MustExec("begin")
tk.MustExec("insert into t values (2)")
tk.MustExec("commit")
tk.MustExec("begin")
tk.MustExec("insert into t values (3)")
tk.MustExec("rollback")
tk.MustQuery(`select * from t`).Check(testkit.Rows("1", "2"))
}
func (s *testSessionSuite2) TestFailStatementCommitInRetry(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t (id int)")
tk.MustExec("begin")
tk.MustExec("insert into t values (1)")
tk.MustExec("insert into t values (2),(3),(4),(5)")
tk.MustExec("insert into t values (6)")
c.Assert(failpoint.Enable("github.com/pingcap/tidb/session/mockCommitError8942", `return(true)`), IsNil)
c.Assert(failpoint.Enable("github.com/pingcap/tidb/session/mockStmtCommitError", `return(true)`), IsNil)
_, err := tk.Exec("commit")
c.Assert(err, NotNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/session/mockCommitError8942"), IsNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/session/mockStmtCommitError"), IsNil)
tk.MustExec("insert into t values (6)")
tk.MustQuery(`select * from t`).Check(testkit.Rows("6"))
}
func (s *testSessionSuite2) TestGetTSFailDirtyState(c *C) {
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t (id int)")
c.Assert(failpoint.Enable("github.com/pingcap/tidb/session/mockGetTSFail", "return"), IsNil)
ctx := failpoint.WithHook(context.Background(), func(ctx context.Context, fpname string) bool {
return fpname == "github.com/pingcap/tidb/session/mockGetTSFail"
})
_, err := tk.Se.Execute(ctx, "select * from t")
c.Assert(err, NotNil)
// Fix a bug that active txn fail set TxnState.fail to error, and then the following write
// affected by this fail flag.
tk.MustExec("insert into t values (1)")
tk.MustQuery(`select * from t`).Check(testkit.Rows("1"))
c.Assert(failpoint.Disable("github.com/pingcap/tidb/session/mockGetTSFail"), IsNil)
}
func (s *testSessionSuite2) TestGetTSFailDirtyStateInretry(c *C) {
defer func() {
c.Assert(failpoint.Disable("github.com/pingcap/tidb/session/mockCommitError"), IsNil)
c.Assert(failpoint.Disable("github.com/pingcap/tidb/store/tikv/mockGetTSErrorInRetry"), IsNil)
}()
tk := testkit.NewTestKitWithInit(c, s.store)
tk.MustExec("create table t (id int)")
c.Assert(failpoint.Enable("github.com/pingcap/tidb/session/mockCommitError", `return(true)`), IsNil)
// This test will mock a PD timeout error, and recover then.
// Just make mockGetTSErrorInRetry return true once, and then return false.
c.Assert(failpoint.Enable("github.com/pingcap/tidb/store/tikv/mockGetTSErrorInRetry",
`1*return(true)->return(false)`), IsNil)
tk.MustExec("insert into t values (2)")
tk.MustQuery(`select * from t`).Check(testkit.Rows("2"))
}