github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/txnutil/gc/testing.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 gc
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	"github.com/pingcap/kvproto/pkg/metapb"
    21  	"github.com/tikv/client-go/v2/oracle"
    22  	pd "github.com/tikv/pd/client"
    23  )
    24  
    25  // MockPDClient mocks pd.Client to facilitate unit testing.
    26  type MockPDClient struct {
    27  	pd.Client
    28  	ClusterID        uint64
    29  	GetAllStoresFunc func(ctx context.Context, opts ...pd.GetStoreOption) ([]*metapb.Store, error)
    30  
    31  	UpdateServiceGCSafePointFunc func(ctx context.Context, serviceID string, ttl int64, safePoint uint64) (uint64, error)
    32  }
    33  
    34  // UpdateServiceGCSafePoint implements pd.Client.UpdateServiceGCSafePoint.
    35  func (m *MockPDClient) UpdateServiceGCSafePoint(ctx context.Context, serviceID string, ttl int64, safePoint uint64) (uint64, error) {
    36  	return m.UpdateServiceGCSafePointFunc(ctx, serviceID, ttl, safePoint)
    37  }
    38  
    39  // GetTS implements pd.Client.GetTS.
    40  func (m *MockPDClient) GetTS(ctx context.Context) (int64, int64, error) {
    41  	return oracle.GetPhysical(time.Now()), 0, nil
    42  }
    43  
    44  // Close implements pd.Client.Close()
    45  // This method is used in some unit test cases.
    46  func (m *MockPDClient) Close() {}
    47  
    48  // GetClusterID gets the cluster ID from PD.
    49  func (m *MockPDClient) GetClusterID(ctx context.Context) uint64 {
    50  	return m.ClusterID
    51  }
    52  
    53  // GetAllStores gets all stores from PD.
    54  func (m *MockPDClient) GetAllStores(
    55  	ctx context.Context, opts ...pd.GetStoreOption,
    56  ) ([]*metapb.Store, error) {
    57  	return m.GetAllStoresFunc(ctx, opts...)
    58  }
    59  
    60  // LoadGlobalConfig loads global config from PD.
    61  func (m *MockPDClient) LoadGlobalConfig(
    62  	ctx context.Context,
    63  	names []string, configPath string,
    64  ) ([]pd.GlobalConfigItem, int64, error) {
    65  	return []pd.GlobalConfigItem{
    66  		{
    67  			Name:  "source_id",
    68  			Value: "1",
    69  		},
    70  	}, 0, nil
    71  }