forked from grandecola/bigqueue
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex_test.go
91 lines (77 loc) · 2.14 KB
/
index_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
90
91
package bigqueue
import (
"bytes"
"fmt"
"io/ioutil"
"math/rand"
"os"
"path"
"testing"
"github.com/stretchr/testify/assert"
)
func createTestDir(t *testing.T, testDir string) {
if _, err := os.Stat(testDir); os.IsNotExist(err) {
if err := os.Mkdir(testDir, cFilePerm); err != nil {
t.Fatalf("unable to create test dir: %v", err)
}
}
}
func deleteTestDir(t *testing.T, testDir string) {
if err := os.RemoveAll(testDir); err != nil {
t.Fatalf("unable to delete test dir: %v", err)
}
}
func TestIndex(t *testing.T) {
testDir := path.Join(os.TempDir(), fmt.Sprintf("testdir_%d", rand.Intn(1000)))
createTestDir(t, testDir)
defer deleteTestDir(t, testDir)
qi, err := newQueueIndex(testDir)
if err != nil {
t.Fatal("error in creating new queue index ::", err)
}
var aid, offset int
aid, offset = qi.getHead()
assert.Equal(t, 0, aid)
assert.Equal(t, 0, offset)
aid, offset = qi.getTail()
assert.Equal(t, 0, aid)
assert.Equal(t, 0, offset)
qi.putHead(0, 8)
aid, offset = qi.getHead()
assert.Equal(t, 0, aid)
assert.Equal(t, 8, offset)
qi.putHead(7, 98)
aid, offset = qi.getHead()
assert.Equal(t, 7, aid)
assert.Equal(t, 98, offset)
aid, offset = qi.getTail()
assert.Equal(t, 0, aid)
assert.Equal(t, 0, offset)
qi.putTail(9, 127*1024*1024)
aid, offset = qi.getTail()
assert.Equal(t, 9, aid)
assert.Equal(t, 127*1024*1024, offset)
arenaSize := 8 * 1024 * 1024
qi.putArenaSize(arenaSize)
assert.Equal(t, qi.getArenaSize(), arenaSize)
if errFlush := qi.flush(); errFlush != nil {
t.Fatalf("error in calling flush: %v", errFlush)
}
indexFile := path.Join(testDir, cIndexFileName)
fd, err := os.Open(indexFile)
if err != nil {
t.Fatal("error in opening index file ::", err)
}
data, err := ioutil.ReadAll(fd)
if err != nil {
t.Fatal("error in reading index file ::", err)
}
expected := []byte{0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x62, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x09, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0xf0, 0x07, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00}
if !bytes.Equal(expected, data) {
t.Fatal("index file has unexpected content")
}
}