Skip to content

Commit 6887d52

Browse files
committed
processlist: Add some tests for Begin/End Operation.
1 parent e992aa5 commit 6887d52

File tree

1 file changed

+53
-0
lines changed

1 file changed

+53
-0
lines changed

processlist_test.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,59 @@ func TestKillConnection(t *testing.T) {
171171
require.False(t, killed[2])
172172
}
173173

174+
func TestBeginEndOperation(t *testing.T) {
175+
knownSession := sql.NewBaseSessionWithClientServer("", sql.Client{}, 1)
176+
unknownSession := sql.NewBaseSessionWithClientServer("", sql.Client{}, 2)
177+
178+
pl := NewProcessList()
179+
pl.AddConnection(1, "")
180+
181+
// Begining an operation with an unknown connection returns an error.
182+
ctx := sql.NewContext(context.Background(), sql.WithSession(unknownSession))
183+
_, err := pl.BeginOperation(ctx)
184+
require.Error(t, err)
185+
186+
// Can begin and end operation before connection is ready.
187+
ctx = sql.NewContext(context.Background(), sql.WithSession(knownSession))
188+
subCtx, err := pl.BeginOperation(ctx)
189+
require.NoError(t, err)
190+
pl.EndOperation(subCtx)
191+
192+
// Can begin and end operation across the connection ready boundary.
193+
subCtx, err = pl.BeginOperation(ctx)
194+
require.NoError(t, err)
195+
pl.ConnectionReady(knownSession)
196+
pl.EndOperation(subCtx)
197+
198+
// Ending the operation cancels the subcontext.
199+
subCtx, err = pl.BeginOperation(ctx)
200+
require.NoError(t, err)
201+
done := make(chan struct{})
202+
context.AfterFunc(subCtx, func() {
203+
close(done)
204+
})
205+
pl.EndOperation(subCtx)
206+
<-done
207+
208+
// Kill on the connection cancels the subcontext.
209+
subCtx, err = pl.BeginOperation(ctx)
210+
require.NoError(t, err)
211+
done = make(chan struct{})
212+
context.AfterFunc(subCtx, func() {
213+
close(done)
214+
})
215+
pl.Kill(1)
216+
<-done
217+
pl.EndOperation(subCtx)
218+
219+
// Beginning an operation while one is outstanding errors.
220+
subCtx, err = pl.BeginOperation(ctx)
221+
require.NoError(t, err)
222+
_, err = pl.BeginOperation(ctx)
223+
require.Error(t, err)
224+
pl.EndOperation(subCtx)
225+
}
226+
174227
// TestSlowQueryTracking tests that processes that take longer than @@long_query_time increment the
175228
// Slow_queries status variable.
176229
func TestSlowQueryTracking(t *testing.T) {

0 commit comments

Comments
 (0)