Skip to content

Commit

Permalink
Fix tests so they can be run simultaneously
Browse files Browse the repository at this point in the history
Signed-off-by: Riley Laine <rlaine@slack-corp.com>
  • Loading branch information
rjlaine committed Jun 9, 2024
1 parent 2dfcd6f commit d328dd4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 72 deletions.
24 changes: 0 additions & 24 deletions go/test/endtoend/vtgateproxy/failure_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
28 changes: 22 additions & 6 deletions go/test/endtoend/vtgateproxy/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,12 @@ var (
) ENGINE=InnoDB;`

vSchema = `{
"tables": {
"product": {},
"customer": {},
"corder": {}
}
}`
"tables": {
"product": {},
"customer": {},
"corder": {}
}
}`
)

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -107,6 +107,9 @@ func TestMain(m *testing.M) {
Host: clusterInstance.Hostname,
Port: clusterInstance.VtgateMySQLPort,
}

insertStartValue(vtParams)

return m.Run()
}()
os.Exit(exitCode)
Expand Down Expand Up @@ -459,3 +462,16 @@ func (q queryCount) Sum() int {

return result
}

func insertStartValue(params mysql.ConnParams) {
conn, err := mysql.Connect(context.Background(), &params)
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)
}
}
16 changes: 1 addition & 15 deletions go/test/endtoend/vtgateproxy/rebalance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down
13 changes: 0 additions & 13 deletions go/test/endtoend/vtgateproxy/scale_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)

Expand All @@ -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
Expand Down
20 changes: 9 additions & 11 deletions go/test/endtoend/vtgateproxy/vtgateproxy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
6 changes: 3 additions & 3 deletions go/vt/vtgateproxy/mysql_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d328dd4

Please sign in to comment.