Skip to content

Commit 09a15e4

Browse files
committed
sql/schemachanger: extend TestServer factory for shared servers
Previously, we only supported running a closure against a TestServer in the schema changer testing framework. This was problematic for tests that could save time by sharing servers (for example BACKUP / RESTORE). To address this, this patch introduces a Start method for starting up a server. Release note: None
1 parent f3c24cf commit 09a15e4

File tree

2 files changed

+46
-11
lines changed

2 files changed

+46
-11
lines changed

pkg/ccl/schemachangerccl/multiregion_testcluster_factory.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -56,10 +56,8 @@ func (f MultiRegionTestClusterFactory) WithSchemaLockDisabled() sctest.TestServe
5656
return f
5757
}
5858

59-
// Run implements the sctest.TestServerFactory interface.
60-
func (f MultiRegionTestClusterFactory) Run(
61-
ctx context.Context, t *testing.T, fn func(_ serverutils.TestServerInterface, _ *gosql.DB),
62-
) {
59+
// Start implements the sctest.TestServerFactory interface.
60+
func (f MultiRegionTestClusterFactory) Start(ctx context.Context, t *testing.T) sctest.TestServer {
6361
const numServers = 3
6462
knobs := base.TestingKnobs{
6563
SQLEvalContext: &eval.TestingKnobs{
@@ -83,6 +81,19 @@ func (f MultiRegionTestClusterFactory) Run(
8381
}
8482
sql.CreateTableWithSchemaLocked.Override(ctx, &st.SV, !f.schemaLockedDisabled)
8583
c, db, _ := multiregionccltestutils.TestingCreateMultiRegionCluster(t, numServers, knobs, multiregionccltestutils.WithSettings(st))
86-
defer c.Stopper().Stop(ctx)
87-
fn(c.Server(0), db)
84+
return sctest.TestServer{
85+
Server: c.Server(0),
86+
DB: db,
87+
Stopper: func(t *testing.T) {
88+
c.Stopper().Stop(ctx)
89+
},
90+
}
91+
}
92+
93+
func (f MultiRegionTestClusterFactory) Run(
94+
ctx context.Context, t *testing.T, fn func(s serverutils.TestServerInterface, tdb *gosql.DB),
95+
) {
96+
s := f.Start(ctx, t)
97+
defer s.Stopper(t)
98+
fn(s.Server, s.DB)
8899
}

pkg/sql/schemachanger/sctest/test_server_factory.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,13 @@ import (
2525
"github.com/cockroachdb/errors"
2626
)
2727

28+
// TestServer is a test cluster and associated test database.
29+
type TestServer struct {
30+
Server serverutils.TestServerInterface
31+
DB *gosql.DB
32+
Stopper func(t *testing.T)
33+
}
34+
2835
// TestServerFactory constructs test clusters for declarative schema changer
2936
// end-to-end tests.
3037
type TestServerFactory interface {
@@ -48,6 +55,12 @@ type TestServerFactory interface {
4855
t *testing.T,
4956
fn func(s serverutils.TestServerInterface, tdb *gosql.DB),
5057
)
58+
59+
// Start creates a test cluster and returns a TestServer.
60+
Start(
61+
ctx context.Context,
62+
t *testing.T,
63+
) TestServer
5164
}
5265

5366
// SingleNodeTestClusterFactory is the vanilla implementation of
@@ -85,8 +98,15 @@ func (f SingleNodeTestClusterFactory) WithSchemaLockDisabled() TestServerFactory
8598

8699
// Run implements the TestServerFactory interface.
87100
func (f SingleNodeTestClusterFactory) Run(
88-
ctx context.Context, t *testing.T, fn func(_ serverutils.TestServerInterface, _ *gosql.DB),
101+
ctx context.Context, t *testing.T, fn func(s serverutils.TestServerInterface, tdb *gosql.DB),
89102
) {
103+
s := f.Start(ctx, t)
104+
defer s.Stopper(t)
105+
fn(s.Server, s.DB)
106+
}
107+
108+
// Start implements the TestServerFactory interface.
109+
func (f SingleNodeTestClusterFactory) Start(ctx context.Context, t *testing.T) TestServer {
90110
args := base.TestServerArgs{
91111
Knobs: base.TestingKnobs{
92112
SQLEvalContext: &eval.TestingKnobs{
@@ -113,10 +133,14 @@ func (f SingleNodeTestClusterFactory) Run(
113133
}
114134
sql.CreateTableWithSchemaLocked.Override(ctx, &args.Settings.SV, !f.schemaLockedDisabled)
115135
s, db, _ := serverutils.StartServer(t, args)
116-
defer func() {
117-
s.Stopper().Stop(ctx)
118-
}()
119-
fn(s, db)
136+
137+
return TestServer{
138+
Server: s,
139+
DB: db,
140+
Stopper: func(t *testing.T) {
141+
s.Stopper().Stop(ctx)
142+
},
143+
}
120144
}
121145

122146
// OldVersionKey is the version key used by the WithMixedVersion method

0 commit comments

Comments
 (0)