github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/processor/sinkmanager/tasks_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 sinkmanager
    15  
    16  import (
    17  	"testing"
    18  	"time"
    19  
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/cdc/processor/sourcemanager/sorter"
    22  	"github.com/pingcap/tiflow/pkg/spanz"
    23  	"github.com/stretchr/testify/require"
    24  	"github.com/tikv/client-go/v2/oracle"
    25  )
    26  
    27  func TestValidateAndAdjustBound(t *testing.T) {
    28  	for _, tc := range []struct {
    29  		name          string
    30  		lowerBound    sorter.Position
    31  		taskTimeRange time.Duration
    32  		expectAdjust  bool
    33  	}{
    34  		{
    35  			name: "bigger than maxTaskTimeRange",
    36  			lowerBound: sorter.Position{
    37  				StartTs:  439333515018895365,
    38  				CommitTs: 439333515018895366,
    39  			},
    40  			taskTimeRange: 60 * time.Minute,
    41  			expectAdjust:  true,
    42  		},
    43  		{
    44  			name: "smaller than maxTaskTimeRange",
    45  			lowerBound: sorter.Position{
    46  				StartTs:  439333515018895365,
    47  				CommitTs: 439333515018895366,
    48  			},
    49  			taskTimeRange: 1 * time.Second,
    50  			expectAdjust:  false,
    51  		},
    52  	} {
    53  		t.Run(tc.name, func(t *testing.T) {
    54  			changefeedID := model.DefaultChangeFeedID("1")
    55  			span := spanz.TableIDToComparableSpan(1)
    56  			lowerPhs := oracle.GetTimeFromTS(tc.lowerBound.CommitTs)
    57  			newUpperCommitTs := oracle.GoTimeToTS(lowerPhs.Add(tc.taskTimeRange))
    58  			upperBound := sorter.GenCommitFence(newUpperCommitTs)
    59  			newLowerBound, newUpperBound := validateAndAdjustBound(changefeedID,
    60  				&span, tc.lowerBound, upperBound)
    61  			if tc.expectAdjust {
    62  				lowerPhs := oracle.GetTimeFromTS(newLowerBound.CommitTs)
    63  				upperPhs := oracle.GetTimeFromTS(newUpperBound.CommitTs)
    64  				require.Equal(t, maxTaskTimeRange, upperPhs.Sub(lowerPhs))
    65  			} else {
    66  				require.Equal(t, tc.lowerBound, newLowerBound)
    67  				require.Equal(t, upperBound, newUpperBound)
    68  			}
    69  		})
    70  	}
    71  }