vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/reservedconn/reconnect4/main_test.go (about)

     1  /*
     2  Copyright 2022 The Vitess Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package reservedconn
    18  
    19  import (
    20  	"context"
    21  	"flag"
    22  	"os"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/test/endtoend/utils"
    26  
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"vitess.io/vitess/go/mysql"
    30  	"vitess.io/vitess/go/test/endtoend/cluster"
    31  )
    32  
    33  var (
    34  	clusterInstance *cluster.LocalProcessCluster
    35  	vtParams        mysql.ConnParams
    36  	keyspaceName    = "ks"
    37  	cell            = "zone1"
    38  	hostname        = "localhost"
    39  	sqlSchema       = `create table test(id bigint primary key)Engine=InnoDB;`
    40  )
    41  
    42  var enableSettingsPool bool
    43  
    44  func TestMain(m *testing.M) {
    45  	defer cluster.PanicHandler(nil)
    46  	flag.Parse()
    47  
    48  	code := runAllTests(m)
    49  	if code != 0 {
    50  		os.Exit(code)
    51  	}
    52  
    53  	println("running with settings pool enabled")
    54  	// run again with settings pool enabled.
    55  	enableSettingsPool = true
    56  	code = runAllTests(m)
    57  	os.Exit(code)
    58  }
    59  
    60  func runAllTests(m *testing.M) int {
    61  	clusterInstance = cluster.NewCluster(cell, hostname)
    62  	defer clusterInstance.Teardown()
    63  
    64  	// Start topo server
    65  	if err := clusterInstance.StartTopo(); err != nil {
    66  		return 1
    67  	}
    68  
    69  	// Start keyspace
    70  	keyspace := &cluster.Keyspace{
    71  		Name:      keyspaceName,
    72  		SchemaSQL: sqlSchema,
    73  	}
    74  	if enableSettingsPool {
    75  		clusterInstance.VtTabletExtraArgs = append(clusterInstance.VtTabletExtraArgs, "--queryserver-enable-settings-pool")
    76  	}
    77  	if err := clusterInstance.StartUnshardedKeyspace(*keyspace, 2, false); err != nil {
    78  		return 1
    79  	}
    80  
    81  	// Start vtgate
    82  	if err := clusterInstance.StartVtgate(); err != nil {
    83  		return 1
    84  	}
    85  
    86  	vtParams = mysql.ConnParams{
    87  		Host: clusterInstance.Hostname,
    88  		Port: clusterInstance.VtgateMySQLPort,
    89  	}
    90  	return m.Run()
    91  }
    92  
    93  func TestVttabletDownServingChange(t *testing.T) {
    94  	conn, err := mysql.Connect(context.Background(), &vtParams)
    95  	require.NoError(t, err)
    96  	defer conn.Close()
    97  
    98  	utils.Exec(t, conn, "set default_week_format = 1")
    99  	_ = utils.Exec(t, conn, "select /*vt+ PLANNER=gen4 */ * from test")
   100  
   101  	primaryTablet := clusterInstance.Keyspaces[0].Shards[0].PrimaryTablet()
   102  	require.NoError(t,
   103  		primaryTablet.MysqlctlProcess.Stop())
   104  	// kill vttablet process
   105  	_ = primaryTablet.VttabletProcess.TearDown()
   106  	require.NoError(t,
   107  		clusterInstance.VtctlclientProcess.ExecuteCommand("EmergencyReparentShard", "--", "--keyspace_shard", "ks/0"))
   108  
   109  	// This should work without any error.
   110  	_ = utils.Exec(t, conn, "select /*vt+ PLANNER=gen4 */ * from test")
   111  }