Skip to content

Commit aaf2f33

Browse files
authored
Merge branch 'master' into fix-Start
2 parents d166caa + 8efcbcb commit aaf2f33

File tree

2 files changed

+77
-10
lines changed

2 files changed

+77
-10
lines changed

testserver/testserver.go

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,9 @@ const (
7676
firstTenantID = 2
7777
)
7878

79+
// By default, we allocate 20% of available memory to the test server.
80+
const defaultStoreMemSize = 0.2
81+
7982
// TestServer is a helper to run a real cockroach node.
8083
type TestServer interface {
8184
// Start starts the server.
@@ -165,8 +168,10 @@ func NewDBForTestWithDatabase(
165168
type TestServerOpt func(args *testServerArgs)
166169

167170
type testServerArgs struct {
168-
secure bool
169-
rootPW string // if nonempty, set as pw for root
171+
secure bool
172+
rootPW string // if nonempty, set as pw for root
173+
storeOnDisk bool // to save database in disk
174+
storeMemSize float64 // the proportion of available memory allocated to test server
170175
}
171176

172177
// SecureOpt is a TestServer option that can be passed to NewTestServer to
@@ -177,6 +182,27 @@ func SecureOpt() TestServerOpt {
177182
}
178183
}
179184

185+
// StoreOnDiskOpt is a TestServer option that can be passed to NewTestServer
186+
// to enable storing database in memory.
187+
func StoreOnDiskOpt() TestServerOpt {
188+
return func(args *testServerArgs) {
189+
args.storeOnDisk = true
190+
}
191+
}
192+
193+
// SetStoreMemSizeOpt is a TestServer option that can be passed to NewTestServer
194+
// to set the proportion of available memory that is allocated
195+
// to the test server.
196+
func SetStoreMemSizeOpt(memSize float64) TestServerOpt {
197+
return func(args *testServerArgs) {
198+
if memSize > 0 {
199+
args.storeMemSize = memSize
200+
} else {
201+
args.storeMemSize = defaultStoreMemSize
202+
}
203+
}
204+
}
205+
180206
// RootPasswordOpt is a TestServer option that, when passed to NewTestServer,
181207
// sets the given password for the root user (and returns a URL using it from
182208
// PGURL(). This avoids having to use client certs.
@@ -199,6 +225,7 @@ const (
199225
// found in your path.
200226
func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
201227
serverArgs := &testServerArgs{}
228+
serverArgs.storeMemSize = defaultStoreMemSize
202229
for _, applyOptToArgs := range opts {
203230
applyOptToArgs(serverArgs)
204231
}
@@ -235,6 +262,9 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
235262
}
236263
return path, nil
237264
}
265+
// TODO(janexing): Make sure the log is written to logDir instead of shown in console.
266+
// Should be done once issue #109 is solved:
267+
// https://github.com/cockroachdb/cockroach-go/issues/109
238268
logDir, err := mkDir(logsDirName)
239269
if err != nil {
240270
return nil, err
@@ -291,6 +321,13 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
291321
startCmd = "start"
292322
}
293323

324+
var storeArg string
325+
if serverArgs.storeOnDisk {
326+
storeArg = "--store=path=" + baseDir
327+
} else {
328+
storeArg = fmt.Sprintf("--store=type=mem,size=%.2f", serverArgs.storeMemSize)
329+
}
330+
294331
args := []string{
295332
cockroachBinary,
296333
startCmd,
@@ -299,7 +336,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
299336
"--host=localhost",
300337
"--port=0",
301338
"--http-port=0",
302-
"--store=" + baseDir,
339+
storeArg,
303340
"--listening-url-file=" + listeningURLFile,
304341
}
305342

testserver/testserver_test.go

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ import (
2424

2525
const noPW = ""
2626

27+
const defStoreMemSize = 0.2
28+
2729
func TestRunServer(t *testing.T) {
2830
const testPW = "foobar"
2931
for _, tc := range []struct {
@@ -34,6 +36,12 @@ func TestRunServer(t *testing.T) {
3436
name: "Insecure",
3537
instantiation: func(t *testing.T) (*sql.DB, func()) { return testserver.NewDBForTest(t) },
3638
},
39+
{
40+
name: "InsecureWithCustomizedMemSize",
41+
instantiation: func(t *testing.T) (*sql.DB, func()) {
42+
return testserver.NewDBForTest(t, testserver.SetStoreMemSizeOpt(0.3))
43+
},
44+
},
3745
{
3846
name: "SecureClientCert",
3947
instantiation: func(t *testing.T) (*sql.DB, func()) { return testserver.NewDBForTest(t, testserver.SecureOpt()) },
@@ -44,34 +52,46 @@ func TestRunServer(t *testing.T) {
4452
return testserver.NewDBForTest(t, testserver.SecureOpt(), testserver.RootPasswordOpt(testPW))
4553
},
4654
},
55+
{
56+
name: "InsecureTenantStoreOnDisk",
57+
instantiation: func(t *testing.T) (*sql.DB, func()) {
58+
return testserver.NewDBForTest(t, testserver.StoreOnDiskOpt())
59+
},
60+
},
61+
{
62+
name: "SecureTenantStoreOnDisk",
63+
instantiation: func(t *testing.T) (*sql.DB, func()) {
64+
return testserver.NewDBForTest(t, testserver.SecureOpt(), testserver.StoreOnDiskOpt())
65+
},
66+
},
4767
{
4868
name: "InsecureTenant",
4969
instantiation: func(t *testing.T) (*sql.DB, func()) {
50-
return newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW)
70+
return newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
5171
},
5272
},
5373
{
5474
name: "SecureTenant",
5575
instantiation: func(t *testing.T) (*sql.DB, func()) {
56-
return newTenantDBForTest(t, true /* secure */, false /* proxy */, noPW)
76+
return newTenantDBForTest(t, true /* secure */, false /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
5777
},
5878
},
5979
{
6080
name: "SecureTenantCustomPassword",
6181
instantiation: func(t *testing.T) (*sql.DB, func()) {
62-
return newTenantDBForTest(t, true /* secure */, false /* proxy */, testPW)
82+
return newTenantDBForTest(t, true /* secure */, false /* proxy */, testPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
6383
},
6484
},
6585
{
6686
name: "SecureTenantThroughProxy",
6787
instantiation: func(t *testing.T) (*sql.DB, func()) {
68-
return newTenantDBForTest(t, true /* secure */, true /* proxy */, noPW)
88+
return newTenantDBForTest(t, true /* secure */, true /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
6989
},
7090
},
7191
{
7292
name: "SecureTenantThroughProxyCustomPassword",
7393
instantiation: func(t *testing.T) (*sql.DB, func()) {
74-
return newTenantDBForTest(t, true /* secure */, true /* proxy */, testPW)
94+
return newTenantDBForTest(t, true /* secure */, true /* proxy */, testPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
7595
},
7696
},
7797
} {
@@ -105,15 +125,25 @@ type tenantInterface interface {
105125
// newTenantDBForTest is a testing helper function that starts a TestServer
106126
// process and a SQL tenant process pointed at this TestServer. A sql connection
107127
// to the tenant and a cleanup function are returned.
108-
func newTenantDBForTest(t *testing.T, secure bool, proxy bool, pw string) (*sql.DB, func()) {
128+
func newTenantDBForTest(
129+
t *testing.T, secure bool, proxy bool, pw string, diskStore bool, storeMemSize float64,
130+
) (*sql.DB, func()) {
109131
t.Helper()
110132
var opts []testserver.TestServerOpt
111133
if secure {
112134
opts = append(opts, testserver.SecureOpt())
113135
}
136+
if diskStore {
137+
opts = append(opts, testserver.StoreOnDiskOpt())
138+
}
114139
if pw != "" {
115140
opts = append(opts, testserver.RootPasswordOpt(pw))
116141
}
142+
if storeMemSize >= 0 {
143+
opts = append(opts, testserver.SetStoreMemSizeOpt(storeMemSize))
144+
} else {
145+
t.Fatal("Percentage memory size for data storage cannot be nagative")
146+
}
117147
ts, err := testserver.NewTestServer(opts...)
118148
if err != nil {
119149
t.Fatal(err)
@@ -138,7 +168,7 @@ func newTenantDBForTest(t *testing.T, secure bool, proxy bool, pw string) (*sql.
138168
}
139169

140170
func TestTenant(t *testing.T) {
141-
db, stop := newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW)
171+
db, stop := newTenantDBForTest(t, false /* secure */, false /* proxy */, noPW, false /* diskStore */, defStoreMemSize /* storeMemSize */)
142172
defer stop()
143173
if _, err := db.Exec("SELECT 1"); err != nil {
144174
t.Fatal(err)

0 commit comments

Comments
 (0)