-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache_test.go
65 lines (61 loc) · 1.24 KB
/
cache_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
// Copyright (c) 2019 Meng Huang ([email protected])
// This package is licensed under a MIT license that can be found in the LICENSE file.
package raft
import (
"testing"
)
func TestCache(t *testing.T) {
size := 3
c := newCache(size)
if c.length() != 0 {
t.Error()
}
c.dequeue()
if entries := c.CopyAfter(0, size); len(entries) != 0 {
t.Error()
}
index := uint64(1000000)
for i := uint64(0); i < uint64(size); i++ {
index++
c.AppendEntries([]*Entry{{Index: index}})
}
if entries := c.CopyAfter(index-10000, size); len(entries) != 0 {
t.Error()
}
if entries := c.CopyAfter(index+10000, size); len(entries) != 0 {
t.Error()
}
for i := uint64(0); i < 100; i++ {
index++
c.AppendEntries([]*Entry{{Index: index}})
if c.firstIndex() > c.lastIndex() {
t.Error()
}
entries := c.CopyAfter(index-uint64(size)+1, size)
if len(entries) != size {
t.Error()
}
}
c.Reset()
if c.length() != 0 {
t.Error()
}
}
func TestReset(t *testing.T) {
size := 4096
c := newCache(4096)
index := uint64(1000000)
for i := uint64(0); i < uint64(size); i++ {
index++
c.AppendEntries([]*Entry{{Index: index}})
}
c.Reset()
if c.length() != 0 {
t.Error()
}
for i := range c.entries {
if c.entries[i] != nil {
t.Error()
}
}
}