@@ -171,6 +171,59 @@ func TestKillConnection(t *testing.T) {
171
171
require .False (t , killed [2 ])
172
172
}
173
173
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
+
174
227
// TestSlowQueryTracking tests that processes that take longer than @@long_query_time increment the
175
228
// Slow_queries status variable.
176
229
func TestSlowQueryTracking (t * testing.T ) {
0 commit comments