2015-01-25 06:19:16 +03:00
|
|
|
// Copyright 2015 CoreOS, Inc.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2014-10-18 02:41:22 +04:00
|
|
|
|
2013-09-29 03:26:19 +04:00
|
|
|
package store
|
2013-09-03 22:30:42 +04:00
|
|
|
|
2016-01-12 09:42:05 +03:00
|
|
|
import "testing"
|
2013-09-03 22:30:42 +04:00
|
|
|
|
|
|
|
// 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-29 06:34:38 +04:00
|
|
|
e := newEvent(Create, "/foo", uint64(i), 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-29 06:34:38 +04:00
|
|
|
eh.addEvent(newEvent(Create, "/foo", 1, 1))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/bar", 2, 2))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/foo", 3, 3))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4))
|
|
|
|
eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5))
|
2013-09-03 22:30:42 +04:00
|
|
|
|
2016-01-12 09:42:05 +03:00
|
|
|
// Delete a dir
|
|
|
|
de := newEvent(Delete, "/foo", 6, 6)
|
|
|
|
de.PrevNode = newDir(nil, "/foo", 1, nil, Permanent).Repr(false, false, nil)
|
|
|
|
eh.addEvent(de)
|
|
|
|
|
2013-12-01 09:47:23 +04:00
|
|
|
e, err := eh.scan("/foo", false, 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
|
|
|
}
|
|
|
|
|
2013-12-01 09:47:23 +04:00
|
|
|
e, err = eh.scan("/foo/bar", false, 1)
|
2013-09-03 22:30:42 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2013-12-01 09:47:23 +04:00
|
|
|
e, err = eh.scan("/foo/bar", true, 3)
|
2013-09-03 22:30:42 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2016-01-12 09:42:05 +03:00
|
|
|
e, err = eh.scan("/foo/foo/foo", false, 6)
|
|
|
|
if err != nil || e.Index() != 6 {
|
|
|
|
t.Fatalf("scan error [/foo/foo/foo] [6] %v", e.Index)
|
|
|
|
}
|
2013-09-03 22:30:42 +04:00
|
|
|
|
2016-01-12 09:42:05 +03:00
|
|
|
e, err = eh.scan("/foo/bar", true, 7)
|
2013-09-03 22:30:42 +04:00
|
|
|
if e != nil {
|
|
|
|
t.Fatalf("bad index shoud reuturn nil")
|
|
|
|
}
|
|
|
|
}
|
2013-12-23 03:38:59 +04:00
|
|
|
|
|
|
|
// TestFullEventQueue tests a queue with capacity = 10
|
|
|
|
// Add 1000 events into that queue, and test if scanning
|
|
|
|
// works still for previous events.
|
|
|
|
func TestFullEventQueue(t *testing.T) {
|
|
|
|
|
|
|
|
eh := newEventHistory(10)
|
|
|
|
|
|
|
|
// Add
|
|
|
|
for i := 0; i < 1000; i++ {
|
2015-02-18 00:15:48 +03:00
|
|
|
ce := newEvent(Create, "/foo", uint64(i), uint64(i))
|
|
|
|
eh.addEvent(ce)
|
2013-12-23 03:38:59 +04:00
|
|
|
e, err := eh.scan("/foo", true, uint64(i-1))
|
|
|
|
if i > 0 {
|
|
|
|
if e == nil || err != nil {
|
|
|
|
t.Fatalf("scan error [/foo] [%v] %v", i-1, i)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2014-10-29 21:52:36 +03:00
|
|
|
|
|
|
|
func TestCloneEvent(t *testing.T) {
|
|
|
|
e1 := &Event{
|
|
|
|
Action: Create,
|
|
|
|
EtcdIndex: 1,
|
|
|
|
Node: nil,
|
|
|
|
PrevNode: nil,
|
|
|
|
}
|
|
|
|
e2 := e1.Clone()
|
|
|
|
if e2.Action != Create {
|
|
|
|
t.Fatalf("Action=%q, want %q", e2.Action, Create)
|
|
|
|
}
|
|
|
|
if e2.EtcdIndex != e1.EtcdIndex {
|
|
|
|
t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, e1.EtcdIndex)
|
|
|
|
}
|
|
|
|
// Changing the cloned node should not affect the original
|
|
|
|
e2.Action = Delete
|
|
|
|
e2.EtcdIndex = uint64(5)
|
|
|
|
if e1.Action != Create {
|
|
|
|
t.Fatalf("Action=%q, want %q", e1.Action, Create)
|
|
|
|
}
|
|
|
|
if e1.EtcdIndex != uint64(1) {
|
|
|
|
t.Fatalf("EtcdIndex=%d, want %d", e1.EtcdIndex, uint64(1))
|
|
|
|
}
|
|
|
|
if e2.Action != Delete {
|
|
|
|
t.Fatalf("Action=%q, want %q", e2.Action, Delete)
|
|
|
|
}
|
|
|
|
if e2.EtcdIndex != uint64(5) {
|
|
|
|
t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, uint64(5))
|
|
|
|
}
|
|
|
|
}
|