github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/owner/barrier_test.go (about)

     1  // Copyright 2021 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 owner
    15  
    16  import (
    17  	"math"
    18  	"math/rand"
    19  	"testing"
    20  
    21  	"github.com/pingcap/check"
    22  	"github.com/pingcap/ticdc/cdc/model"
    23  	"github.com/pingcap/ticdc/pkg/util/testleak"
    24  )
    25  
    26  func Test(t *testing.T) { check.TestingT(t) }
    27  
    28  var _ = check.Suite(&barrierSuite{})
    29  
    30  type barrierSuite struct {
    31  }
    32  
    33  func (s *barrierSuite) TestBarrier(c *check.C) {
    34  	defer testleak.AfterTest(c)()
    35  	b := newBarriers()
    36  	b.Update(ddlJobBarrier, 2)
    37  	b.Update(syncPointBarrier, 3)
    38  	b.Update(finishBarrier, 1)
    39  	tp, ts := b.Min()
    40  	c.Assert(tp, check.Equals, finishBarrier)
    41  	c.Assert(ts, check.Equals, uint64(1))
    42  
    43  	b.Update(finishBarrier, 4)
    44  	tp, ts = b.Min()
    45  	c.Assert(tp, check.Equals, ddlJobBarrier)
    46  	c.Assert(ts, check.Equals, uint64(2))
    47  
    48  	b.Remove(ddlJobBarrier)
    49  	tp, ts = b.Min()
    50  	c.Assert(tp, check.Equals, syncPointBarrier)
    51  	c.Assert(ts, check.Equals, uint64(3))
    52  
    53  	b.Update(finishBarrier, 1)
    54  	tp, ts = b.Min()
    55  	c.Assert(tp, check.Equals, finishBarrier)
    56  	c.Assert(ts, check.Equals, uint64(1))
    57  
    58  	b.Update(ddlJobBarrier, 5)
    59  	tp, ts = b.Min()
    60  	c.Assert(tp, check.Equals, finishBarrier)
    61  	c.Assert(ts, check.Equals, uint64(1))
    62  }
    63  
    64  func (s *barrierSuite) TestBarrierRandom(c *check.C) {
    65  	defer testleak.AfterTest(c)()
    66  	maxBarrierType := 50
    67  	maxBarrierTs := 1000000
    68  	b := newBarriers()
    69  	expectedBarriers := make(map[barrierType]model.Ts)
    70  
    71  	// set a barrier which can not be removed to avoid the barrier map is empty
    72  	b.Update(barrierType(maxBarrierType), model.Ts(maxBarrierTs))
    73  	expectedBarriers[barrierType(maxBarrierType)] = model.Ts(maxBarrierTs)
    74  
    75  	for i := 0; i < 100000; i++ {
    76  		switch rand.Intn(2) {
    77  		case 0:
    78  			tp := barrierType(rand.Intn(maxBarrierType))
    79  			ts := model.Ts(rand.Intn(maxBarrierTs))
    80  			b.Update(tp, ts)
    81  			expectedBarriers[tp] = ts
    82  		case 1:
    83  			tp := barrierType(rand.Intn(maxBarrierType))
    84  			b.Remove(tp)
    85  			delete(expectedBarriers, tp)
    86  		}
    87  		expectedMinTs := uint64(math.MaxUint64)
    88  		for _, ts := range expectedBarriers {
    89  			if ts < expectedMinTs {
    90  				expectedMinTs = ts
    91  			}
    92  		}
    93  		tp, ts := b.Min()
    94  		c.Assert(ts, check.Equals, expectedMinTs)
    95  		c.Assert(expectedBarriers[tp], check.Equals, expectedMinTs)
    96  	}
    97  }