@@ -21,13 +21,26 @@ Functionality of this Executor is tested in go/test/endtoend/onlineddl/...
2121package onlineddl
2222
2323import (
24+ "context"
2425 "testing"
2526 "time"
2627
2728 "github.com/stretchr/testify/assert"
2829 "github.com/stretchr/testify/require"
2930
31+ "vitess.io/vitess/go/mysql/fakesqldb"
32+ "vitess.io/vitess/go/sqltypes"
33+ "vitess.io/vitess/go/timer"
34+ "vitess.io/vitess/go/vt/dbconfigs"
35+ "vitess.io/vitess/go/vt/dbconnpool"
3036 "vitess.io/vitess/go/vt/schema"
37+ "vitess.io/vitess/go/vt/topo/memorytopo"
38+ "vitess.io/vitess/go/vt/vtenv"
39+ "vitess.io/vitess/go/vt/vttablet/tabletserver/tabletenv"
40+ "vitess.io/vitess/go/vt/vttablet/tmclient"
41+ "vitess.io/vitess/go/vt/vttablet/tmclienttest"
42+
43+ topodatapb "vitess.io/vitess/go/vt/proto/topodata"
3144)
3245
3346func TestShouldCutOverAccordingToBackoff (t * testing.T ) {
@@ -249,3 +262,90 @@ func TestGetInOrderCompletionPendingCount(t *testing.T) {
249262 require .Equal (t , uint64 (3 ), getInOrderCompletionPendingCount (onlineDDL , pendingMigrationsUUIDs ))
250263 }
251264}
265+
266+ func TestInitDBConnectionLockWaitTimeout (t * testing.T ) {
267+ db := fakesqldb .New (t )
268+ defer db .Close ()
269+ params := db .ConnParams ()
270+ connector := dbconfigs .NewTestDBConfigs (* params , * params , params .DbName ).DbaWithDB ()
271+ conn , err := dbconnpool .NewDBConnection (context .Background (), connector )
272+ require .NoError (t , err )
273+ defer conn .Close ()
274+
275+ db .AddQuery ("set @lock_wait_timeout=@@session.lock_wait_timeout" , & sqltypes.Result {})
276+ db .AddQuery ("set @@session.lock_wait_timeout=5" , & sqltypes.Result {})
277+ db .AddQuery ("set @@session.lock_wait_timeout=@lock_wait_timeout" , & sqltypes.Result {})
278+
279+ executor := & Executor {}
280+ deferFunc , err := executor .initDBConnectionLockWaitTimeout (conn , 5 * time .Second )
281+ require .NoError (t , err )
282+ queryLog := db .QueryLog ()
283+ assert .Contains (t , queryLog , "set @lock_wait_timeout=@@session.lock_wait_timeout" )
284+ assert .Contains (t , queryLog , "set @@session.lock_wait_timeout=5" )
285+
286+ deferFunc ()
287+ assert .Contains (t , db .QueryLog (), "set @@session.lock_wait_timeout=@lock_wait_timeout" )
288+ }
289+
290+ func TestExecuteDirectlySetsLockWaitTimeout (t * testing.T ) {
291+ ctx := t .Context ()
292+ db := fakesqldb .New (t )
293+ defer db .Close ()
294+ params := db .ConnParams ()
295+ connector := dbconfigs .NewTestDBConfigs (* params , * params , params .DbName ).DbaWithDB ()
296+ conn , err := dbconnpool .NewDBConnection (ctx , connector )
297+ require .NoError (t , err )
298+ defer conn .Close ()
299+
300+ db .AddQuery ("set @lock_wait_timeout=@@session.lock_wait_timeout" , & sqltypes.Result {})
301+ db .AddQuery ("set @@session.lock_wait_timeout=5" , & sqltypes.Result {})
302+ db .AddQuery ("set @@session.lock_wait_timeout=@lock_wait_timeout" , & sqltypes.Result {})
303+ db .AddQuery ("create table test_lock_wait(id int)" , & sqltypes.Result {})
304+
305+ venv := vtenv .NewTestEnv ()
306+ cfg := tabletenv .NewDefaultConfig ()
307+ cfg .DB = dbconfigs .NewTestDBConfigs (* params , * params , params .DbName )
308+ protocolName := t .Name ()
309+ resetProtocol := tmclienttest .SetProtocol (t .Name (), protocolName )
310+ defer resetProtocol ()
311+ tmclient .RegisterTabletManagerClientFactory (protocolName , func () tmclient.TabletManagerClient {
312+ return & fakeTabletManagerClient {}
313+ })
314+ alias := & topodatapb.TabletAlias {Cell : "cell" , Uid : 1 }
315+ ts := memorytopo .NewServer (ctx , "cell" )
316+ err = ts .CreateTablet (ctx , & topodatapb.Tablet {
317+ Alias : alias ,
318+ Keyspace : "ks" ,
319+ Shard : "0" ,
320+ Type : topodatapb .TabletType_PRIMARY ,
321+ })
322+ require .NoError (t , err )
323+ executor := & Executor {
324+ env : tabletenv .NewEnv (venv , cfg , "ExecutorTest" ),
325+ ts : ts ,
326+ tabletAlias : alias ,
327+ execQuery : func (ctx context.Context , query string ) (* sqltypes.Result , error ) {
328+ return & sqltypes.Result {}, nil
329+ },
330+ ticks : timer .NewTimer (migrationCheckInterval ),
331+ }
332+
333+ onlineDDL := & schema.OnlineDDL {SQL : "create table test_lock_wait(id int)" , CutOverThreshold : 5 * time .Second , UUID : "uuid" }
334+ _ , err = executor .executeDirectly (ctx , onlineDDL )
335+ require .NoError (t , err )
336+
337+ queryLog := db .QueryLog ()
338+ assert .Contains (t , queryLog , "set @lock_wait_timeout=@@session.lock_wait_timeout" )
339+ assert .Contains (t , queryLog , "set @@session.lock_wait_timeout=5" )
340+ assert .Contains (t , queryLog , "set @@session.lock_wait_timeout=@lock_wait_timeout" )
341+ }
342+
343+ type fakeTabletManagerClient struct {
344+ tmclient.TabletManagerClient
345+ }
346+
347+ func (fakeTabletManagerClient ) Close () {}
348+
349+ func (fakeTabletManagerClient ) ReloadSchema (ctx context.Context , tablet * topodatapb.Tablet , waitPosition string ) error {
350+ return nil
351+ }
0 commit comments