github.com/pingcap/br@v5.3.0-alpha.0.20220125034240-ec59c7b6ce30+incompatible/pkg/lightning/common/pause_test.go (about)

     1  // Copyright 2019 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 common_test
    15  
    16  import (
    17  	"context"
    18  	"sync"
    19  	"time"
    20  
    21  	. "github.com/pingcap/check"
    22  
    23  	"github.com/pingcap/br/pkg/lightning/common"
    24  )
    25  
    26  // unblocksAfter is a checker which ensures the WaitGroup's Wait() method
    27  // returns between the given durations.
    28  var unblocksBetween Checker = &unblocksChecker{
    29  	&CheckerInfo{Name: "unblocksBetween", Params: []string{"waitGroupPtr", "min", "max"}},
    30  }
    31  
    32  type unblocksChecker struct {
    33  	*CheckerInfo
    34  }
    35  
    36  func (checker *unblocksChecker) Check(params []interface{}, names []string) (bool, string) {
    37  	wg := params[0].(*sync.WaitGroup)
    38  	min := params[1].(time.Duration)
    39  	max := params[2].(time.Duration)
    40  
    41  	ch := make(chan time.Duration)
    42  	start := time.Now()
    43  	go func() {
    44  		wg.Wait()
    45  		ch <- time.Since(start)
    46  	}()
    47  	select {
    48  	case dur := <-ch:
    49  		if dur < min {
    50  			return false, "WaitGroup unblocked before minimum duration, it was " + dur.String()
    51  		}
    52  		return true, ""
    53  	case <-time.After(max):
    54  		select {
    55  		case dur := <-ch:
    56  			return false, "WaitGroup did not unblock after maximum duration, it was " + dur.String()
    57  		case <-time.After(1 * time.Second):
    58  			return false, "WaitGroup did not unblock after maximum duration"
    59  		}
    60  	}
    61  }
    62  
    63  var _ = Suite(&pauseSuite{})
    64  
    65  type pauseSuite struct{}
    66  
    67  func (s *pauseSuite) TestPause(c *C) {
    68  	var wg sync.WaitGroup
    69  	p := common.NewPauser()
    70  
    71  	// initially these calls should not be blocking.
    72  
    73  	wg.Add(10)
    74  	for i := 0; i < 10; i++ {
    75  		go func() {
    76  			defer wg.Done()
    77  			err := p.Wait(context.Background())
    78  			c.Assert(err, IsNil)
    79  		}()
    80  	}
    81  
    82  	// Give them more time to unblock in case of time exceeding due to high pressure of CI.
    83  	c.Assert(&wg, unblocksBetween, 0*time.Millisecond, 100*time.Millisecond)
    84  
    85  	// after calling Pause(), these should be blocking...
    86  
    87  	p.Pause()
    88  
    89  	wg.Add(10)
    90  	for i := 0; i < 10; i++ {
    91  		go func() {
    92  			defer wg.Done()
    93  			err := p.Wait(context.Background())
    94  			c.Assert(err, IsNil)
    95  		}()
    96  	}
    97  
    98  	// ... until we call Resume()
    99  
   100  	go func() {
   101  		time.Sleep(500 * time.Millisecond)
   102  		p.Resume()
   103  	}()
   104  
   105  	// Give them more time to unblock in case of time exceeding due to high pressure of CI.
   106  	c.Assert(&wg, unblocksBetween, 500*time.Millisecond, 800*time.Millisecond)
   107  
   108  	// if the context is canceled, Wait() should immediately unblock...
   109  
   110  	ctx, cancel := context.WithCancel(context.Background())
   111  
   112  	p.Pause()
   113  
   114  	wg.Add(10)
   115  	for i := 0; i < 10; i++ {
   116  		go func() {
   117  			defer wg.Done()
   118  			err := p.Wait(ctx)
   119  			c.Assert(err, Equals, context.Canceled)
   120  		}()
   121  	}
   122  
   123  	cancel()
   124  	// Give them more time to unblock in case of time exceeding due to high pressure of CI.
   125  	c.Assert(&wg, unblocksBetween, 0*time.Millisecond, 100*time.Millisecond)
   126  
   127  	// canceling the context does not affect the state of the pauser
   128  
   129  	wg.Add(1)
   130  	go func() {
   131  		defer wg.Done()
   132  		err := p.Wait(context.Background())
   133  		c.Assert(err, IsNil)
   134  	}()
   135  
   136  	go func() {
   137  		time.Sleep(500 * time.Millisecond)
   138  		p.Resume()
   139  	}()
   140  
   141  	// Give them more time to unblock in case of time exceeding due to high pressure of CI.
   142  	c.Assert(&wg, unblocksBetween, 500*time.Millisecond, 800*time.Millisecond)
   143  }
   144  
   145  // Run `go test github.com/pingcap/br/pkg/lightning/common -check.b -test.v` to get benchmark result.
   146  func (s *pauseSuite) BenchmarkWaitNoOp(c *C) {
   147  	p := common.NewPauser()
   148  	ctx := context.Background()
   149  	for i := 0; i < c.N; i++ {
   150  		_ = p.Wait(ctx)
   151  	}
   152  }
   153  
   154  func (s *pauseSuite) BenchmarkWaitCtxCanceled(c *C) {
   155  	p := common.NewPauser()
   156  	p.Pause()
   157  	ctx, cancel := context.WithCancel(context.Background())
   158  	cancel()
   159  	for i := 0; i < c.N; i++ {
   160  		_ = p.Wait(ctx)
   161  	}
   162  }
   163  
   164  func (s *pauseSuite) BenchmarkWaitContended(c *C) {
   165  	p := common.NewPauser()
   166  
   167  	done := make(chan struct{})
   168  	defer close(done)
   169  	go func() {
   170  		isPaused := false
   171  		for {
   172  			select {
   173  			case <-done:
   174  				return
   175  			default:
   176  				if isPaused {
   177  					p.Pause()
   178  				} else {
   179  					p.Resume()
   180  				}
   181  				isPaused = !isPaused
   182  			}
   183  		}
   184  	}()
   185  
   186  	ctx := context.Background()
   187  	for i := 0; i < c.N; i++ {
   188  		_ = p.Wait(ctx)
   189  	}
   190  }