github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/meta/internal/etcdkv/namespace/util_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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // [reference]: https://github.com/etcd-io/etcd/blob/aa75fd08509db3aea8939cdad44e1ee9b8157b8c/client/v3/namespace/util_test.go
    16  
    17  package namespace
    18  
    19  import (
    20  	"testing"
    21  
    22  	"github.com/stretchr/testify/require"
    23  )
    24  
    25  func TestPrefixInterval(t *testing.T) {
    26  	t.Parallel()
    27  
    28  	tests := []struct {
    29  		pfx string
    30  		key []byte
    31  		end []byte
    32  
    33  		wKey []byte
    34  		wEnd []byte
    35  	}{
    36  		// single key
    37  		{
    38  			pfx: "pfx/",
    39  			key: []byte("a"),
    40  
    41  			wKey: []byte("pfx/a"),
    42  		},
    43  		// range
    44  		{
    45  			pfx: "pfx/",
    46  			key: []byte("abc"),
    47  			end: []byte("def"),
    48  
    49  			wKey: []byte("pfx/abc"),
    50  			wEnd: []byte("pfx/def"),
    51  		},
    52  		// one-sided range
    53  		{
    54  			pfx: "pfx/",
    55  			key: []byte("abc"),
    56  			end: []byte{0},
    57  
    58  			wKey: []byte("pfx/abc"),
    59  			wEnd: []byte("pfx0"),
    60  		},
    61  		// one-sided range, end of keyspace
    62  		{
    63  			pfx: "\xff\xff",
    64  			key: []byte("abc"),
    65  			end: []byte{0},
    66  
    67  			wKey: []byte("\xff\xffabc"),
    68  			wEnd: []byte{0},
    69  		},
    70  	}
    71  	for _, tt := range tests {
    72  		pfxKey, pfxEnd := prefixInterval(tt.pfx, tt.key, tt.end)
    73  		require.Equal(t, pfxKey, tt.wKey)
    74  		require.Equal(t, pfxEnd, tt.wEnd)
    75  	}
    76  }