-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfile_map_test.go
More file actions
112 lines (102 loc) · 2.48 KB
/
file_map_test.go
File metadata and controls
112 lines (102 loc) · 2.48 KB
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package goutil
import (
"fmt"
"os"
"strconv"
"strings"
"sync"
"testing"
"time"
)
var once sync.Once
var fileMapObj *FileMap
var err error
func ensureInit() {
once.Do(func() {
os.MkdirAll("/tmp/fm", 0777)
fileMapObj, err = NewFileMap("testFileMap", "/tmp/fm", true)
if err != nil {
panic(err)
}
})
}
func TestFileMap_PutAndGet(t *testing.T) {
ensureInit()
for i := 1; i < 1000; i++ {
if err := fileMapObj.Put("testkey"+strconv.Itoa(i), UnsafeStringToBytes(strings.Repeat("testdata"+strconv.Itoa(i), i)), true, time.Second*time.Duration(i)); err != nil {
t.Fatal(err.Error())
}
}
for i := 1; i < 1000; i++ {
if UnsafeBytesToString(fileMapObj.Get("testkey"+strconv.Itoa(i))) != strings.Repeat("testdata"+strconv.Itoa(i), i) {
t.Fatal("get is not equal for put")
}
}
fileMapObj.Close()
fmt.Println("test TestFileMap_PutAndGet ok.")
}
func TestNewFileMap(t *testing.T) {
fm, err := NewFileMap("testFileMap", "/tmp/fm", true)
if err != nil {
t.Fatal(err)
}
defer fm.Close()
for i := 1; i < 1000; i++ {
if UnsafeBytesToString(fm.Get("testkey"+strconv.Itoa(i))) != strings.Repeat("testdata"+strconv.Itoa(i), i) {
t.Fatal("get is not equal for put")
}
}
fmt.Println("test TestNewFileMap ok.")
}
func TestFileMapExpire(t *testing.T) {
time.Sleep(time.Second * 2)
fm, err := NewFileMap("testFileMap", "/tmp/fm", false)
if err != nil {
t.Fatal(err)
}
defer fm.Close()
fm.RemoveExpiredData()
fmt.Printf("after expired, len: %d\n", fm.Len())
//if fm.Len() > 995 {
// t.Fatal("testfilemapexpire fail")
//}
//fmt.Println("testfilemapexpire ok ")
}
func TestFileMap_Pop(t *testing.T) {
fm, err := NewFileMap("testFileMap", "/tmp/fm", true)
if err != nil {
t.Fatal(err)
}
defer fm.Close()
for i := 1; i < 1000; i++ {
ret, _ := fm.Pop(time.Second)
if ret != "testkey"+strconv.Itoa(i) {
t.Fatal("pop data != push key")
}
}
fmt.Println("test TestFileMap_Pop ok.")
}
func TestFileMap_Delete(t *testing.T) {
fm, err := NewFileMap("testFileMap", "/tmp/fm", true)
if err != nil {
t.Fatal(err)
}
defer fm.Close()
for i := 1; i < 1000; i++ {
fm.Delete("testkey" + strconv.Itoa(i))
if UnsafeBytesToString(fm.Get("testkey"+strconv.Itoa(i))) == strings.Repeat("testdata"+strconv.Itoa(i), i) {
t.Fatal("get ok after delete")
}
}
fmt.Println("test TestFileMap_Delete ok.")
}
// final, clean
func TestFileMap_Clear(t *testing.T) {
return
fm, err := NewFileMap("testFileMap", "/tmp/fm", true)
if err != nil {
t.Fatal(err)
}
fm.clear()
fm.Close()
}