event_test.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // Copyright 2015 The etcd Authors
  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 (
  16. "testing"
  17. etcdErr "github.com/coreos/etcd/error"
  18. )
  19. // TestEventQueue tests a queue with capacity = 100
  20. // Add 200 events into that queue, and test if the
  21. // previous 100 events have been swapped out.
  22. func TestEventQueue(t *testing.T) {
  23. eh := newEventHistory(100)
  24. // Add
  25. for i := 0; i < 200; i++ {
  26. e := newEvent(Create, "/foo", uint64(i), uint64(i))
  27. eh.addEvent(e)
  28. }
  29. // Test
  30. j := 100
  31. i := eh.Queue.Front
  32. n := eh.Queue.Size
  33. for ; n > 0; n-- {
  34. e := eh.Queue.Events[i]
  35. if e.Index() != uint64(j) {
  36. t.Fatalf("queue error!")
  37. }
  38. j++
  39. i = (i + 1) % eh.Queue.Capacity
  40. }
  41. }
  42. func TestScanHistory(t *testing.T) {
  43. eh := newEventHistory(100)
  44. // Add
  45. eh.addEvent(newEvent(Create, "/foo", 1, 1))
  46. eh.addEvent(newEvent(Create, "/foo/bar", 2, 2))
  47. eh.addEvent(newEvent(Create, "/foo/foo", 3, 3))
  48. eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4))
  49. eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5))
  50. // Delete a dir
  51. de := newEvent(Delete, "/foo", 6, 6)
  52. de.PrevNode = newDir(nil, "/foo", 1, nil, Permanent).Repr(false, false, nil)
  53. eh.addEvent(de)
  54. e, err := eh.scan("/foo", false, 1)
  55. if err != nil || e.Index() != 1 {
  56. t.Fatalf("scan error [/foo] [1] %d (%v)", e.Index(), err)
  57. }
  58. e, err = eh.scan("/foo/bar", false, 1)
  59. if err != nil || e.Index() != 2 {
  60. t.Fatalf("scan error [/foo/bar] [2] %d (%v)", e.Index(), err)
  61. }
  62. e, err = eh.scan("/foo/bar", true, 3)
  63. if err != nil || e.Index() != 4 {
  64. t.Fatalf("scan error [/foo/bar/bar] [4] %d (%v)", e.Index(), err)
  65. }
  66. e, err = eh.scan("/foo/foo/foo", false, 6)
  67. if err != nil || e.Index() != 6 {
  68. t.Fatalf("scan error [/foo/foo/foo] [6] %d (%v)", e.Index(), err)
  69. }
  70. e, _ = eh.scan("/foo/bar", true, 7)
  71. if e != nil {
  72. t.Fatalf("bad index shoud reuturn nil")
  73. }
  74. }
  75. func TestEventIndexHistoryCleared(t *testing.T) {
  76. eh := newEventHistory(5)
  77. // Add
  78. eh.addEvent(newEvent(Create, "/foo", 1, 1))
  79. eh.addEvent(newEvent(Create, "/foo/bar", 2, 2))
  80. eh.addEvent(newEvent(Create, "/foo/foo", 3, 3))
  81. eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4))
  82. eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5))
  83. // Add a new event which will replace/de-queue the first entry
  84. eh.addEvent(newEvent(Create, "/foo/bar/bar/bar", 6, 6))
  85. // test for the event which has been replaced.
  86. _, err := eh.scan("/foo", false, 1)
  87. if err == nil || err.ErrorCode != etcdErr.EcodeEventIndexCleared {
  88. t.Fatalf("scan error cleared index should return err with %d got (%v)", etcdErr.EcodeEventIndexCleared, err)
  89. }
  90. }
  91. // TestFullEventQueue tests a queue with capacity = 10
  92. // Add 1000 events into that queue, and test if scanning
  93. // works still for previous events.
  94. func TestFullEventQueue(t *testing.T) {
  95. eh := newEventHistory(10)
  96. // Add
  97. for i := 0; i < 1000; i++ {
  98. ce := newEvent(Create, "/foo", uint64(i), uint64(i))
  99. eh.addEvent(ce)
  100. e, err := eh.scan("/foo", true, uint64(i-1))
  101. if i > 0 {
  102. if e == nil || err != nil {
  103. t.Fatalf("scan error [/foo] [%v] %v", i-1, i)
  104. }
  105. }
  106. }
  107. }
  108. func TestCloneEvent(t *testing.T) {
  109. e1 := &Event{
  110. Action: Create,
  111. EtcdIndex: 1,
  112. Node: nil,
  113. PrevNode: nil,
  114. }
  115. e2 := e1.Clone()
  116. if e2.Action != Create {
  117. t.Fatalf("Action=%q, want %q", e2.Action, Create)
  118. }
  119. if e2.EtcdIndex != e1.EtcdIndex {
  120. t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, e1.EtcdIndex)
  121. }
  122. // Changing the cloned node should not affect the original
  123. e2.Action = Delete
  124. e2.EtcdIndex = uint64(5)
  125. if e1.Action != Create {
  126. t.Fatalf("Action=%q, want %q", e1.Action, Create)
  127. }
  128. if e1.EtcdIndex != uint64(1) {
  129. t.Fatalf("EtcdIndex=%d, want %d", e1.EtcdIndex, uint64(1))
  130. }
  131. if e2.Action != Delete {
  132. t.Fatalf("Action=%q, want %q", e2.Action, Delete)
  133. }
  134. if e2.EtcdIndex != uint64(5) {
  135. t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, uint64(5))
  136. }
  137. }