github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/meta/example_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 meta
    15  
    16  import (
    17  	"context"
    18  	"testing"
    19  	"time"
    20  
    21  	metaMock "github.com/pingcap/tiflow/engine/pkg/meta/mock"
    22  	metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model"
    23  	"github.com/stretchr/testify/require"
    24  )
    25  
    26  // Backend KV store data:
    27  //|		KEY		|		VALUE	|
    28  //|		apple	|		 red	|
    29  //|		orange  |		orange	|
    30  //|		data	|		flow	|
    31  //|		ticdc	|		kv		|
    32  //|		dm		|		DDL		|
    33  
    34  // nolint: ineffassign
    35  func Test(t *testing.T) {
    36  	// clientConn, err := NewClientConn(metaModel.DefaultStoreConfig())
    37  	clientConn := metaMock.NewMockClientConn()
    38  	cli, _ := NewKVClientWithNamespace(clientConn, "fakeProject", "fakeJob")
    39  	defer cli.Close()
    40  	ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
    41  	defer cancel()
    42  
    43  	prepareData(ctx, cli)
    44  
    45  	var (
    46  		putRsp *metaModel.PutResponse
    47  		getRsp *metaModel.GetResponse
    48  		delRsp *metaModel.DeleteResponse
    49  		txnRsp *metaModel.TxnResponse
    50  	)
    51  
    52  	//
    53  	// Basic Put/Get/Delete
    54  	//
    55  	putRsp, err := cli.Put(ctx, "TiDB", "DistDB")
    56  	require.NoError(t, err)
    57  
    58  	// always get latest revision data
    59  	getRsp, err = cli.Get(ctx, "TiDB")
    60  	require.NoError(t, err)
    61  	require.Len(t, getRsp.Kvs, 1)
    62  
    63  	kv := getRsp.Kvs[0]
    64  	require.Equal(t, []byte("TiDB"), kv.Key)
    65  	require.Equal(t, []byte("DistDB"), kv.Value)
    66  
    67  	delRsp, err = cli.Delete(ctx, "TiDB")
    68  	require.NoError(t, err)
    69  
    70  	getRsp, err = cli.Get(ctx, "TiDB")
    71  	require.NoError(t, err)
    72  	require.Len(t, getRsp.Kvs, 0)
    73  
    74  	//
    75  	//	Options: Key Range/From Key/Key Prefix attributes
    76  	//
    77  	// Key Range, forbit Put using metaModel.WithRange
    78  	// current data:
    79  	//		apple  red
    80  	//		orange orange
    81  	//		ticdc  kv
    82  	//		dm	   DDL
    83  	getRsp, err = cli.Get(ctx, "ap", metaModel.WithRange("zz"))
    84  	// expect len(getRsp.Kvs) == 4(apple. orange, ticdc, dm)
    85  	getRsp, err = cli.Get(ctx, "ap", metaModel.WithRange("apple2"))
    86  	// expect len(getRsp.Kvs) == 1(apple)
    87  	delRsp, err = cli.Delete(ctx, "dzst", metaModel.WithRange("panda"))
    88  	// delete key orange
    89  
    90  	// From Key, forbit Put using metaModel.WithFromKey
    91  	// current data:
    92  	//		apple  red
    93  	//		ticdc  kv
    94  	//		dm	   DDL
    95  	getRsp, err = cli.Get(ctx, "data", metaModel.WithFromKey())
    96  	// expect len(getRsp.Kvs) == 2(ticdc, dm)
    97  	delRsp, err = cli.Delete(ctx, "tian", metaModel.WithFromKey())
    98  	// delete key ticdc
    99  
   100  	// Key Prefix, forbit Put using metaModel.WithPrefix
   101  	// current data:
   102  	//		apple  red
   103  	//		apple2  green
   104  	//		ticdc  kv
   105  	//		dm	   DDL
   106  	getRsp, err = cli.Get(ctx, "apple", metaModel.WithPrefix())
   107  	// expect len(getRsp.Kvs) == 2(apple, apple2)
   108  	delRsp, err = cli.Delete(ctx, "apple", metaModel.WithPrefix())
   109  	// delete key apple, apple2
   110  
   111  	//
   112  	// Txn, forbit nested txn
   113  	//
   114  	// current data:
   115  	//		apple  red
   116  	//		apple2  green
   117  	//		ticdc  kv
   118  	//		dm	   DDL
   119  	getOp := metaModel.OpGet("apple3", metaModel.WithRange("zz"))
   120  	putOp := metaModel.OpPut("apple3", "t3")
   121  	delOp := metaModel.OpDelete("apple3", metaModel.WithRange("ti"))
   122  	txn := cli.Txn(ctx)
   123  	txnRsp, err = txn.Do(getOp).Do(putOp).Do(delOp).Commit()
   124  	// When succeed, txnRsp will contain a getRsp, a putRsp and a delRsp
   125  	// txnRsp.ResponseOp[0].GetResponseGet()
   126  	// txnRsp.ResponseOp[1].GetResponsePut()
   127  	// txnRsp.ResponseOp[2].GetResponseDelete()
   128  	// When failed, all ops will take no effect.
   129  
   130  	epoch, _ := cli.GenEpoch(ctx)
   131  	// expect epoch is always an increasing int64
   132  
   133  	_ = epoch
   134  	_ = getRsp
   135  	_ = putRsp
   136  	_ = delRsp
   137  	_ = txnRsp
   138  }
   139  
   140  func prepareData(ctx context.Context, cli metaModel.KVClient) {
   141  	cli.Put(ctx, "apple", "red")
   142  	cli.Put(ctx, "orange", "orange")
   143  	cli.Put(ctx, "data", "flow")
   144  	cli.Put(ctx, "ticdc", "kv")
   145  	cli.Put(ctx, "dm", "DDL")
   146  }