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

     1  /*
     2  Copyright 2020 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  	"flag"
    21  	"fmt"
    22  	"os"
    23  	"testing"
    24  
    25  	"vitess.io/vitess/go/test/endtoend/utils"
    26  	querypb "vitess.io/vitess/go/vt/proto/query"
    27  
    28  	"github.com/stretchr/testify/assert"
    29  
    30  	"vitess.io/vitess/go/mysql"
    31  	"vitess.io/vitess/go/test/endtoend/cluster"
    32  )
    33  
    34  var (
    35  	clusterInstance *cluster.LocalProcessCluster
    36  	vtParams        mysql.ConnParams
    37  	keyspaceName    = "ks"
    38  	cell            = "zone1"
    39  	hostname        = "localhost"
    40  	sqlSchema       = `
    41  	create table test(
    42  		id bigint,
    43  		val1 varchar(16),
    44  		val2 int,
    45  		val3 float,
    46  		primary key(id)
    47  	)Engine=InnoDB;
    48  
    49  CREATE TABLE test_vdx (
    50      val1 varchar(16) NOT NULL,
    51      keyspace_id binary(8),
    52      UNIQUE KEY (val1)
    53  ) ENGINE=Innodb;
    54  `
    55  
    56  	vSchema = `
    57  		{	
    58  			"sharded":true,
    59  			"vindexes": {
    60  				"hash_index": {
    61  					"type": "hash"
    62  				},
    63  				"lookup1": {
    64  					"type": "consistent_lookup",
    65  					"params": {
    66  						"table": "test_vdx",
    67  						"from": "val1",
    68  						"to": "keyspace_id",
    69  						"ignore_nulls": "true"
    70  					},
    71  					"owner": "test"
    72  				},
    73  				"unicode_vdx":{
    74  					"type": "unicode_loose_md5"
    75                  }
    76  			},	
    77  			"tables": {
    78  				"test":{
    79  					"column_vindexes": [
    80  						{
    81  							"column": "id",
    82  							"name": "hash_index"
    83  						},
    84  						{
    85  							"column": "val1",
    86  							"name": "lookup1"
    87  						}
    88  					]
    89  				},
    90  				"test_vdx":{
    91  					"column_vindexes": [
    92  						{
    93  							"column": "val1",
    94  							"name": "unicode_vdx"
    95  						}
    96  					]
    97  				}
    98  			}
    99  		}
   100  	`
   101  )
   102  
   103  var enableSettingsPool bool
   104  
   105  func TestMain(m *testing.M) {
   106  	defer cluster.PanicHandler(nil)
   107  	flag.Parse()
   108  
   109  	code := runAllTests(m)
   110  	if code != 0 {
   111  		os.Exit(code)
   112  	}
   113  
   114  	println("running with settings pool enabled")
   115  	// run again with settings pool enabled.
   116  	enableSettingsPool = true
   117  	code = runAllTests(m)
   118  	os.Exit(code)
   119  }
   120  
   121  func runAllTests(m *testing.M) int {
   122  	clusterInstance = cluster.NewCluster(cell, hostname)
   123  	defer clusterInstance.Teardown()
   124  
   125  	// Start topo server
   126  	if err := clusterInstance.StartTopo(); err != nil {
   127  		return 1
   128  	}
   129  
   130  	// Start keyspace
   131  	keyspace := &cluster.Keyspace{
   132  		Name:      keyspaceName,
   133  		SchemaSQL: sqlSchema,
   134  		VSchema:   vSchema,
   135  	}
   136  	clusterInstance.VtTabletExtraArgs = []string{"--queryserver-config-transaction-timeout", "5"}
   137  	if enableSettingsPool {
   138  		clusterInstance.VtTabletExtraArgs = append(clusterInstance.VtTabletExtraArgs, "--queryserver-enable-settings-pool")
   139  	}
   140  	if err := clusterInstance.StartKeyspace(*keyspace, []string{"-80", "80-"}, 1, false); err != nil {
   141  		return 1
   142  	}
   143  
   144  	// Start vtgate
   145  	// This test requires setting the mysql_server_version vtgate flag
   146  	// to 5.7 regardless of the actual MySQL version used for the tests.
   147  	clusterInstance.VtGateExtraArgs = []string{"--lock_heartbeat_time", "2s", "--mysql_server_version", "5.7.0"}
   148  	clusterInstance.VtGatePlannerVersion = querypb.ExecuteOptions_Gen4
   149  	if err := clusterInstance.StartVtgate(); err != nil {
   150  		return 1
   151  	}
   152  
   153  	vtParams = mysql.ConnParams{
   154  		Host: clusterInstance.Hostname,
   155  		Port: clusterInstance.VtgateMySQLPort,
   156  	}
   157  	return m.Run()
   158  }
   159  
   160  func assertIsEmpty(t *testing.T, conn *mysql.Conn, query string) {
   161  	t.Helper()
   162  	qr := utils.Exec(t, conn, query)
   163  	assert.Empty(t, qr.Rows)
   164  }
   165  
   166  func assertResponseMatch(t *testing.T, conn *mysql.Conn, query1, query2 string) {
   167  	qr1 := utils.Exec(t, conn, query1)
   168  	got1 := fmt.Sprintf("%v", qr1.Rows)
   169  
   170  	qr2 := utils.Exec(t, conn, query2)
   171  	got2 := fmt.Sprintf("%v", qr2.Rows)
   172  
   173  	assert.Equal(t, got1, got2)
   174  }