github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/model/mounter_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 model
    15  
    16  import (
    17  	"context"
    18  	"sync"
    19  
    20  	"github.com/pingcap/check"
    21  	"github.com/pingcap/ticdc/pkg/util/testleak"
    22  )
    23  
    24  type mounterSuite struct{}
    25  
    26  var _ = check.Suite(&mounterSuite{})
    27  
    28  func (s *mounterSuite) TestPolymorphicEvent(c *check.C) {
    29  	defer testleak.AfterTest(c)()
    30  	raw := &RawKVEntry{
    31  		StartTs:  99,
    32  		CRTs:     100,
    33  		OpType:   OpTypePut,
    34  		RegionID: 2,
    35  	}
    36  	resolved := &RawKVEntry{
    37  		OpType: OpTypeResolved,
    38  		CRTs:   101,
    39  	}
    40  
    41  	polyEvent := NewPolymorphicEvent(raw)
    42  	c.Assert(polyEvent.RawKV, check.DeepEquals, raw)
    43  	c.Assert(polyEvent.CRTs, check.Equals, raw.CRTs)
    44  	c.Assert(polyEvent.StartTs, check.Equals, raw.StartTs)
    45  	c.Assert(polyEvent.RegionID(), check.Equals, raw.RegionID)
    46  
    47  	rawResolved := &RawKVEntry{CRTs: resolved.CRTs, OpType: OpTypeResolved}
    48  	polyEvent = NewPolymorphicEvent(resolved)
    49  	c.Assert(polyEvent.RawKV, check.DeepEquals, rawResolved)
    50  	c.Assert(polyEvent.CRTs, check.Equals, resolved.CRTs)
    51  	c.Assert(polyEvent.StartTs, check.Equals, uint64(0))
    52  }
    53  
    54  func (s *mounterSuite) TestPolymorphicEventPrepare(c *check.C) {
    55  	defer testleak.AfterTest(c)()
    56  	ctx := context.Background()
    57  	polyEvent := NewPolymorphicEvent(&RawKVEntry{OpType: OpTypeResolved})
    58  	c.Assert(polyEvent.WaitPrepare(ctx), check.IsNil)
    59  
    60  	polyEvent = NewPolymorphicEvent(&RawKVEntry{OpType: OpTypePut})
    61  	polyEvent.SetUpFinishedChan()
    62  	var wg sync.WaitGroup
    63  	wg.Add(1)
    64  	go func() {
    65  		defer wg.Done()
    66  		err := polyEvent.WaitPrepare(ctx)
    67  		c.Assert(err, check.IsNil)
    68  	}()
    69  	polyEvent.PrepareFinished()
    70  	wg.Wait()
    71  
    72  	cctx, cancel := context.WithCancel(ctx)
    73  	polyEvent = NewPolymorphicEvent(&RawKVEntry{OpType: OpTypePut})
    74  	polyEvent.SetUpFinishedChan()
    75  	cancel()
    76  	err := polyEvent.WaitPrepare(cctx)
    77  	c.Assert(err, check.Equals, context.Canceled)
    78  }