github.com/matrixorigin/matrixone@v1.2.0/pkg/hakeeper/task/cn_pool_test.go (about) 1 // Copyright 2022 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 task 16 17 import ( 18 "container/heap" 19 "testing" 20 21 "github.com/stretchr/testify/assert" 22 ) 23 24 func TestOrderedMap(t *testing.T) { 25 orderedMap := newCNPool() 26 orderedMap.set(cnStore{uuid: "a"}, 100) 27 orderedMap.set(cnStore{uuid: "b"}, 110) 28 orderedMap.set(cnStore{uuid: "c"}, 90) 29 orderedMap.set(cnStore{uuid: "d"}, 80) 30 orderedMap.set(cnStore{uuid: "e"}, 120) 31 orderedMap.set(cnStore{uuid: "f"}, 50) 32 assert.Equal(t, 6, len(orderedMap.freq)) 33 assert.True(t, len(orderedMap.freq) == len(orderedMap.sortedCN)) 34 assert.Equal(t, "f", orderedMap.min().uuid) 35 36 orderedMap.set(cnStore{uuid: "f"}, 150) 37 assert.Equal(t, "d", orderedMap.min().uuid) 38 39 orderedMap.set(cnStore{uuid: "b"}, 70) 40 assert.Equal(t, "b", orderedMap.min().uuid) 41 42 orderedMap.set(cnStore{uuid: "b"}, 100) 43 for i := 0; i < 100; i++ { 44 heap.Push(orderedMap, cnStore{uuid: "g"}) 45 assert.Equal(t, uint32(i+1), orderedMap.getFreq("g")) 46 } 47 assert.Equal(t, "d", orderedMap.min().uuid) 48 assert.Equal(t, 7, len(orderedMap.freq)) 49 assert.True(t, len(orderedMap.freq) == len(orderedMap.sortedCN)) 50 }