ledisdb/ledis/event_test.go

57 lines
867 B
Go
Raw Normal View History

2014-09-17 13:54:04 +04:00
package ledis
import (
"reflect"
"testing"
)
2014-09-22 13:50:51 +04:00
type testEvent struct {
Key []byte
Value []byte
}
type testEventWriter struct {
evs []testEvent
}
func (w *testEventWriter) Put(key []byte, value []byte) {
e := testEvent{key, value}
w.evs = append(w.evs, e)
}
func (w *testEventWriter) Delete(key []byte) {
e := testEvent{key, nil}
w.evs = append(w.evs, e)
}
2014-09-17 13:54:04 +04:00
func TestEvent(t *testing.T) {
k1 := []byte("k1")
v1 := []byte("v1")
k2 := []byte("k2")
k3 := []byte("k3")
v3 := []byte("v3")
b := new(eventBatch)
b.Put(k1, v1)
b.Delete(k2)
b.Put(k3, v3)
buf := b.Bytes()
2014-09-22 13:50:51 +04:00
w := &testEventWriter{}
ev2 := &testEventWriter{
evs: []testEvent{
testEvent{k1, v1},
testEvent{k2, nil},
testEvent{k3, v3}},
2014-09-17 13:54:04 +04:00
}
2014-09-22 13:50:51 +04:00
if err := decodeEventBatch(w, buf); err != nil {
2014-09-17 13:54:04 +04:00
t.Fatal(err)
2014-09-22 13:50:51 +04:00
} else if !reflect.DeepEqual(w, ev2) {
2014-09-17 13:54:04 +04:00
t.Fatal("not equal")
}
}