github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/pkg/notify/notify_test.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package notify
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/pingcap/check"
    22  	"github.com/pingcap/ticdc/pkg/errors"
    23  	"github.com/pingcap/ticdc/pkg/util/testleak"
    24  )
    25  
    26  func Test(t *testing.T) {
    27  	check.TestingT(t)
    28  }
    29  
    30  type notifySuite struct{}
    31  
    32  var _ = check.Suite(&notifySuite{})
    33  
    34  func (s *notifySuite) TestNotifyHub(c *check.C) {
    35  	defer testleak.AfterTest(c)()
    36  	notifier := new(Notifier)
    37  	r1, err := notifier.NewReceiver(-1)
    38  	c.Assert(err, check.IsNil)
    39  	r2, err := notifier.NewReceiver(-1)
    40  	c.Assert(err, check.IsNil)
    41  	r3, err := notifier.NewReceiver(-1)
    42  	c.Assert(err, check.IsNil)
    43  	finishedCh := make(chan struct{})
    44  	go func() {
    45  		for i := 0; i < 5; i++ {
    46  			time.Sleep(time.Second)
    47  			notifier.Notify()
    48  		}
    49  		close(finishedCh)
    50  	}()
    51  	<-r1.C
    52  	r1.Stop()
    53  	<-r2.C
    54  	<-r3.C
    55  
    56  	r2.Stop()
    57  	r3.Stop()
    58  	c.Assert(len(notifier.receivers), check.Equals, 0)
    59  	r4, err := notifier.NewReceiver(-1)
    60  	c.Assert(err, check.IsNil)
    61  	<-r4.C
    62  	r4.Stop()
    63  
    64  	notifier2 := new(Notifier)
    65  	r5, err := notifier2.NewReceiver(10 * time.Millisecond)
    66  	c.Assert(err, check.IsNil)
    67  	<-r5.C
    68  	r5.Stop()
    69  	<-finishedCh // To make the leak checker happy
    70  }
    71  
    72  func (s *notifySuite) TestContinusStop(c *check.C) {
    73  	defer testleak.AfterTest(c)()
    74  	ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
    75  	defer cancel()
    76  	notifier := new(Notifier)
    77  	go func() {
    78  		for {
    79  			select {
    80  			case <-ctx.Done():
    81  				return
    82  			default:
    83  			}
    84  			notifier.Notify()
    85  		}
    86  	}()
    87  	n := 50
    88  	receivers := make([]*Receiver, n)
    89  	var err error
    90  	for i := 0; i < n; i++ {
    91  		receivers[i], err = notifier.NewReceiver(10 * time.Millisecond)
    92  		c.Assert(err, check.IsNil)
    93  	}
    94  	for i := 0; i < n; i++ {
    95  		i := i
    96  		go func() {
    97  			for {
    98  				select {
    99  				case <-ctx.Done():
   100  					return
   101  				case <-receivers[i].C:
   102  				}
   103  			}
   104  		}()
   105  	}
   106  	for i := 0; i < n; i++ {
   107  		receivers[i].Stop()
   108  	}
   109  	<-ctx.Done()
   110  }
   111  
   112  func (s *notifySuite) TestNewReceiverWithClosedNotifier(c *check.C) {
   113  	defer testleak.AfterTest(c)()
   114  	notifier := new(Notifier)
   115  	notifier.Close()
   116  	_, err := notifier.NewReceiver(50 * time.Millisecond)
   117  	c.Assert(errors.ErrOperateOnClosedNotifier.Equal(err), check.IsTrue)
   118  }