github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/puller/multiplexing_puller_test.go (about)

     1  // Copyright 2023 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 puller
    15  
    16  import (
    17  	"context"
    18  	"sync"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/pingcap/tiflow/cdc/kv"
    23  	"github.com/pingcap/tiflow/cdc/model"
    24  	"github.com/pingcap/tiflow/cdc/processor/tablepb"
    25  	"github.com/pingcap/tiflow/pkg/config"
    26  	"github.com/pingcap/tiflow/pkg/pdutil"
    27  	"github.com/pingcap/tiflow/pkg/spanz"
    28  	"github.com/stretchr/testify/require"
    29  )
    30  
    31  func newMultiplexingPullerForTest(outputCh chan<- *model.RawKVEntry) *MultiplexingPuller {
    32  	cfg := &config.ServerConfig{Debug: &config.DebugConfig{Puller: &config.PullerConfig{LogRegionDetails: false}}}
    33  	client := kv.NewSharedClient(model.ChangeFeedID{}, cfg, false, nil, nil, nil, nil, nil)
    34  	consume := func(ctx context.Context, e *model.RawKVEntry, _ []tablepb.Span) error {
    35  		select {
    36  		case <-ctx.Done():
    37  			return ctx.Err()
    38  		case outputCh <- e:
    39  			return nil
    40  		}
    41  	}
    42  	return NewMultiplexingPuller(
    43  		model.ChangeFeedID{},
    44  		client,
    45  		pdutil.NewClock4Test(),
    46  		consume,
    47  		1, func(tablepb.Span, int) int { return 0 }, 1,
    48  	)
    49  }
    50  
    51  func TestMultiplexingPullerResolvedForward(t *testing.T) {
    52  	outputCh := make(chan *model.RawKVEntry, 16)
    53  	puller := newMultiplexingPullerForTest(outputCh)
    54  	defer puller.client.Close()
    55  
    56  	ctx, cancel := context.WithCancel(context.Background())
    57  	var wg sync.WaitGroup
    58  	wg.Add(1)
    59  	go func() {
    60  		defer wg.Done()
    61  		puller.run(ctx, false)
    62  	}()
    63  
    64  	events := []model.RegionFeedEvent{
    65  		{
    66  			Resolved: &model.ResolvedSpans{
    67  				Spans: []model.RegionComparableSpan{{
    68  					Span: spanz.ToSpan([]byte("t_a"), []byte("t_c")),
    69  				}}, ResolvedTs: uint64(1001),
    70  			},
    71  		},
    72  		{
    73  			Resolved: &model.ResolvedSpans{
    74  				Spans: []model.RegionComparableSpan{{
    75  					Span: spanz.ToSpan([]byte("t_c"), []byte("t_d")),
    76  				}}, ResolvedTs: uint64(1002),
    77  			},
    78  		},
    79  		{
    80  			Resolved: &model.ResolvedSpans{
    81  				Spans: []model.RegionComparableSpan{{
    82  					Span: spanz.ToSpan([]byte("t_d"), []byte("t_e")),
    83  				}}, ResolvedTs: uint64(1000),
    84  			},
    85  		},
    86  	}
    87  
    88  	spans := []tablepb.Span{spanz.ToSpan([]byte("t_a"), []byte("t_e"))}
    89  	spans[0].TableID = 1
    90  	puller.subscribe(spans, 996, "test")
    91  	subID := puller.subscriptions.n.GetV(spans[0]).subID
    92  	for _, event := range events {
    93  		puller.inputChs[0] <- kv.MultiplexingEvent{RegionFeedEvent: event, SubscriptionID: subID}
    94  	}
    95  
    96  	select {
    97  	case ev := <-outputCh:
    98  		require.Equal(t, model.OpTypeResolved, ev.OpType)
    99  		require.Equal(t, uint64(1000), ev.CRTs)
   100  	case <-time.NewTimer(100 * time.Millisecond).C:
   101  		require.True(t, false, "must get an event")
   102  	}
   103  	cancel()
   104  	wg.Wait()
   105  }