vitess.io/vitess@v0.16.2/go/test/endtoend/vtgate/transaction/restart/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 misc
    18  
    19  import (
    20  	"context"
    21  	_ "embed"
    22  	"flag"
    23  	"os"
    24  	"testing"
    25  
    26  	"github.com/stretchr/testify/assert"
    27  	"github.com/stretchr/testify/require"
    28  
    29  	"vitess.io/vitess/go/mysql"
    30  	"vitess.io/vitess/go/test/endtoend/cluster"
    31  	"vitess.io/vitess/go/test/endtoend/utils"
    32  )
    33  
    34  var (
    35  	clusterInstance *cluster.LocalProcessCluster
    36  	vtParams        mysql.ConnParams
    37  	keyspaceName    = "ks"
    38  	cell            = "test"
    39  
    40  	//go:embed schema.sql
    41  	schemaSQL string
    42  )
    43  
    44  func TestMain(m *testing.M) {
    45  	defer cluster.PanicHandler(nil)
    46  	flag.Parse()
    47  
    48  	exitCode := func() int {
    49  		clusterInstance = cluster.NewCluster(cell, "localhost")
    50  		defer clusterInstance.Teardown()
    51  
    52  		// Start topo server
    53  		err := clusterInstance.StartTopo()
    54  		if err != nil {
    55  			return 1
    56  		}
    57  
    58  		// Start keyspace
    59  		keyspace := &cluster.Keyspace{
    60  			Name:      keyspaceName,
    61  			SchemaSQL: schemaSQL,
    62  		}
    63  		err = clusterInstance.StartUnshardedKeyspace(*keyspace, 1, false)
    64  		if err != nil {
    65  			return 1
    66  		}
    67  
    68  		// Start vtgate
    69  		clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs,
    70  			"--planner-version=gen4",
    71  			"--mysql_default_workload=olap")
    72  		err = clusterInstance.StartVtgate()
    73  		if err != nil {
    74  			return 1
    75  		}
    76  
    77  		vtParams = mysql.ConnParams{
    78  			Host: clusterInstance.Hostname,
    79  			Port: clusterInstance.VtgateMySQLPort,
    80  		}
    81  		return m.Run()
    82  	}()
    83  	os.Exit(exitCode)
    84  }
    85  
    86  /*
    87  TestStreamTxRestart tests that when a connection is killed my mysql (may be due to restart),
    88  then the transaction should not continue to serve the query via reconnect.
    89  */
    90  func TestStreamTxRestart(t *testing.T) {
    91  	ctx := context.Background()
    92  	conn, err := mysql.Connect(ctx, &vtParams)
    93  	require.NoError(t, err)
    94  	defer conn.Close()
    95  
    96  	utils.Exec(t, conn, "begin")
    97  	// BeginStreamExecute
    98  	_ = utils.Exec(t, conn, "select connection_id()")
    99  
   100  	// StreamExecute
   101  	_ = utils.Exec(t, conn, "select connection_id()")
   102  
   103  	// restart the mysql to terminate all the existing connections.
   104  	primTablet := clusterInstance.Keyspaces[0].Shards[0].PrimaryTablet()
   105  	err = primTablet.MysqlctlProcess.Stop()
   106  	require.NoError(t, err)
   107  	err = primTablet.MysqlctlProcess.StartProvideInit(false)
   108  	require.NoError(t, err)
   109  
   110  	// query should return connection error
   111  	_, err = utils.ExecAllowError(t, conn, "select connection_id()")
   112  	require.Error(t, err)
   113  	assert.Contains(t, err.Error(), "broken pipe (errno 2006) (sqlstate HY000)")
   114  }