@@ -373,8 +373,9 @@ func TestCoordinatorScheduling(t *testing.T) {
373373}
374374
375375func TestScaleNode (t * testing.T ) {
376- ctx := context .Background ()
377- info := node .NewInfo ("127.0.0.1:28300" , "" )
376+ ctx , cancel := context .WithCancel (context .Background ())
377+ t .Cleanup (cancel )
378+ info , lis1 := newMaintainerNodeForTest (t )
378379 etcdClient := newMockEtcdClient (string (info .ID ))
379380 nodeManager := watcher .NewNodeManager (nil , etcdClient )
380381 appcontext .SetService (watcher .NodeManagerName , nodeManager )
@@ -384,13 +385,10 @@ func TestScaleNode(t *testing.T) {
384385 cfg := config .NewDefaultMessageCenterConfig (info .AdvertiseAddr )
385386 mc1 := messaging .NewMessageCenter (ctx , info .ID , cfg , nil )
386387 mc1 .Run (ctx )
387- defer func () {
388- mc1 .Close ()
389- log .Info ("close message center 1" )
390- }()
391388
392389 appcontext .SetService (appcontext .MessageCenter , mc1 )
393- startMaintainerNode (ctx , info , mc1 , nodeManager )
390+ node1 := startMaintainerNode (ctx , info , mc1 , nodeManager , lis1 )
391+ t .Cleanup (node1 .stop )
394392
395393 serviceID := "default"
396394
@@ -428,23 +426,16 @@ func TestScaleNode(t *testing.T) {
428426 }, waitTime , time .Millisecond * 5 )
429427
430428 // add two nodes
431- info2 := node . NewInfo ( "127.0.0.1:28400" , "" )
429+ info2 , lis2 := newMaintainerNodeForTest ( t )
432430 mc2 := messaging .NewMessageCenter (ctx , info2 .ID , config .NewDefaultMessageCenterConfig (info2 .AdvertiseAddr ), nil )
433431 mc2 .Run (ctx )
434- defer func () {
435- mc2 .Close ()
436- log .Info ("close message center 2" )
437- }()
438- startMaintainerNode (ctx , info2 , mc2 , nodeManager )
439- info3 := node .NewInfo ("127.0.0.1:28500" , "" )
432+ node2 := startMaintainerNode (ctx , info2 , mc2 , nodeManager , lis2 )
433+ t .Cleanup (node2 .stop )
434+ info3 , lis3 := newMaintainerNodeForTest (t )
440435 mc3 := messaging .NewMessageCenter (ctx , info3 .ID , config .NewDefaultMessageCenterConfig (info3 .AdvertiseAddr ), nil )
441436 mc3 .Run (ctx )
442- defer func () {
443- mc3 .Close ()
444- log .Info ("close message center 3" )
445- }()
446-
447- startMaintainerNode (ctx , info3 , mc3 , nodeManager )
437+ node3 := startMaintainerNode (ctx , info3 , mc3 , nodeManager , lis3 )
438+ t .Cleanup (node3 .stop )
448439
449440 log .Info ("Start maintainer node" ,
450441 zap .Stringer ("id" , info3 .ID ),
@@ -490,7 +481,7 @@ func TestScaleNode(t *testing.T) {
490481func TestBootstrapWithUnStoppedChangefeed (t * testing.T ) {
491482 ctx , cancel := context .WithCancel (context .Background ())
492483 defer cancel ()
493- info := node . NewInfo ( "127.0.0.1:28301" , "" )
484+ info , lis := newMaintainerNodeForTest ( t )
494485 etcdClient := newMockEtcdClient (string (info .ID ))
495486 nodeManager := watcher .NewNodeManager (nil , etcdClient )
496487 appcontext .SetService (watcher .NodeManagerName , nodeManager )
@@ -500,10 +491,10 @@ func TestBootstrapWithUnStoppedChangefeed(t *testing.T) {
500491
501492 mc1 := messaging .NewMessageCenter (ctx , info .ID , config .NewDefaultMessageCenterConfig (info .AdvertiseAddr ), nil )
502493 mc1 .Run (ctx )
503- defer mc1 .Close ()
504494
505495 appcontext .SetService (appcontext .MessageCenter , mc1 )
506- mNode := startMaintainerNode (ctx , info , mc1 , nodeManager )
496+ mNode := startMaintainerNode (ctx , info , mc1 , nodeManager , lis )
497+ defer mNode .stop ()
507498
508499 removingCf1 := & changefeed.ChangefeedMetaWrapper {
509500 Info : & config.ChangeFeedInfo {
@@ -712,40 +703,51 @@ type maintainNode struct {
712703 cancel context.CancelFunc
713704 mc messaging.MessageCenter
714705 manager * mockMaintainerManager
706+ wg sync.WaitGroup
715707}
716708
717709func (d * maintainNode ) stop () {
718- d .mc .Close ()
719710 d .cancel ()
711+ d .wg .Wait ()
712+ d .mc .Close ()
713+ }
714+
715+ func newMaintainerNodeForTest (t * testing.T ) (* node.Info , net.Listener ) {
716+ t .Helper ()
717+
718+ lis , err := net .Listen ("tcp" , "127.0.0.1:0" )
719+ require .NoError (t , err )
720+
721+ return node .NewInfo (lis .Addr ().String (), "" ), lis
720722}
721723
722724func startMaintainerNode (ctx context.Context ,
723725 node * node.Info , mc messaging.MessageCenter ,
724726 nodeManager * watcher.NodeManager ,
727+ lis net.Listener ,
725728) * maintainNode {
726729 nodeManager .RegisterNodeChangeHandler (node .ID , mc .OnNodeChanges )
727730 ctx , cancel := context .WithCancel (ctx )
728731 maintainerM := NewMaintainerManager (mc )
732+ res := & maintainNode {
733+ cancel : cancel ,
734+ mc : mc ,
735+ manager : maintainerM ,
736+ }
737+ res .wg .Add (1 )
729738 go func () {
739+ defer res .wg .Done ()
730740 var opts []grpc.ServerOption
731741 grpcServer := grpc .NewServer (opts ... )
732742 mcs := messaging .NewMessageCenterServer (mc )
733743 proto .RegisterMessageServiceServer (grpcServer , mcs )
734- lis , err := net .Listen ("tcp" , node .AdvertiseAddr )
735- if err != nil {
736- panic (err )
737- }
738744 go func () {
739745 _ = grpcServer .Serve (lis )
740746 }()
741747 _ = maintainerM .Run (ctx )
742748 grpcServer .Stop ()
743749 }()
744- return & maintainNode {
745- cancel : cancel ,
746- mc : mc ,
747- manager : maintainerM ,
748- }
750+ return res
749751}
750752
751753type mockEtcdClient struct {
0 commit comments