event_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 (
  16. "testing"
  17. )
  18. // TestEventQueue tests a queue with capacity = 100
  19. // Add 200 events into that queue, and test if the
  20. // previous 100 events have been swapped out.
  21. func TestEventQueue(t *testing.T) {
  22. eh := newEventHistory(100)
  23. // Add
  24. for i := 0; i < 200; i++ {
  25. e := newEvent(Create, "/foo", uint64(i), uint64(i))
  26. eh.addEvent(e)
  27. }
  28. // Test
  29. j := 100
  30. i := eh.Queue.Front
  31. n := eh.Queue.Size
  32. for ; n > 0; n-- {
  33. e := eh.Queue.Events[i]
  34. if e.Index() != uint64(j) {
  35. t.Fatalf("queue error!")
  36. }
  37. j++
  38. i = (i + 1) % eh.Queue.Capacity
  39. }
  40. }
  41. func TestScanHistory(t *testing.T) {
  42. eh := newEventHistory(100)
  43. // Add
  44. eh.addEvent(newEvent(Create, "/foo", 1, 1))
  45. eh.addEvent(newEvent(Create, "/foo/bar", 2, 2))
  46. eh.addEvent(newEvent(Create, "/foo/foo", 3, 3))
  47. eh.addEvent(newEvent(Create, "/foo/bar/bar", 4, 4))
  48. eh.addEvent(newEvent(Create, "/foo/foo/foo", 5, 5))
  49. e, err := eh.scan("/foo", false, 1)
  50. if err != nil || e.Index() != 1 {
  51. t.Fatalf("scan error [/foo] [1] %v", e.Index)
  52. }
  53. e, err = eh.scan("/foo/bar", false, 1)
  54. if err != nil || e.Index() != 2 {
  55. t.Fatalf("scan error [/foo/bar] [2] %v", e.Index)
  56. }
  57. e, err = eh.scan("/foo/bar", true, 3)
  58. if err != nil || e.Index() != 4 {
  59. t.Fatalf("scan error [/foo/bar/bar] [4] %v", e.Index)
  60. }
  61. e, err = eh.scan("/foo/bar", true, 6)
  62. if e != nil {
  63. t.Fatalf("bad index shoud reuturn nil")
  64. }
  65. }
  66. // TestFullEventQueue tests a queue with capacity = 10
  67. // Add 1000 events into that queue, and test if scanning
  68. // works still for previous events.
  69. func TestFullEventQueue(t *testing.T) {
  70. eh := newEventHistory(10)
  71. // Add
  72. for i := 0; i < 1000; i++ {
  73. ce := newEvent(Create, "/foo", uint64(i), uint64(i))
  74. eh.addEvent(ce)
  75. e, err := eh.scan("/foo", true, uint64(i-1))
  76. if i > 0 {
  77. if e == nil || err != nil {
  78. t.Fatalf("scan error [/foo] [%v] %v", i-1, i)
  79. }
  80. }
  81. }
  82. }
  83. func TestCloneEvent(t *testing.T) {
  84. e1 := &Event{
  85. Action: Create,
  86. EtcdIndex: 1,
  87. Node: nil,
  88. PrevNode: nil,
  89. }
  90. e2 := e1.Clone()
  91. if e2.Action != Create {
  92. t.Fatalf("Action=%q, want %q", e2.Action, Create)
  93. }
  94. if e2.EtcdIndex != e1.EtcdIndex {
  95. t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, e1.EtcdIndex)
  96. }
  97. // Changing the cloned node should not affect the original
  98. e2.Action = Delete
  99. e2.EtcdIndex = uint64(5)
  100. if e1.Action != Create {
  101. t.Fatalf("Action=%q, want %q", e1.Action, Create)
  102. }
  103. if e1.EtcdIndex != uint64(1) {
  104. t.Fatalf("EtcdIndex=%d, want %d", e1.EtcdIndex, uint64(1))
  105. }
  106. if e2.Action != Delete {
  107. t.Fatalf("Action=%q, want %q", e2.Action, Delete)
  108. }
  109. if e2.EtcdIndex != uint64(5) {
  110. t.Fatalf("EtcdIndex=%d, want %d", e2.EtcdIndex, uint64(5))
  111. }
  112. }