event_test.go 1.8 KB

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