github.com/matrixorigin/matrixone@v1.2.0/pkg/gossip/base_item_test.go (about)

     1  // Copyright 2021 - 2024 Matrix Origin
     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  package gossip
    16  
    17  import (
    18  	"github.com/matrixorigin/matrixone/pkg/pb/gossip"
    19  	"github.com/matrixorigin/matrixone/pkg/pb/query"
    20  	"github.com/matrixorigin/matrixone/pkg/pb/statsinfo"
    21  	"github.com/stretchr/testify/assert"
    22  	"testing"
    23  )
    24  
    25  func TestDataCacheKey_AddItem(t *testing.T) {
    26  	dc := newBaseStore[query.CacheKey]("127.0.0.1:8888")
    27  	assert.NotNil(t, dc)
    28  	k := query.CacheKey{
    29  		Path:   "p1",
    30  		Offset: 10,
    31  		Sz:     10,
    32  	}
    33  	dc.AddItem(gossip.CommonItem{
    34  		Operation: gossip.Operation_Set,
    35  		Key: &gossip.CommonItem_CacheKey{
    36  			CacheKey: &k,
    37  		},
    38  	})
    39  	assert.Equal(t, 1, len(dc.queueMu.itemQueue))
    40  }
    41  
    42  func TestStatsInfoKey_AddItem(t *testing.T) {
    43  	si := newBaseStore[statsinfo.StatsInfoKey]("127.0.0.1:8888")
    44  	assert.NotNil(t, si)
    45  	k := statsinfo.StatsInfoKey{
    46  		DatabaseID: 1,
    47  		TableID:    2,
    48  	}
    49  	si.AddItem(gossip.CommonItem{
    50  		Operation: gossip.Operation_Set,
    51  		Key: &gossip.CommonItem_StatsInfoKey{
    52  			StatsInfoKey: &k,
    53  		},
    54  	})
    55  	assert.Equal(t, 1, len(si.queueMu.itemQueue))
    56  }
    57  
    58  func TestDataCacheKey_Target(t *testing.T) {
    59  	dc := newBaseStore[query.CacheKey]("127.0.0.1:8888")
    60  	assert.NotNil(t, dc)
    61  	k1 := query.CacheKey{
    62  		Path:   "p1",
    63  		Offset: 10,
    64  		Sz:     10,
    65  	}
    66  	k2 := query.CacheKey{
    67  		Path:   "p2",
    68  		Offset: 20,
    69  		Sz:     20,
    70  	}
    71  	dc.update("127.0.0.1:8889", []query.CacheKey{k1})
    72  	target := dc.Target(k1)
    73  	assert.Equal(t, "127.0.0.1:8889", target)
    74  
    75  	target = dc.Target(k2)
    76  	assert.Equal(t, "", target)
    77  }
    78  
    79  func TestStatsInfoKey_Target(t *testing.T) {
    80  	dc := newBaseStore[statsinfo.StatsInfoKey]("127.0.0.1:8888")
    81  	assert.NotNil(t, dc)
    82  	k1 := statsinfo.StatsInfoKey{
    83  		DatabaseID: 11,
    84  		TableID:    12,
    85  	}
    86  	k2 := statsinfo.StatsInfoKey{
    87  		DatabaseID: 21,
    88  		TableID:    22,
    89  	}
    90  	dc.update("127.0.0.1:8889", []statsinfo.StatsInfoKey{k1})
    91  	target := dc.Target(k1)
    92  	assert.Equal(t, "127.0.0.1:8889", target)
    93  
    94  	target = dc.Target(k2)
    95  	assert.Equal(t, "", target)
    96  }