vitess.io/vitess@v0.16.2/go/test/endtoend/topotest/etcd2/main_test.go (about) 1 /* 2 Copyright 2021 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 ectd2 18 19 import ( 20 "context" 21 "flag" 22 "os" 23 "testing" 24 "time" 25 26 "vitess.io/vitess/go/test/endtoend/utils" 27 28 "vitess.io/vitess/go/vt/log" 29 30 "github.com/stretchr/testify/require" 31 32 "vitess.io/vitess/go/mysql" 33 "vitess.io/vitess/go/sqltypes" 34 "vitess.io/vitess/go/test/endtoend/cluster" 35 ) 36 37 var ( 38 clusterInstance *cluster.LocalProcessCluster 39 cell = "zone1" 40 hostname = "localhost" 41 KeyspaceName = "customer" 42 SchemaSQL = ` 43 CREATE TABLE t1 ( 44 c1 BIGINT NOT NULL, 45 c2 BIGINT NOT NULL, 46 c3 BIGINT, 47 c4 varchar(100), 48 PRIMARY KEY (c1), 49 UNIQUE KEY (c2), 50 UNIQUE KEY (c3), 51 UNIQUE KEY (c4) 52 ) ENGINE=Innodb;` 53 VSchema = ` 54 { 55 "sharded": false, 56 "tables": { 57 "t1": {} 58 } 59 } 60 ` 61 ) 62 63 func TestMain(m *testing.M) { 64 defer cluster.PanicHandler(nil) 65 flag.Parse() 66 67 exitCode := func() int { 68 clusterInstance = cluster.NewCluster(cell, hostname) 69 defer clusterInstance.Teardown() 70 71 // Start topo server 72 if err := clusterInstance.StartTopo(); err != nil { 73 return 1 74 } 75 76 // Start keyspace 77 Keyspace := &cluster.Keyspace{ 78 Name: KeyspaceName, 79 SchemaSQL: SchemaSQL, 80 VSchema: VSchema, 81 } 82 if err := clusterInstance.StartUnshardedKeyspace(*Keyspace, 0, false); err != nil { 83 log.Fatal(err.Error()) 84 return 1 85 } 86 87 // Start vtgate 88 if err := clusterInstance.StartVtgate(); err != nil { 89 log.Fatal(err.Error()) 90 return 1 91 } 92 93 return m.Run() 94 }() 95 os.Exit(exitCode) 96 } 97 98 func TestTopoDownServingQuery(t *testing.T) { 99 defer cluster.PanicHandler(t) 100 ctx := context.Background() 101 vtParams := mysql.ConnParams{ 102 Host: "localhost", 103 Port: clusterInstance.VtgateMySQLPort, 104 } 105 conn, err := mysql.Connect(ctx, &vtParams) 106 require.Nil(t, err) 107 defer conn.Close() 108 109 defer utils.Exec(t, conn, `delete from t1`) 110 111 execMulti(t, conn, `insert into t1(c1, c2, c3, c4) values (300,100,300,'abc'); ;; insert into t1(c1, c2, c3, c4) values (301,101,301,'abcd');;`) 112 utils.AssertMatches(t, conn, `select c1,c2,c3 from t1`, `[[INT64(300) INT64(100) INT64(300)] [INT64(301) INT64(101) INT64(301)]]`) 113 clusterInstance.TopoProcess.TearDown(clusterInstance.Cell, clusterInstance.OriginalVTDATAROOT, clusterInstance.CurrentVTDATAROOT, true, *clusterInstance.TopoFlavorString()) 114 time.Sleep(3 * time.Second) 115 utils.AssertMatches(t, conn, `select c1,c2,c3 from t1`, `[[INT64(300) INT64(100) INT64(300)] [INT64(301) INT64(101) INT64(301)]]`) 116 } 117 118 func execMulti(t *testing.T, conn *mysql.Conn, query string) []*sqltypes.Result { 119 t.Helper() 120 var res []*sqltypes.Result 121 qr, more, err := conn.ExecuteFetchMulti(query, 1000, true) 122 res = append(res, qr) 123 require.NoError(t, err) 124 for more == true { 125 qr, more, _, err = conn.ReadQueryResult(1000, true) 126 require.NoError(t, err) 127 res = append(res, qr) 128 } 129 return res 130 }