event_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. Copyright 2014 CoreOS, Inc.
  3. Licensed under the Apache License, Version 2.0 (the "License");
  4. you may not use this file except in compliance with the License.
  5. You may obtain a copy of the License at
  6. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package store
  14. import (
  15. "testing"
  16. )
  17. // TestEventQueue tests a queue with capacity = 100
  18. // Add 200 events into that queue, and test if the
  19. // previous 100 events have been swapped out.
  20. func TestEventQueue(t *testing.T) {
  21. eh := newEventHistory(100)
  22. // Add
  23. for i := 0; i < 200; i++ {
  24. e := newEvent(Create, "/foo", uint64(i), uint64(i))
  25. eh.addEvent(e)
  26. }
  27. // Test
  28. j := 100
  29. i := eh.Queue.Front
  30. n := eh.Queue.Size
  31. for ; n > 0; n-- {
  32. e := eh.Queue.Events[i]
  33. if e.Index() != uint64(j) {
  34. t.Fatalf("queue error!")
  35. }
  36. j++
  37. i = (i + 1) % eh.Queue.Capacity
  38. }
  39. }
  40. func TestScanHistory(t *testing.T) {
  41. eh := newEventHistory(100)
  42. // Add
  43. eh.addEvent(newEvent(Create, "/foo", 1, 1))
  44. eh.addEvent(newEvent(Create, "/foo/bar", 2, 2))
  45. eh.addEvent(newEvent(Create, "/foo/foo", 3, 3))
  46. eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4))
  47. eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5))
  48. e, err := eh.scan("/foo", false, 1)
  49. if err != nil || e.Index() != 1 {
  50. t.Fatalf("scan error [/foo] [1] %v", e.Index)
  51. }
  52. e, err = eh.scan("/foo/bar", false, 1)
  53. if err != nil || e.Index() != 2 {
  54. t.Fatalf("scan error [/foo/bar] [2] %v", e.Index)
  55. }
  56. e, err = eh.scan("/foo/bar", true, 3)
  57. if err != nil || e.Index() != 4 {
  58. t.Fatalf("scan error [/foo/bar/bar] [4] %v", e.Index)
  59. }
  60. e, err = eh.scan("/foo/bar", true, 6)
  61. if e != nil {
  62. t.Fatalf("bad index shoud reuturn nil")
  63. }
  64. }
  65. // TestFullEventQueue tests a queue with capacity = 10
  66. // Add 1000 events into that queue, and test if scanning
  67. // works still for previous events.
  68. func TestFullEventQueue(t *testing.T) {
  69. eh := newEventHistory(10)
  70. // Add
  71. for i := 0; i < 1000; i++ {
  72. e := newEvent(Create, "/foo", uint64(i), uint64(i))
  73. eh.addEvent(e)
  74. e, err := eh.scan("/foo", true, uint64(i-1))
  75. if i > 0 {
  76. if e == nil || err != nil {
  77. t.Fatalf("scan error [/foo] [%v] %v", i-1, i)
  78. }
  79. }
  80. }
  81. }
  82. func TestCloneEvent(t *testing.T) {
  83. e1 := &Event{
  84. Action: Create,
  85. EtcdIndex: 1,
  86. Node: nil,
  87. PrevNode: nil,
  88. }
  89. e2 := e1.Clone()
  90. if e2.Action != Create {
  91. t.Fatalf("Action=%q, want %q", e2.Action, Create)
  92. }
  93. if e2.EtcdIndex != e1.EtcdIndex {
  94. t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, e1.EtcdIndex)
  95. }
  96. // Changing the cloned node should not affect the original
  97. e2.Action = Delete
  98. e2.EtcdIndex = uint64(5)
  99. if e1.Action != Create {
  100. t.Fatalf("Action=%q, want %q", e1.Action, Create)
  101. }
  102. if e1.EtcdIndex != uint64(1) {
  103. t.Fatalf("EtcdIndex=%d, want %d", e1.EtcdIndex, uint64(1))
  104. }
  105. if e2.Action != Delete {
  106. t.Fatalf("Action=%q, want %q", e2.Action, Delete)
  107. }
  108. if e2.EtcdIndex != uint64(5) {
  109. t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, uint64(5))
  110. }
  111. }