github.com/kula/etcd@v0.2.1-0.20131226070625-e96234382ac0/store/event_test.go (about)

     1  package store
     2  
     3  import (
     4  	"testing"
     5  )
     6  
     7  // TestEventQueue tests a queue with capacity = 100
     8  // Add 200 events into that queue, and test if the
     9  // previous 100 events have been swapped out.
    10  func TestEventQueue(t *testing.T) {
    11  
    12  	eh := newEventHistory(100)
    13  
    14  	// Add
    15  	for i := 0; i < 200; i++ {
    16  		e := newEvent(Create, "/foo", uint64(i), uint64(i))
    17  		eh.addEvent(e)
    18  	}
    19  
    20  	// Test
    21  	j := 100
    22  	i := eh.Queue.Front
    23  	n := eh.Queue.Size
    24  	for ; n > 0; n-- {
    25  		e := eh.Queue.Events[i]
    26  		if e.Index() != uint64(j) {
    27  			t.Fatalf("queue error!")
    28  		}
    29  		j++
    30  		i = (i + 1) % eh.Queue.Capacity
    31  	}
    32  }
    33  
    34  func TestScanHistory(t *testing.T) {
    35  	eh := newEventHistory(100)
    36  
    37  	// Add
    38  	eh.addEvent(newEvent(Create, "/foo", 1, 1))
    39  	eh.addEvent(newEvent(Create, "/foo/bar", 2, 2))
    40  	eh.addEvent(newEvent(Create, "/foo/foo", 3, 3))
    41  	eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4))
    42  	eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5))
    43  
    44  	e, err := eh.scan("/foo", false, 1)
    45  	if err != nil || e.Index() != 1 {
    46  		t.Fatalf("scan error [/foo] [1] %v", e.Index)
    47  	}
    48  
    49  	e, err = eh.scan("/foo/bar", false, 1)
    50  
    51  	if err != nil || e.Index() != 2 {
    52  		t.Fatalf("scan error [/foo/bar] [2] %v", e.Index)
    53  	}
    54  
    55  	e, err = eh.scan("/foo/bar", true, 3)
    56  
    57  	if err != nil || e.Index() != 4 {
    58  		t.Fatalf("scan error [/foo/bar/bar] [4] %v", e.Index)
    59  	}
    60  
    61  	e, err = eh.scan("/foo/bar", true, 6)
    62  
    63  	if e != nil {
    64  		t.Fatalf("bad index shoud reuturn nil")
    65  	}
    66  }
    67  
    68  // TestFullEventQueue tests a queue with capacity = 10
    69  // Add 1000 events into that queue, and test if scanning
    70  // works still for previous events.
    71  func TestFullEventQueue(t *testing.T) {
    72  
    73  	eh := newEventHistory(10)
    74  
    75  	// Add
    76  	for i := 0; i < 1000; i++ {
    77  		e := newEvent(Create, "/foo", uint64(i), uint64(i))
    78  		eh.addEvent(e)
    79  		e, err := eh.scan("/foo", true, uint64(i-1))
    80  		if i > 0 {
    81  			if e == nil || err != nil {
    82  				t.Fatalf("scan error [/foo] [%v] %v", i-1, i)
    83  			}
    84  		}
    85  	}
    86  }