Skip to content

Commit cd30d9d

Browse files
committed
Initial e2e test
Signed-off-by: Mohamed Hamza <mhamza15@github.com>
1 parent 8fae28c commit cd30d9d

3 files changed

Lines changed: 176 additions & 0 deletions

File tree

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
Copyright 2025 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 sessionbalancer
18+
19+
import (
20+
_ "embed"
21+
"flag"
22+
"os"
23+
"testing"
24+
25+
"vitess.io/vitess/go/mysql"
26+
"vitess.io/vitess/go/test/endtoend/cluster"
27+
)
28+
29+
var (
30+
clusterInstance *cluster.LocalProcessCluster
31+
vtParams mysql.ConnParams
32+
uks = "uks"
33+
cell = "test_misc"
34+
35+
//go:embed uschema.sql
36+
uschemaSQL string
37+
)
38+
39+
func TestMain(m *testing.M) {
40+
flag.Parse()
41+
42+
exitCode := func() int {
43+
clusterInstance = cluster.NewCluster(cell, "localhost")
44+
defer clusterInstance.Teardown()
45+
46+
// Start topo server
47+
err := clusterInstance.StartTopo()
48+
if err != nil {
49+
return 1
50+
}
51+
52+
// Enable session balancer in vtgate
53+
clusterInstance.VtGateExtraArgs = append(clusterInstance.VtGateExtraArgs,
54+
"--enable-balancer",
55+
"--balancer-vtgate-cells", clusterInstance.Cell,
56+
"--balancer-type", "session")
57+
58+
// Start keyspace with multiple tablets per shard
59+
keyspace := &cluster.Keyspace{
60+
Name: uks,
61+
SchemaSQL: uschemaSQL,
62+
}
63+
err = clusterInstance.StartUnshardedKeyspace(*keyspace, 2, false)
64+
if err != nil {
65+
return 1
66+
}
67+
68+
// Start vtgate
69+
err = clusterInstance.StartVtgate()
70+
if err != nil {
71+
return 1
72+
}
73+
74+
vtParams = clusterInstance.GetVTParams(uks)
75+
76+
return m.Run()
77+
}()
78+
os.Exit(exitCode)
79+
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
Copyright 2025 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 sessionbalancer
18+
19+
import (
20+
"context"
21+
"testing"
22+
23+
"github.com/stretchr/testify/require"
24+
25+
"vitess.io/vitess/go/mysql"
26+
)
27+
28+
// TestSessionBalancer validates that the session balancer consistently routes
29+
// queries for the same session to the same tablet.
30+
func TestSessionBalancer(t *testing.T) {
31+
// Get connections that route to different tablets
32+
conn1, conn2, id1, id2 := connections(t)
33+
34+
defer conn1.Close()
35+
defer conn2.Close()
36+
37+
// Validate that each connection consistently returns the same server ID
38+
for range 20 {
39+
newID1 := serverID(t, conn1)
40+
require.Equal(t, id1, newID1)
41+
42+
newID2 := serverID(t, conn2)
43+
require.Equal(t, id2, newID2)
44+
45+
require.NotEqual(t, newID2, newID1)
46+
}
47+
}
48+
49+
// connections returns two connections that should route to different servers.
50+
func connections(t *testing.T) (conn1, conn2 *mysql.Conn, id1, id2 string) {
51+
t.Helper()
52+
53+
// Keep creating connections until we find two that route to different server IDs
54+
vtParams.DbName = uks + "@replica"
55+
for {
56+
conn1, err := mysql.Connect(context.Background(), &vtParams)
57+
require.NoError(t, err)
58+
59+
conn2, err = mysql.Connect(context.Background(), &vtParams)
60+
require.NoError(t, err)
61+
62+
id1 = serverID(t, conn1)
63+
id2 = serverID(t, conn2)
64+
65+
// Break if we found connections with different server IDs
66+
if id1 != id2 {
67+
return conn1, conn2, id1, id2
68+
}
69+
70+
// If not, close the connections and try again
71+
conn1.Close()
72+
conn2.Close()
73+
}
74+
}
75+
76+
// serverID runs a `SELECT @@server_id` on the given connection and returns the
77+
// server's ID.
78+
func serverID(t *testing.T, conn *mysql.Conn) string {
79+
t.Helper()
80+
81+
result1, err := conn.ExecuteFetch("SELECT @@server_uuid", 1, false)
82+
require.NoError(t, err)
83+
84+
tablet1Bytes, err := result1.Rows[0][0].ToBytes()
85+
require.NoError(t, err)
86+
87+
return string(tablet1Bytes)
88+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
CREATE TABLE t1 (
2+
id int PRIMARY KEY,
3+
name varchar(255)
4+
);
5+
6+
CREATE TABLE t2 (
7+
id int PRIMARY KEY,
8+
value varchar(255)
9+
);

0 commit comments

Comments
 (0)