github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/processor/sourcemanager/sorter/mounted_iter_test.go (about)

     1  // Copyright 2022 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 sorter
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  
    20  	"github.com/pingcap/tiflow/cdc/entry"
    21  	"github.com/pingcap/tiflow/cdc/model"
    22  	"github.com/pingcap/tiflow/cdc/processor/memquota"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  type mockIter struct {
    27  	repeatItem func() *model.PolymorphicEvent
    28  }
    29  
    30  func (i *mockIter) Next() (*model.PolymorphicEvent, Position, error) {
    31  	return i.repeatItem(), Position{}, nil
    32  }
    33  
    34  func (i *mockIter) Close() error {
    35  	return nil
    36  }
    37  
    38  func TestMountedEventIter(t *testing.T) {
    39  	t.Parallel()
    40  
    41  	rawIter := &mockIter{
    42  		repeatItem: func() *model.PolymorphicEvent {
    43  			return &model.PolymorphicEvent{
    44  				RawKV: &model.RawKVEntry{
    45  					Key:   []byte("testbytes"),
    46  					Value: []byte("testbytes"),
    47  				},
    48  				Row: &model.RowChangedEvent{
    49  					TableInfo: &model.TableInfo{
    50  						TableName: model.TableName{
    51  							Schema: "schema",
    52  							Table:  "table",
    53  						},
    54  						IndexColumnsOffset: [][]int{{1}},
    55  					},
    56  				},
    57  			}
    58  		},
    59  	}
    60  
    61  	mg := &entry.MockMountGroup{}
    62  	quota := memquota.NewMemQuota(model.ChangeFeedID{}, 1024*1024, "test")
    63  	defer quota.Close()
    64  	iter := NewMountedEventIter(model.ChangeFeedID{}, rawIter, mg, 3, quota)
    65  
    66  	for i := 0; i < 3; i++ {
    67  		event, _, err := iter.Next(context.Background())
    68  		require.NotNil(t, event)
    69  		require.Nil(t, err)
    70  		if i != 2 {
    71  			require.NotZero(t, quota.GetUsedBytes())
    72  		} else {
    73  			require.Zero(t, quota.GetUsedBytes())
    74  		}
    75  	}
    76  	require.Equal(t, 3, iter.nextToEmit)
    77  
    78  	rawIter.repeatItem = func() *model.PolymorphicEvent { return nil }
    79  	event, _, err := iter.Next(context.Background())
    80  	require.Nil(t, event)
    81  	require.Nil(t, err)
    82  	require.Equal(t, iter.nextToEmit, 0)
    83  	require.Nil(t, iter.iter)
    84  	require.Nil(t, iter.Close())
    85  }
    86  
    87  func TestMountedEventIterReadBatch(t *testing.T) {
    88  	t.Parallel()
    89  
    90  	rawIter := &mockIter{
    91  		repeatItem: func() *model.PolymorphicEvent {
    92  			return &model.PolymorphicEvent{
    93  				RawKV: &model.RawKVEntry{
    94  					Key:   []byte("testbytes"),
    95  					Value: []byte("testbytes"),
    96  				},
    97  			}
    98  		},
    99  	}
   100  
   101  	quota := memquota.NewMemQuota(model.ChangeFeedID{}, 1024*1024, "test")
   102  	defer quota.Close()
   103  
   104  	mg := &entry.MockMountGroup{IsFull: true}
   105  	iter := NewMountedEventIter(model.ChangeFeedID{}, rawIter, mg, 3, quota)
   106  
   107  	iter.readBatch(context.Background())
   108  	require.NotNil(t, iter.rawEventBuffer.event)
   109  	require.Equal(t, 1, len(iter.rawEvents))
   110  
   111  	rawIter.repeatItem = func() *model.PolymorphicEvent { return nil }
   112  	iter.readBatch(context.Background())
   113  	require.Nil(t, iter.rawEventBuffer.event)
   114  	require.Equal(t, 1, len(iter.rawEvents))
   115  
   116  	require.Equal(t, uint64(36), quota.GetUsedBytes())
   117  }