Skip to content

Commit 8615e75

Browse files
add ut
Signed-off-by: 923048992@qq.com <923048992@qq.com>
1 parent 283ad72 commit 8615e75

File tree

1 file changed

+45
-0
lines changed

1 file changed

+45
-0
lines changed

pkg/cache/v2/cluster_test.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -345,3 +345,48 @@ func BenchmarkClusterFlush(b *testing.B) {
345345
assert.Equal(t, core_v2.ApiStatus_NONE, cluster.GetApiStatus())
346346
}
347347
}
348+
349+
func TestClearClusterStats(t *testing.T) {
350+
config := options.BpfConfig{
351+
Mode: "ads",
352+
BpfFsPath: "/sys/fs/bpf",
353+
Cgroup2Path: "/mnt/kmesh_cgroup2",
354+
}
355+
cleanup, loader := test.InitBpfMap(t, config)
356+
t.Cleanup(cleanup)
357+
358+
adsObj := loader.GetBpfKmesh()
359+
hashName := utils.NewHashName()
360+
clusterCache := NewClusterCache(adsObj, hashName)
361+
testClusters := []string{"test_cluster1", "test_cluster2", "test_cluster3"}
362+
testKeys := []ClusterStatsKey{
363+
{NetnsCookie: 0, ClusterId: hashName.StrToNum(testClusters[0])},
364+
{NetnsCookie: 1, ClusterId: hashName.StrToNum(testClusters[0])},
365+
{NetnsCookie: 2, ClusterId: hashName.StrToNum(testClusters[0])},
366+
{NetnsCookie: 0, ClusterId: hashName.StrToNum(testClusters[1])},
367+
{NetnsCookie: 0, ClusterId: hashName.StrToNum(testClusters[2])},
368+
}
369+
370+
testValues := []ClusterStatsValue{
371+
{ActiveConnections: 1},
372+
{ActiveConnections: 2},
373+
{ActiveConnections: 3},
374+
{ActiveConnections: 4},
375+
{ActiveConnections: 5},
376+
}
377+
378+
clusterStatsMap := adsObj.GetClusterStatsMap()
379+
clusterStatsMap.BatchUpdate(testKeys, testValues, nil)
380+
381+
var key ClusterStatsKey
382+
var value ClusterStatsValue
383+
for _, cluster := range testClusters {
384+
clusterCache.clearClusterStats(cluster)
385+
iter := clusterStatsMap.Iterate()
386+
clusterId := hashName.StrToNum(cluster)
387+
for iter.Next(&key, &value) {
388+
assert.NotEqual(t, clusterId, key.ClusterId)
389+
}
390+
assert.Nil(t, iter.Err())
391+
}
392+
}

0 commit comments

Comments
 (0)