forked from jellydator/ttlcache
-
Notifications
You must be signed in to change notification settings - Fork 0
/
priority_queue_test.go
89 lines (77 loc) · 2.45 KB
/
priority_queue_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
package ttlcache
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestPriorityQueuePush(t *testing.T) {
queue := newPriorityQueue()
for i := 0; i < 10; i++ {
queue.push(newItem(fmt.Sprintf("key_%d", i), "data", -1))
}
assert.Equal(t, queue.Len(), 10, "Expected queue to have 10 elements")
}
func TestPriorityQueuePop(t *testing.T) {
queue := newPriorityQueue()
for i := 0; i < 10; i++ {
queue.push(newItem(fmt.Sprintf("key_%d", i), "data", -1))
}
for i := 0; i < 5; i++ {
item := queue.pop()
assert.Equal(t, fmt.Sprintf("%T", item), "*ttlcache.item", "Expected 'item' to be a '*ttlcache.item'")
}
assert.Equal(t, queue.Len(), 5, "Expected queue to have 5 elements")
for i := 0; i < 5; i++ {
item := queue.pop()
assert.Equal(t, fmt.Sprintf("%T", item), "*ttlcache.item", "Expected 'item' to be a '*ttlcache.item'")
}
assert.Equal(t, queue.Len(), 0, "Expected queue to have 0 elements")
item := queue.pop()
assert.Nil(t, item, "*ttlcache.item", "Expected 'item' to be nil")
}
func TestPriorityQueueCheckOrder(t *testing.T) {
queue := newPriorityQueue()
for i := 10; i > 0; i-- {
queue.push(newItem(fmt.Sprintf("key_%d", i), "data", time.Duration(i)*time.Second))
}
for i := 1; i <= 10; i++ {
item := queue.pop()
assert.Equal(t, item.key, fmt.Sprintf("key_%d", i), "error")
}
}
func TestPriorityQueueRemove(t *testing.T) {
queue := newPriorityQueue()
items := make(map[string]*item)
var itemRemove *item
for i := 0; i < 5; i++ {
key := fmt.Sprintf("key_%d", i)
items[key] = newItem(key, "data", time.Duration(i)*time.Second)
queue.push(items[key])
if i == 2 {
itemRemove = items[key]
}
}
assert.Equal(t, queue.Len(), 5, "Expected queue to have 5 elements")
queue.remove(itemRemove)
assert.Equal(t, queue.Len(), 4, "Expected queue to have 4 elements")
for {
item := queue.pop()
if item == nil {
break
}
assert.NotEqual(t, itemRemove.key, item.key, "This element was not supose to be in the queue")
}
assert.Equal(t, queue.Len(), 0, "The queue is supose to be with 0 items")
}
func TestPriorityQueueUpdate(t *testing.T) {
queue := newPriorityQueue()
item := newItem("key", "data", 1*time.Second)
queue.push(item)
assert.Equal(t, queue.Len(), 1, "The queue is supose to be with 1 item")
item.key = "newKey"
queue.update(item)
newItem := queue.pop()
assert.Equal(t, newItem.key, "newKey", "The item key didn't change")
assert.Equal(t, queue.Len(), 0, "The queue is supose to be with 0 items")
}