github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/pkg/pdutil/utils_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 pdutil
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  	"time"
    20  
    21  	"github.com/pingcap/tidb/pkg/kv"
    22  	"github.com/pingcap/tidb/pkg/session"
    23  	"github.com/pingcap/tidb/pkg/store/mockstore"
    24  	"github.com/stretchr/testify/require"
    25  )
    26  
    27  func TestGetSourceID(t *testing.T) {
    28  	store, err := mockstore.NewMockStore()
    29  	require.NoError(t, err)
    30  	defer func() {
    31  		err := store.Close()
    32  		require.NoError(t, err)
    33  	}()
    34  	domain, err := session.BootstrapSession(store)
    35  	require.NoError(t, err)
    36  	defer domain.Close()
    37  	se, err := session.CreateSession4Test(store)
    38  	require.NoError(t, err)
    39  	_, err = se.Execute(context.Background(), "set @@global.tidb_source_id=2;")
    40  	require.NoError(t, err)
    41  	require.Eventually(t, func() bool {
    42  		client := store.(kv.StorageWithPD).GetPDClient()
    43  		sourceID, err := GetSourceID(context.Background(), client)
    44  		require.NoError(t, err)
    45  		return sourceID == 2
    46  	}, 5*time.Second, 100*time.Millisecond)
    47  }