heap_test.go 667 B

123456789101112131415161718192021222324252627282930313233343536
  1. package store
  2. import (
  3. "container/heap"
  4. "fmt"
  5. "testing"
  6. "time"
  7. )
  8. func TestHeapPushPop(t *testing.T) {
  9. h := &TTLKeyHeap{Map: make(map[*Node]int)}
  10. heap.Init(h)
  11. kvs := make([]*Node, 10)
  12. // add from older expire time to earlier expire time
  13. // the path is equal to ttl from now
  14. for i, n := range kvs {
  15. path := fmt.Sprintf("%v", 10-i)
  16. m := time.Duration(10 - i)
  17. n = newKV(nil, path, path, 0, 0, nil, "", time.Now().Add(time.Second*m))
  18. heap.Push(h, n)
  19. }
  20. min := time.Now()
  21. for i := 0; i < 9; i++ {
  22. iNode := heap.Pop(h)
  23. node, _ := iNode.(*Node)
  24. if node.ExpireTime.Before(min) {
  25. t.Fatal("heap sort wrong!")
  26. }
  27. min = node.ExpireTime
  28. }
  29. }