event_test.go 3.5 KB

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