Skip to content

Commit

Permalink
Fix test when run with others in the package
Browse files Browse the repository at this point in the history
Signed-off-by: Matt Lord <[email protected]>
  • Loading branch information
mattlord committed Jan 14, 2025
1 parent 0d3cc67 commit e1122ea
Showing 1 changed file with 23 additions and 14 deletions.
37 changes: 23 additions & 14 deletions go/test/endtoend/vtgate/vschema/vschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"path"
"strings"
"sync"
"sync/atomic"
"testing"
"time"

Expand Down Expand Up @@ -168,10 +169,18 @@ func TestVSchema(t *testing.T) {
utils.AssertMatches(t, conn, "delete from vt_user", `[]`)

if utils.BinaryIsAtLeastAtVersion(22, "vtgate") {
// Don't allow any users to modify the vschema via the SQL API
// in order to test that behavior.
writeConfig(configFile, map[string]string{
"vschema_ddl_authorized_users": "",
})

// Allow anyone to modify the vschema via the SQL API again when
// the test completes.
defer func() {
writeConfig(configFile, map[string]string{
"vschema_ddl_authorized_users": "%",
})
}()
require.EventuallyWithT(t, func(t *assert.CollectT) {
_, err = conn.ExecuteFetch("ALTER VSCHEMA DROP TABLE main", 1000, false)
assert.Error(t, err)
Expand All @@ -183,13 +192,14 @@ func TestVSchema(t *testing.T) {
// TestVSchemaSQLAPIConcurrency tests that we prevent lost writes when we have
// concurrent vschema changes being made via the SQL API.
func TestVSchemaSQLAPIConcurrency(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 60*time.Second)
ctx, cancel := context.WithTimeout(context.Background(), 90*time.Second)
defer cancel()
conn, err := mysql.Connect(ctx, &vtParams)
require.NoError(t, err)
defer conn.Close()

utils.AssertMatches(t, conn, "SHOW VSCHEMA TABLES", `[]`)
initialVSchema, err := conn.ExecuteFetch("SHOW VSCHEMA TABLES", -1, false)
require.NoError(t, err)
baseTableName := "t"
numTables := 1000
mysqlConns := make([]*mysql.Conn, numTables)
Expand All @@ -201,40 +211,39 @@ func TestVSchemaSQLAPIConcurrency(t *testing.T) {
}

wg := sync.WaitGroup{}
preventedLostWrites := false
preventedLostWrites := atomic.Bool{}
for i := 0; i < numTables; i++ {
wg.Add(1)
go func() {
defer wg.Done()
time.Sleep(time.Duration(rand.Intn(1000) * int(time.Nanosecond)))
time.Sleep(time.Duration(rand.Intn(100) * int(time.Nanosecond)))
tableName := fmt.Sprintf("%s%d", baseTableName, i)
_, err = mysqlConns[i].ExecuteFetch(fmt.Sprintf("ALTER VSCHEMA ADD TABLE %s", tableName), -1, false)
if err != nil {
// The error we get is an SQL error so we have to do string matching.
if err != nil && strings.Contains(err.Error(), vtgate.ErrStaleVSchema.Error()) {
preventedLostWrites = true
}
// The error we get is an SQL error so we have to do string matching.
if err != nil && strings.Contains(err.Error(), vtgate.ErrStaleVSchema.Error()) {
preventedLostWrites.Store(true)
} else {
require.NoError(t, err)
time.Sleep(time.Duration(rand.Intn(1000) * int(time.Nanosecond)))
time.Sleep(time.Duration(rand.Intn(75) * int(time.Nanosecond)))
_, err = mysqlConns[i].ExecuteFetch(fmt.Sprintf("ALTER VSCHEMA DROP TABLE %s", tableName), -1, false)
// The error we get is an SQL error so we have to do string matching.
if err != nil && strings.Contains(err.Error(), vtgate.ErrStaleVSchema.Error()) {
preventedLostWrites = true
preventedLostWrites.Store(true)
} else {
require.NoError(t, err)
}
}
}()
}
wg.Wait()
require.True(t, preventedLostWrites)
require.True(t, preventedLostWrites.Load())

// Cleanup any tables that were not dropped because the DROP query
// failed due to a bad node version.
for i := 0; i < numTables; i++ {
tableName := fmt.Sprintf("%s%d", baseTableName, i)
_, _ = mysqlConns[i].ExecuteFetch(fmt.Sprintf("ALTER VSCHEMA DROP TABLE %s", tableName), -1, false)
}
utils.AssertMatches(t, conn, "SHOW VSCHEMA TABLES", `[]`)
// Confirm that we're back to the initial state.
utils.AssertMatches(t, conn, "SHOW VSCHEMA TABLES", fmt.Sprintf("%v", initialVSchema.Rows))
}

0 comments on commit e1122ea

Please sign in to comment.