github.com/pingcap/tidb-lightning@v5.0.0-rc.0.20210428090220-84b649866577+incompatible/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/tidb-lightning/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 go func() { 43 start := time.Now() 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" 51 } 52 return true, "" 53 case <-time.After(max): 54 return false, "WaitGroup did not unblock after maximum duration" 55 } 56 } 57 58 var _ = Suite(&pauseSuite{}) 59 60 type pauseSuite struct{} 61 62 func (s *pauseSuite) TestPause(c *C) { 63 var wg sync.WaitGroup 64 p := common.NewPauser() 65 66 // initially these calls should not be blocking. 67 68 wg.Add(10) 69 for i := 0; i < 10; i++ { 70 go func() { 71 defer wg.Done() 72 err := p.Wait(context.Background()) 73 c.Assert(err, IsNil) 74 }() 75 } 76 77 c.Assert(&wg, unblocksBetween, 0*time.Millisecond, 10*time.Millisecond) 78 79 // after calling Pause(), these should be blocking... 80 81 p.Pause() 82 83 wg.Add(10) 84 for i := 0; i < 10; i++ { 85 go func() { 86 defer wg.Done() 87 err := p.Wait(context.Background()) 88 c.Assert(err, IsNil) 89 }() 90 } 91 92 // ... until we call Resume() 93 94 go func() { 95 time.Sleep(500 * time.Millisecond) 96 p.Resume() 97 }() 98 99 c.Assert(&wg, unblocksBetween, 500*time.Millisecond, 520*time.Millisecond) 100 101 // if the context is canceled, Wait() should immediately unblock... 102 103 ctx, cancel := context.WithCancel(context.Background()) 104 105 p.Pause() 106 107 wg.Add(10) 108 for i := 0; i < 10; i++ { 109 go func() { 110 defer wg.Done() 111 err := p.Wait(ctx) 112 c.Assert(err, Equals, context.Canceled) 113 }() 114 } 115 116 cancel() 117 c.Assert(&wg, unblocksBetween, 0*time.Millisecond, 10*time.Millisecond) 118 119 // canceling the context does not affect the state of the pauser 120 121 wg.Add(1) 122 go func() { 123 defer wg.Done() 124 err := p.Wait(context.Background()) 125 c.Assert(err, IsNil) 126 }() 127 128 go func() { 129 time.Sleep(500 * time.Millisecond) 130 p.Resume() 131 }() 132 133 c.Assert(&wg, unblocksBetween, 500*time.Millisecond, 520*time.Millisecond) 134 } 135 136 // Run `go test github.com/pingcap/tidb-lightning/lightning/common -check.b -test.v` to get benchmark result. 137 func (s *pauseSuite) BenchmarkWaitNoOp(c *C) { 138 p := common.NewPauser() 139 ctx := context.Background() 140 for i := 0; i < c.N; i++ { 141 p.Wait(ctx) 142 } 143 } 144 145 func (s *pauseSuite) BenchmarkWaitCtxCanceled(c *C) { 146 p := common.NewPauser() 147 p.Pause() 148 ctx, cancel := context.WithCancel(context.Background()) 149 cancel() 150 for i := 0; i < c.N; i++ { 151 p.Wait(ctx) 152 } 153 } 154 155 func (s *pauseSuite) BenchmarkWaitContended(c *C) { 156 p := common.NewPauser() 157 158 done := make(chan struct{}) 159 defer close(done) 160 go func() { 161 isPaused := false 162 for { 163 select { 164 case <-done: 165 return 166 default: 167 if isPaused { 168 p.Pause() 169 } else { 170 p.Resume() 171 } 172 isPaused = !isPaused 173 } 174 } 175 }() 176 177 ctx := context.Background() 178 for i := 0; i < c.N; i++ { 179 p.Wait(ctx) 180 } 181 }