1// Copyright 2015 The etcd Authors 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 15package store 16 17import ( 18 "fmt" 19 "testing" 20 "time" 21) 22 23func TestHeapPushPop(t *testing.T) { 24 h := newTtlKeyHeap() 25 26 // add from older expire time to earlier expire time 27 // the path is equal to ttl from now 28 for i := 0; i < 10; i++ { 29 path := fmt.Sprintf("%v", 10-i) 30 m := time.Duration(10 - i) 31 n := newKV(nil, path, path, 0, nil, time.Now().Add(time.Second*m)) 32 h.push(n) 33 } 34 35 min := time.Now() 36 37 for i := 0; i < 10; i++ { 38 node := h.pop() 39 if node.ExpireTime.Before(min) { 40 t.Fatal("heap sort wrong!") 41 } 42 min = node.ExpireTime 43 } 44 45} 46 47func TestHeapUpdate(t *testing.T) { 48 h := newTtlKeyHeap() 49 50 kvs := make([]*node, 10) 51 52 // add from older expire time to earlier expire time 53 // the path is equal to ttl from now 54 for i := range kvs { 55 path := fmt.Sprintf("%v", 10-i) 56 m := time.Duration(10 - i) 57 n := newKV(nil, path, path, 0, nil, time.Now().Add(time.Second*m)) 58 kvs[i] = n 59 h.push(n) 60 } 61 62 // Path 7 63 kvs[3].ExpireTime = time.Now().Add(time.Second * 11) 64 65 // Path 5 66 kvs[5].ExpireTime = time.Now().Add(time.Second * 12) 67 68 h.update(kvs[3]) 69 h.update(kvs[5]) 70 71 min := time.Now() 72 73 for i := 0; i < 10; i++ { 74 node := h.pop() 75 if node.ExpireTime.Before(min) { 76 t.Fatal("heap sort wrong!") 77 } 78 min = node.ExpireTime 79 80 if i == 8 { 81 if node.Path != "7" { 82 t.Fatal("heap sort wrong!", node.Path) 83 } 84 } 85 86 if i == 9 { 87 if node.Path != "5" { 88 t.Fatal("heap sort wrong!") 89 } 90 } 91 92 } 93 94} 95