2013-09-29 03:26:19 +04:00
|
|
|
package store
|
2013-09-03 22:30:42 +04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
|
|
|
// TestEventQueue tests a queue with capacity = 100
|
|
|
|
// Add 200 events into that queue, and test if the
|
|
|
|
// previous 100 events have been swapped out.
|
|
|
|
func TestEventQueue(t *testing.T) {
|
|
|
|
|
|
|
|
eh := newEventHistory(100)
|
|
|
|
|
|
|
|
// Add
|
|
|
|
for i := 0; i < 200; i++ {
|
2013-11-10 05:55:54 +04:00
|
|
|
e := newEvent(Create, "/foo", uint64(i))
|
2013-09-03 22:30:42 +04:00
|
|
|
eh.addEvent(e)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test
|
|
|
|
j := 100
|
2013-09-14 01:10:40 +04:00
|
|
|
i := eh.Queue.Front
|
|
|
|
n := eh.Queue.Size
|
2013-09-08 17:55:54 +04:00
|
|
|
for ; n > 0; n-- {
|
2013-09-14 01:10:40 +04:00
|
|
|
e := eh.Queue.Events[i]
|
2013-11-10 08:49:19 +04:00
|
|
|
if e.Index() != uint64(j) {
|
2013-09-03 22:30:42 +04:00
|
|
|
t.Fatalf("queue error!")
|
|
|
|
}
|
|
|
|
j++
|
2013-09-14 01:10:40 +04:00
|
|
|
i = (i + 1) % eh.Queue.Capacity
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestScanHistory(t *testing.T) {
|
|
|
|
eh := newEventHistory(100)
|
|
|
|
|
|
|
|
// Add
|
2013-11-10 05:55:54 +04:00
|
|
|
eh.addEvent(newEvent(Create, "/foo", 1))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/bar", 2))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/foo", 3))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/bar/bar", 4))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/foo/foo", 5))
|
2013-09-03 22:30:42 +04:00
|
|
|
|
|
|
|
e, err := eh.scan("/foo", 1)
|
2013-11-10 08:49:19 +04:00
|
|
|
if err != nil || e.Index() != 1 {
|
2013-11-10 05:55:54 +04:00
|
|
|
t.Fatalf("scan error [/foo] [1] %v", e.Index)
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
e, err = eh.scan("/foo/bar", 1)
|
|
|
|
|
2013-11-10 08:49:19 +04:00
|
|
|
if err != nil || e.Index() != 2 {
|
2013-11-10 05:55:54 +04:00
|
|
|
t.Fatalf("scan error [/foo/bar] [2] %v", e.Index)
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
e, err = eh.scan("/foo/bar", 3)
|
|
|
|
|
2013-11-10 08:49:19 +04:00
|
|
|
if err != nil || e.Index() != 4 {
|
2013-11-10 05:55:54 +04:00
|
|
|
t.Fatalf("scan error [/foo/bar/bar] [4] %v", e.Index)
|
2013-09-03 22:30:42 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
e, err = eh.scan("/foo/bar", 6)
|
|
|
|
|
|
|
|
if e != nil {
|
|
|
|
t.Fatalf("bad index shoud reuturn nil")
|
|
|
|
}
|
|
|
|
}
|