-
Notifications
You must be signed in to change notification settings - Fork 11
/
store_test.go
76 lines (62 loc) · 1.75 KB
/
store_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
package main
import "github.com/bmizerany/assert"
import _ "net/http/pprof"
import (
"testing"
"log"
_ "regexp" //makes things work with go -race
"net/http"
"fmt"
)
func TestSerialize(t *testing.T) {
for i := 1; i < 100000; i++ {
read, err := readSequence(writeSequence(int64(i)))
assert.T(t, err == nil, err)
assert.T(t, read == int64(i))
}
}
func TestEncodeDecodeEvent(t *testing.T){
event := &Event{Channel: "test123", Body: []byte("test123")}
encoded, err := encodeEvent(event)
assert.T(t, err == nil, err)
decoded, err := decodeEvent(encoded)
assert.T(t, err == nil, err)
assert.T(t, event.Channel == decoded.Channel, fmt.Sprintf("%v+ %v+", event, decoded))
assert.T(t, string(event.Body) == string(decoded.Body), fmt.Sprintf("%v+ %v+", event, decoded))
}
func TestOpenCloseStore(t *testing.T) {
go func() {
log.Println(http.ListenAndServe("localhost:6060", nil))
}()
for i := 0; i < 100; i++ {
store, err := OpenStore("test.db")
assert.T(t, err == nil, err)
init := store.getWritePointer()
acks := make(chan bool)
for i := 0; i < 100; i++ {
store.EventsInChannel() <- &EventIn{ event: &Event{ Channel:"test", Body:[]byte("BODY") }, saved:acks}
<-acks
}
curr := store.getWritePointer()
assert.Equal(t, init+100, curr)
log.Println("CLOSING")
store.Close()
store, err = OpenStore("test.db")
assert.T(t, err == nil, err)
init = store.getWritePointer()
assert.Equal(t, init, curr)
store.Close()
}
}
func BenchmarkEvents(b *testing.B) {
store, _ := OpenStore("test.db")
acks := make(chan bool)
b.ResetTimer()
for i := 0; i < b.N ; i++ {
store.EventsInChannel() <- &EventIn{ event: &Event {Channel:"test", Body:[]byte("BODY")}, saved:acks}
<-acks
}
b.StopTimer()
log.Println("CLOSING")
store.Close()
}