-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathregistrar_test.go
72 lines (65 loc) · 1.35 KB
/
registrar_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
package main
import (
"fmt"
"io"
"os"
"strconv"
"testing"
)
func InitEnv() *Registrar {
recordDir := "records"
registrar := &Registrar{source: "./test/a.log"}
_, err := registrar.OpenRecord(recordDir)
if err != nil {
fmt.Println("OpenRecord failed!")
os.Exit(2)
}
return registrar
}
func TestOpenRecord(t *testing.T) {
recordDir := "records"
registrar := &Registrar{source: "./test/a.log"}
_, err := registrar.OpenRecord(recordDir)
if err != nil {
t.Log("OpenRecord failed!")
t.Fail()
}
t.Log("OpenRecord OK")
}
func TestRecordOffset(t *testing.T) {
registrar := InitEnv()
var offset int64 = 12
if err := registrar.RecordOffset(offset); err != nil {
t.Log("RecordOffset err", err)
t.Fail()
}
//registrar.file.Sync()
r, err := os.Open(registrar.file.Name())
if err != nil {
t.Log("Open record file read failed, err:", err)
t.Fail()
}
buf := make([]byte, 1024)
bytes, err := r.ReadAt(buf, 0)
if err != nil {
if err == io.EOF {
offsetStr := string(buf)
offsetStr = offsetStr[0:bytes]
off, err := strconv.ParseInt(offsetStr, 10, 64)
if off == offset {
fmt.Println("OK", off, "=", offset)
} else {
t.Log("failed, error:", bytes, err)
t.Fail()
}
} else {
t.Log("Read record error:", err)
t.Fail()
}
} else {
t.Log("havn't eof:")
t.Fail()
}
registrar.file.Close()
os.RemoveAll("records")
}