github.com/ghodss/etcd@v0.3.1-0.20140417172404-cc329bfa55cb/store/heap_test.go (about) 1 package store 2 3 import ( 4 "fmt" 5 "testing" 6 "time" 7 ) 8 9 func TestHeapPushPop(t *testing.T) { 10 h := newTtlKeyHeap() 11 12 // add from older expire time to earlier expire time 13 // the path is equal to ttl from now 14 for i := 0; i < 10; i++ { 15 path := fmt.Sprintf("%v", 10-i) 16 m := time.Duration(10 - i) 17 n := newKV(nil, path, path, 0, nil, "", time.Now().Add(time.Second*m)) 18 h.push(n) 19 } 20 21 min := time.Now() 22 23 for i := 0; i < 10; i++ { 24 node := h.pop() 25 if node.ExpireTime.Before(min) { 26 t.Fatal("heap sort wrong!") 27 } 28 min = node.ExpireTime 29 } 30 31 } 32 33 func TestHeapUpdate(t *testing.T) { 34 h := newTtlKeyHeap() 35 36 kvs := make([]*node, 10) 37 38 // add from older expire time to earlier expire time 39 // the path is equal to ttl from now 40 for i, n := range kvs { 41 path := fmt.Sprintf("%v", 10-i) 42 m := time.Duration(10 - i) 43 n = newKV(nil, path, path, 0, nil, "", time.Now().Add(time.Second*m)) 44 kvs[i] = n 45 h.push(n) 46 } 47 48 // Path 7 49 kvs[3].ExpireTime = time.Now().Add(time.Second * 11) 50 51 // Path 5 52 kvs[5].ExpireTime = time.Now().Add(time.Second * 12) 53 54 h.update(kvs[3]) 55 h.update(kvs[5]) 56 57 min := time.Now() 58 59 for i := 0; i < 10; i++ { 60 node := h.pop() 61 if node.ExpireTime.Before(min) { 62 t.Fatal("heap sort wrong!") 63 } 64 min = node.ExpireTime 65 66 if i == 8 { 67 if node.Path != "7" { 68 t.Fatal("heap sort wrong!", node.Path) 69 } 70 } 71 72 if i == 9 { 73 if node.Path != "5" { 74 t.Fatal("heap sort wrong!") 75 } 76 } 77 78 } 79 80 }