forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstepped.go
256 lines (216 loc) · 6.91 KB
/
stepped.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
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
// Copyright 2022 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,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package testkit
import (
"testing"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/failpoint"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/util/breakpoint"
"github.com/stretchr/testify/require"
)
var errCommandRunFailed = errors.New("command run failed")
var defaultChanTimeout = time.Second * 10
type steppedTestkitMsgType int
const (
msgTpCmdContinue steppedTestkitMsgType = iota
msgTpCmdStopOnBreakPoint
msgTpCmdDone
)
type steppedTestKitMsg struct {
tp steppedTestkitMsgType
val any
}
type steppedTestKitMsgChan chan *steppedTestKitMsg
func (ch steppedTestKitMsgChan) sendMsg(tp steppedTestkitMsgType, val any) error {
select {
case ch <- &steppedTestKitMsg{tp: tp, val: val}:
return nil
case <-time.After(defaultChanTimeout):
return errors.New("send msg timeout")
}
}
func (ch steppedTestKitMsgChan) sendMsgWithType(tp steppedTestkitMsgType) error {
return ch.sendMsg(tp, nil)
}
func (ch steppedTestKitMsgChan) recvMsg() (*steppedTestKitMsg, error) {
select {
case msg := <-ch:
return msg, nil
case <-time.After(defaultChanTimeout):
return nil, errors.New("send msg timeout")
}
}
func (ch steppedTestKitMsgChan) recvMsgWithCheck(tp steppedTestkitMsgType) (*steppedTestKitMsg, error) {
msg, err := ch.recvMsg()
if err != nil {
return nil, err
}
if msg.tp != tp {
return nil, errors.Errorf("unexpected msg type: %v, expect: %v", msg.tp, tp)
}
return msg, nil
}
type steppedTestKitCommandContext struct {
t testing.TB
tk *TestKit
notifyBreakPointAndWait func(string)
}
type steppedTestKitCommand func(ctx *steppedTestKitCommandContext) any
// SteppedTestKit is the testkit that can run stepped command
type SteppedTestKit struct {
t testing.TB
tk *TestKit
// ch1 is used to send msg from foreground to background
ch1 steppedTestKitMsgChan
// ch2 is used to send msg from background to foreground
ch2 steppedTestKitMsgChan
// breakPoints is the break points we want to stop at
breakPoints []string
// cmdStopAt is the current break point it stopped at
cmdStopAt string
// the result of the current command
cmdResult any
}
// NewSteppedTestKit creates a new SteppedTestKit
func NewSteppedTestKit(t testing.TB, store kv.Storage) *SteppedTestKit {
tk := &SteppedTestKit{
t: t,
tk: NewTestKit(t, store),
ch1: make(steppedTestKitMsgChan),
ch2: make(steppedTestKitMsgChan),
}
return tk
}
// ExpectIdle checks no command is running
func (tk *SteppedTestKit) ExpectIdle() {
require.Equal(tk.t, "", tk.cmdStopAt)
}
// ExpectStopOnBreakPoint checks stopped on the specified break point
func (tk *SteppedTestKit) ExpectStopOnBreakPoint(breakPoint string) {
require.Equal(tk.t, breakPoint, tk.cmdStopAt)
}
// ExpectStopOnAnyBreakPoint checks stopped on any break point
func (tk *SteppedTestKit) ExpectStopOnAnyBreakPoint() {
require.NotEqual(tk.t, "", tk.cmdStopAt)
}
// SetBreakPoints sets the break points we want to stop at
func (tk *SteppedTestKit) SetBreakPoints(breakPoints ...string) {
tk.breakPoints = breakPoints
}
func (tk *SteppedTestKit) handleCommandMsg() {
msg, err := tk.ch2.recvMsg()
require.NoError(tk.t, err)
switch msg.tp {
case msgTpCmdDone:
tk.cmdStopAt = ""
if msg.val == errCommandRunFailed {
require.FailNow(tk.t, "internal command failed")
} else {
tk.cmdResult = msg.val
}
case msgTpCmdStopOnBreakPoint:
require.IsType(tk.t, "", msg.val)
require.NotEqual(tk.t, "", msg.val)
tk.cmdStopAt = msg.val.(string)
default:
require.FailNow(tk.t, "invalid msg type", "tp %v", msg.tp)
}
}
func (tk *SteppedTestKit) beforeCommand() {
tk.ExpectIdle()
tk.cmdResult = nil
}
func (tk *SteppedTestKit) steppedCommand(cmd steppedTestKitCommand) *SteppedTestKit {
tk.beforeCommand()
go func() {
var success bool
var result any
var breakPointPaths []string
defer func() {
if !success {
result = errCommandRunFailed
}
tk.tk.Session().SetValue(breakpoint.NotifyBreakPointFuncKey, nil)
for _, path := range breakPointPaths {
require.NoError(tk.t, failpoint.Disable(path))
}
require.NoError(tk.t, tk.ch2.sendMsg(msgTpCmdDone, result))
}()
ctx := &steppedTestKitCommandContext{
t: tk.t,
tk: tk.tk,
notifyBreakPointAndWait: func(breakPoint string) {
require.NoError(tk.t, tk.ch2.sendMsg(msgTpCmdStopOnBreakPoint, breakPoint))
_, err := tk.ch1.recvMsgWithCheck(msgTpCmdContinue)
require.NoError(tk.t, err)
},
}
tk.tk.Session().SetValue(breakpoint.NotifyBreakPointFuncKey, ctx.notifyBreakPointAndWait)
for _, breakPoint := range tk.breakPoints {
path := "github.com/pingcap/tidb/util/breakpoint/" + breakPoint
require.NoError(tk.t, failpoint.Enable(path, "return"))
breakPointPaths = append(breakPointPaths, path)
}
result = cmd(ctx)
success = true
}()
tk.handleCommandMsg()
return tk
}
// Continue continues current command
func (tk *SteppedTestKit) Continue() *SteppedTestKit {
tk.ExpectStopOnAnyBreakPoint()
require.NoError(tk.t, tk.ch1.sendMsgWithType(msgTpCmdContinue))
tk.handleCommandMsg()
return tk
}
// SteppedMustExec creates a new stepped task for MustExec
func (tk *SteppedTestKit) SteppedMustExec(sql string, args ...interface{}) *SteppedTestKit {
return tk.steppedCommand(func(_ *steppedTestKitCommandContext) any {
tk.MustExec(sql, args...)
return nil
})
}
// SteppedMustQuery creates a new stepped task for MustQuery
func (tk *SteppedTestKit) SteppedMustQuery(sql string, args ...interface{}) *SteppedTestKit {
return tk.steppedCommand(func(_ *steppedTestKitCommandContext) any {
return tk.MustQuery(sql, args...)
})
}
// MustExec executes a sql statement and asserts nil error.
func (tk *SteppedTestKit) MustExec(sql string, args ...interface{}) {
tk.beforeCommand()
tk.tk.MustExec(sql, args...)
}
// MustQuery query the statements and returns result rows.
// If expected result is set it asserts the query result equals expected result.
func (tk *SteppedTestKit) MustQuery(sql string, args ...interface{}) *Result {
tk.beforeCommand()
result := tk.tk.MustQuery(sql, args...)
tk.cmdResult = result
return result
}
// GetResult returns the result of the latest command
func (tk *SteppedTestKit) GetResult() any {
tk.ExpectIdle()
return tk.cmdResult
}
// GetQueryResult returns the query result of the latest command
func (tk *SteppedTestKit) GetQueryResult() *Result {
tk.ExpectIdle()
require.IsType(tk.t, &Result{}, tk.cmdResult)
return tk.cmdResult.(*Result)
}