@@ -215,7 +215,8 @@ type testServerArgs struct {
215
215
rootPW string // if nonempty, set as pw for root
216
216
storeOnDisk bool // to save database in disk
217
217
storeMemSize float64 // the proportion of available memory allocated to test server
218
- httpPort int
218
+ httpPorts []int
219
+ listenAddrPorts []int
219
220
testConfig TestConfig
220
221
nonStableDB bool
221
222
cockroachBinary string // path to cockroach executable file
@@ -287,9 +288,29 @@ func NonStableDbOpt() TestServerOpt {
287
288
288
289
// ExposeConsoleOpt is a TestServer option that can be passed to NewTestServer to
289
290
// expose the console of the server on the given port.
291
+ // Warning: This is kept around for backwards compatibility, use AddHttpPortOpt
292
+ // instead.
290
293
func ExposeConsoleOpt (port int ) TestServerOpt {
291
294
return func (args * testServerArgs ) {
292
- args .httpPort = port
295
+ args .httpPorts = []int {port }
296
+ }
297
+ }
298
+
299
+ // AddHttpPortOpt is a TestServer option that can be passed to NewTestServer to
300
+ // specify the http ports for the Cockroach nodes.
301
+ func AddHttpPortOpt (port int ) TestServerOpt {
302
+ return func (args * testServerArgs ) {
303
+ args .httpPorts = append (args .httpPorts , port )
304
+ }
305
+ }
306
+
307
+ // AddListenAddrPortOpt is a TestServer option that can be passed to NewTestServer to
308
+ // specify the ports for the Cockroach nodes.
309
+ // In the case of restarting nodes, it is up to the user of TestServer to make
310
+ // sure the port used here cannot be re-used.
311
+ func AddListenAddrPortOpt (port int ) TestServerOpt {
312
+ return func (args * testServerArgs ) {
313
+ args .listenAddrPorts = append (args .listenAddrPorts , port )
293
314
}
294
315
}
295
316
@@ -337,6 +358,19 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
337
358
serverArgs .cockroachBinary = customBinaryEnv
338
359
}
339
360
361
+ // For backwards compatibility, in the 3 node case where no args are
362
+ // specified, default to ports 26257, 26258, 26259.
363
+ if serverArgs .numNodes == 3 && len (serverArgs .listenAddrPorts ) == 0 {
364
+ serverArgs .listenAddrPorts = []int {26257 , 26258 , 26259 }
365
+ } else if serverArgs .numNodes != 1 && len (serverArgs .listenAddrPorts ) != serverArgs .numNodes {
366
+ panic (fmt .Sprintf ("need to specify a port for each node using AddListenAddrPortOpt, got %d nodes, need %d ports" ,
367
+ serverArgs .numNodes , len (serverArgs .listenAddrPorts )))
368
+ }
369
+
370
+ if len (serverArgs .listenAddrPorts ) == 0 || len (serverArgs .listenAddrPorts ) == 1 {
371
+ serverArgs .listenAddrPorts = []int {0 }
372
+ }
373
+
340
374
var err error
341
375
if serverArgs .cockroachBinary != "" {
342
376
log .Printf ("Using custom cockroach binary: %s" , serverArgs .cockroachBinary )
@@ -445,19 +479,30 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
445
479
446
480
nodes := make ([]nodeInfo , serverArgs .numNodes )
447
481
var initArgs []string
482
+ joinAddrs := make ([]string , 3 )
483
+ hostPort := serverArgs .listenAddrPorts [0 ]
484
+ for i , port := range serverArgs .listenAddrPorts {
485
+ joinAddrs [i ] = fmt .Sprintf ("localhost:%d" , port )
486
+ }
487
+
488
+ if len (serverArgs .httpPorts ) == 0 {
489
+ serverArgs .httpPorts = make ([]int , serverArgs .numNodes )
490
+ }
491
+
448
492
for i := 0 ; i < serverArgs .numNodes ; i ++ {
449
493
nodes [i ].state = stateNew
450
494
nodes [i ].listeningURLFile = filepath .Join (baseDir , fmt .Sprintf ("listen-url%d" , i ))
451
495
if serverArgs .numNodes > 1 {
496
+ joinArg := fmt .Sprintf ("--join=%s" , strings .Join (joinAddrs , "," ))
452
497
nodes [i ].startCmdArgs = []string {
453
498
serverArgs .cockroachBinary ,
454
499
startCmd ,
455
500
secureOpt ,
456
501
storeArg + strconv .Itoa (i ),
457
- fmt .Sprintf ("--listen-addr=localhost:%d" , 26257 + i ),
458
- fmt .Sprintf ("--http-addr=localhost:%d" , 8080 + i ),
502
+ fmt .Sprintf ("--listen-addr=localhost:%d" , serverArgs . listenAddrPorts [ i ] ),
503
+ fmt .Sprintf ("--http-addr=localhost:%d" , serverArgs . httpPorts [ i ] ),
459
504
"--listening-url-file=" + nodes [i ].listeningURLFile ,
460
- fmt . Sprintf ( "--join=localhost:%d,localhost:%d,localhost:%d" , 26257 , 26258 , 26259 ) ,
505
+ joinArg ,
461
506
}
462
507
} else {
463
508
nodes [0 ].startCmdArgs = []string {
@@ -467,7 +512,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
467
512
secureOpt ,
468
513
"--host=localhost" ,
469
514
"--port=0" ,
470
- "--http-port=" + strconv .Itoa (serverArgs .httpPort ),
515
+ "--http-port=" + strconv .Itoa (serverArgs .httpPorts [ 0 ] ),
471
516
storeArg ,
472
517
"--listening-url-file=" + nodes [i ].listeningURLFile ,
473
518
}
@@ -480,7 +525,7 @@ func NewTestServer(opts ...TestServerOpt) (TestServer, error) {
480
525
serverArgs .cockroachBinary ,
481
526
"init" ,
482
527
secureOpt ,
483
- "--host=localhost:26259" ,
528
+ fmt . Sprintf ( "--host=localhost:%d" , hostPort ) ,
484
529
}
485
530
486
531
states := make ([]int , serverArgs .numNodes )
0 commit comments