From d328dd4de33407f67546cd748d66f0fb4a131a59 Mon Sep 17 00:00:00 2001 From: Riley Laine Date: Sun, 9 Jun 2024 16:38:26 -0700 Subject: [PATCH] Fix tests so they can be run simultaneously Signed-off-by: Riley Laine --- go/test/endtoend/vtgateproxy/failure_test.go | 24 ---------------- go/test/endtoend/vtgateproxy/main_test.go | 28 +++++++++++++++---- .../endtoend/vtgateproxy/rebalance_test.go | 16 +---------- go/test/endtoend/vtgateproxy/scale_test.go | 13 --------- .../endtoend/vtgateproxy/vtgateproxy_test.go | 20 ++++++------- go/vt/vtgateproxy/mysql_server.go | 6 ++-- 6 files changed, 35 insertions(+), 72 deletions(-) diff --git a/go/test/endtoend/vtgateproxy/failure_test.go b/go/test/endtoend/vtgateproxy/failure_test.go index 179afc770ea..14c1ad0e2c2 100644 --- a/go/test/endtoend/vtgateproxy/failure_test.go +++ b/go/test/endtoend/vtgateproxy/failure_test.go @@ -29,26 +29,13 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/assert" - "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/test/endtoend/cluster" - "vitess.io/vitess/go/test/endtoend/utils" "vitess.io/vitess/go/vt/log" ) func TestVtgateProxyVtgateFailureRoundRobin(t *testing.T) { defer cluster.PanicHandler(t) - // insert test value - func() { - conn, err := mysql.Connect(context.Background(), &vtParams) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - - utils.Exec(t, conn, "insert into customer(id, email) values(1, 'email1')") - }() - const targetAffinity = "use1-az1" const vtgateCount = 4 const vtgateproxyConnections = 4 @@ -129,17 +116,6 @@ func TestVtgateProxyVtgateFailureRoundRobin(t *testing.T) { func TestVtgateProxyVtgateFailureFirstReady(t *testing.T) { defer cluster.PanicHandler(t) - // insert test value - func() { - conn, err := mysql.Connect(context.Background(), &vtParams) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - - utils.Exec(t, conn, "insert into customer(id, email) values(1, 'email1')") - }() - const targetAffinity = "use1-az1" const vtgateCount = 4 const vtgateproxyConnections = 4 diff --git a/go/test/endtoend/vtgateproxy/main_test.go b/go/test/endtoend/vtgateproxy/main_test.go index d5aca900228..375d658df51 100644 --- a/go/test/endtoend/vtgateproxy/main_test.go +++ b/go/test/endtoend/vtgateproxy/main_test.go @@ -65,12 +65,12 @@ var ( ) ENGINE=InnoDB;` vSchema = `{ - "tables": { - "product": {}, - "customer": {}, - "corder": {} - } - }` + "tables": { + "product": {}, + "customer": {}, + "corder": {} + } + }` ) func TestMain(m *testing.M) { @@ -107,6 +107,9 @@ func TestMain(m *testing.M) { Host: clusterInstance.Hostname, Port: clusterInstance.VtgateMySQLPort, } + + insertStartValue(vtParams) + return m.Run() }() os.Exit(exitCode) @@ -459,3 +462,16 @@ func (q queryCount) Sum() int { return result } + +func insertStartValue(params mysql.ConnParams) { + conn, err := mysql.Connect(context.Background(), ¶ms) + if err != nil { + panic(err) + } + defer conn.Close() + + _, err = conn.ExecuteFetch("insert into customer(id, email) values(1, 'email1')", 1000, true) + if err != nil { + panic(err) + } +} diff --git a/go/test/endtoend/vtgateproxy/rebalance_test.go b/go/test/endtoend/vtgateproxy/rebalance_test.go index 18fab8e8400..c1bcabe9655 100644 --- a/go/test/endtoend/vtgateproxy/rebalance_test.go +++ b/go/test/endtoend/vtgateproxy/rebalance_test.go @@ -48,7 +48,7 @@ func testVtgateProxyRebalance(t *testing.T, loadBalancer string) { const targetAffinity = "use1-az1" const targetPool = "pool1" const vtgateCount = 10 - const vtgatesInAffinity = 8 + const vtgatesInAffinity = 5 const vtgateproxyConnections = 4 vtgates, err := startAdditionalVtgates(vtgateCount) @@ -112,20 +112,6 @@ func testVtgateProxyRebalance(t *testing.T, loadBalancer string) { t.Fatal(err) } - log.Info("Inserting test value") - tx, err := conn.BeginTx(context.Background(), nil) - if err != nil { - t.Fatal(err) - } - - _, err = tx.Exec("insert into customer(id, email) values(1, 'email1')") - if err != nil { - t.Fatal(err) - } - if err := tx.Commit(); err != nil { - t.Fatal(err) - } - log.Info("Reading test value while adding vtgates") const totalQueries = 1000 diff --git a/go/test/endtoend/vtgateproxy/scale_test.go b/go/test/endtoend/vtgateproxy/scale_test.go index 43b365c2c6a..a98f1c38393 100644 --- a/go/test/endtoend/vtgateproxy/scale_test.go +++ b/go/test/endtoend/vtgateproxy/scale_test.go @@ -31,9 +31,7 @@ import ( _ "github.com/go-sql-driver/mysql" "github.com/stretchr/testify/assert" - "vitess.io/vitess/go/mysql" "vitess.io/vitess/go/test/endtoend/cluster" - "vitess.io/vitess/go/test/endtoend/utils" "vitess.io/vitess/go/vt/log" ) @@ -48,17 +46,6 @@ func TestVtgateProxyScaleFirstReady(t *testing.T) { func testVtgateProxyScale(t *testing.T, loadBalancer string) { defer cluster.PanicHandler(t) - // insert test value - func() { - conn, err := mysql.Connect(context.Background(), &vtParams) - if err != nil { - t.Fatal(err) - } - defer conn.Close() - - utils.Exec(t, conn, "insert into customer(id, email) values(1, 'email1')") - }() - const targetAffinity = "use1-az1" const targetPool = "pool1" const vtgateCount = 5 diff --git a/go/test/endtoend/vtgateproxy/vtgateproxy_test.go b/go/test/endtoend/vtgateproxy/vtgateproxy_test.go index 2f96e74ebf5..a5a84c23dc7 100644 --- a/go/test/endtoend/vtgateproxy/vtgateproxy_test.go +++ b/go/test/endtoend/vtgateproxy/vtgateproxy_test.go @@ -15,11 +15,12 @@ limitations under the License. This tests select/insert using the unshared keyspace added in main_test */ -package vtgateproxytest +package vtgateproxy import ( "context" "encoding/json" + "math/rand" "os" "path/filepath" "strconv" @@ -91,26 +92,23 @@ func testVtgateProxyProcess(t *testing.T, loadBalancer string) { } log.Info("Inserting test value") - tx, err := conn.BeginTx(context.Background(), nil) - if err != nil { - t.Fatal(err) - } + value := "email" + strconv.Itoa(rand.Intn(1000)) - _, err = tx.Exec("insert into customer(id, email) values(1, 'email1')") + // Yes yes little bobby tables, I see you. We don't support parameterized + // queries yet. VTGateProxy.Prepare still needs to be implemented. + _, err = conn.Exec("insert into customer(email) values('" + value + "')") if err != nil { t.Fatal(err) } - if err := tx.Commit(); err != nil { - t.Fatal(err) - } log.Info("Reading test value") - result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer") + result, err := selectHelper[customerEntry](context.Background(), conn, "select id, email from customer order by id desc limit 1") if err != nil { t.Fatal(err) } log.Infof("Read value %v", result) - assert.Equal(t, []customerEntry{{1, "email1"}}, result) + assert.Len(t, result, 1) + assert.Equal(t, value, result[0].Email) } diff --git a/go/vt/vtgateproxy/mysql_server.go b/go/vt/vtgateproxy/mysql_server.go index e07b4ff109f..559dea49608 100644 --- a/go/vt/vtgateproxy/mysql_server.go +++ b/go/vt/vtgateproxy/mysql_server.go @@ -271,13 +271,13 @@ func (ph *proxyHandler) ComPrepare(c *mysql.Conn, query string, bindVars map[str if !session.SessionPb().InTransaction { atomic.AddInt32(&busyConnections, 1) } - defer func() { + defer func(session *vtgateconn.VTGateSession) { if !session.SessionPb().InTransaction { atomic.AddInt32(&busyConnections, -1) } - }() + }(session) - session, fld, err := ph.proxy.Prepare(ctx, session, query, bindVars) + _, fld, err := ph.proxy.Prepare(ctx, session, query, bindVars) err = mysql.NewSQLErrorFromError(err) if err != nil { return nil, err