node_test.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  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. "time"
  18. "github.com/jonboulle/clockwork"
  19. )
  20. var (
  21. key, val = "foo", "bar"
  22. val1, val2 = "bar1", "bar2"
  23. expiration = time.Minute
  24. )
  25. func TestNewKVIs(t *testing.T) {
  26. nd := newTestNode()
  27. if nd.IsHidden() {
  28. t.Errorf("nd.Hidden() = %v, want = false", nd.IsHidden())
  29. }
  30. if nd.IsPermanent() {
  31. t.Errorf("nd.IsPermanent() = %v, want = false", nd.IsPermanent())
  32. }
  33. if nd.IsDir() {
  34. t.Errorf("nd.IsDir() = %v, want = false", nd.IsDir())
  35. }
  36. }
  37. func TestNewKVReadWriteCompare(t *testing.T) {
  38. nd := newTestNode()
  39. if v, err := nd.Read(); v != val || err != nil {
  40. t.Errorf("value = %s and err = %v, want value = %s and err = nil", v, err, val)
  41. }
  42. if err := nd.Write(val1, nd.CreatedIndex+1); err != nil {
  43. t.Errorf("nd.Write error = %v, want = nil", err)
  44. } else {
  45. if v, err := nd.Read(); v != val1 || err != nil {
  46. t.Errorf("value = %s and err = %v, want value = %s and err = nil", v, err, val1)
  47. }
  48. }
  49. if err := nd.Write(val2, nd.CreatedIndex+2); err != nil {
  50. t.Errorf("nd.Write error = %v, want = nil", err)
  51. } else {
  52. if v, err := nd.Read(); v != val2 || err != nil {
  53. t.Errorf("value = %s and err = %v, want value = %s and err = nil", v, err, val2)
  54. }
  55. }
  56. if ok, which := nd.Compare(val2, 2); !ok || which != 0 {
  57. t.Errorf("ok = %v and which = %d, want ok = true and which = 0", ok, which)
  58. }
  59. }
  60. func TestNewKVExpiration(t *testing.T) {
  61. nd := newTestNode()
  62. if _, ttl := nd.expirationAndTTL(clockwork.NewFakeClock()); ttl > expiration.Nanoseconds() {
  63. t.Errorf("ttl = %d, want %d < %d", ttl, ttl, expiration.Nanoseconds())
  64. }
  65. newExpiration := time.Hour
  66. nd.UpdateTTL(time.Now().Add(newExpiration))
  67. if _, ttl := nd.expirationAndTTL(clockwork.NewFakeClock()); ttl > newExpiration.Nanoseconds() {
  68. t.Errorf("ttl = %d, want %d < %d", ttl, ttl, newExpiration.Nanoseconds())
  69. }
  70. if ns, err := nd.List(); ns != nil || err == nil {
  71. t.Errorf("nodes = %v and err = %v, want nodes = nil and err != nil", ns, err)
  72. }
  73. en := nd.Repr(false, false, clockwork.NewFakeClock())
  74. if en.Key != nd.Path {
  75. t.Errorf("en.Key = %s, want = %s", en.Key, nd.Path)
  76. }
  77. if *(en.Value) != nd.Value {
  78. t.Errorf("*(en.Key) = %s, want = %s", *(en.Value), nd.Value)
  79. }
  80. }
  81. func TestNewKVListReprCompareClone(t *testing.T) {
  82. nd := newTestNode()
  83. if ns, err := nd.List(); ns != nil || err == nil {
  84. t.Errorf("nodes = %v and err = %v, want nodes = nil and err != nil", ns, err)
  85. }
  86. en := nd.Repr(false, false, clockwork.NewFakeClock())
  87. if en.Key != nd.Path {
  88. t.Errorf("en.Key = %s, want = %s", en.Key, nd.Path)
  89. }
  90. if *(en.Value) != nd.Value {
  91. t.Errorf("*(en.Key) = %s, want = %s", *(en.Value), nd.Value)
  92. }
  93. cn := nd.Clone()
  94. if cn.Path != nd.Path {
  95. t.Errorf("cn.Path = %s, want = %s", cn.Path, nd.Path)
  96. }
  97. if cn.Value != nd.Value {
  98. t.Errorf("cn.Value = %s, want = %s", cn.Value, nd.Value)
  99. }
  100. }
  101. func TestNewKVRemove(t *testing.T) {
  102. nd := newTestNode()
  103. if v, err := nd.Read(); v != val || err != nil {
  104. t.Errorf("value = %s and err = %v, want value = %s and err = nil", v, err, val)
  105. }
  106. if err := nd.Write(val1, nd.CreatedIndex+1); err != nil {
  107. t.Errorf("nd.Write error = %v, want = nil", err)
  108. } else {
  109. if v, err := nd.Read(); v != val1 || err != nil {
  110. t.Errorf("value = %s and err = %v, want value = %s and err = nil", v, err, val1)
  111. }
  112. }
  113. if err := nd.Write(val2, nd.CreatedIndex+2); err != nil {
  114. t.Errorf("nd.Write error = %v, want = nil", err)
  115. } else {
  116. if v, err := nd.Read(); v != val2 || err != nil {
  117. t.Errorf("value = %s and err = %v, want value = %s and err = nil", v, err, val2)
  118. }
  119. }
  120. if err := nd.Remove(false, false, nil); err != nil {
  121. t.Errorf("nd.Remove err = %v, want = nil", err)
  122. } else {
  123. // still readable
  124. if v, err := nd.Read(); v != val2 || err != nil {
  125. t.Errorf("value = %s and err = %v, want value = %s and err = nil", v, err, val2)
  126. }
  127. if len(nd.store.ttlKeyHeap.array) != 0 {
  128. t.Errorf("len(nd.store.ttlKeyHeap.array) = %d, want = 0", len(nd.store.ttlKeyHeap.array))
  129. }
  130. if len(nd.store.ttlKeyHeap.keyMap) != 0 {
  131. t.Errorf("len(nd.store.ttlKeyHeap.keyMap) = %d, want = 0", len(nd.store.ttlKeyHeap.keyMap))
  132. }
  133. }
  134. }
  135. func TestNewDirIs(t *testing.T) {
  136. nd, _ := newTestNodeDir()
  137. if nd.IsHidden() {
  138. t.Errorf("nd.Hidden() = %v, want = false", nd.IsHidden())
  139. }
  140. if nd.IsPermanent() {
  141. t.Errorf("nd.IsPermanent() = %v, want = false", nd.IsPermanent())
  142. }
  143. if !nd.IsDir() {
  144. t.Errorf("nd.IsDir() = %v, want = true", nd.IsDir())
  145. }
  146. }
  147. func TestNewDirReadWriteListReprClone(t *testing.T) {
  148. nd, _ := newTestNodeDir()
  149. if _, err := nd.Read(); err == nil {
  150. t.Errorf("err = %v, want err != nil", err)
  151. }
  152. if err := nd.Write(val, nd.CreatedIndex+1); err == nil {
  153. t.Errorf("err = %v, want err != nil", err)
  154. }
  155. if ns, err := nd.List(); ns == nil && err != nil {
  156. t.Errorf("nodes = %v and err = %v, want nodes = nil and err == nil", ns, err)
  157. }
  158. en := nd.Repr(false, false, clockwork.NewFakeClock())
  159. if en.Key != nd.Path {
  160. t.Errorf("en.Key = %s, want = %s", en.Key, nd.Path)
  161. }
  162. cn := nd.Clone()
  163. if cn.Path != nd.Path {
  164. t.Errorf("cn.Path = %s, want = %s", cn.Path, nd.Path)
  165. }
  166. }
  167. func TestNewDirExpirationTTL(t *testing.T) {
  168. nd, _ := newTestNodeDir()
  169. if _, ttl := nd.expirationAndTTL(clockwork.NewFakeClock()); ttl > expiration.Nanoseconds() {
  170. t.Errorf("ttl = %d, want %d < %d", ttl, ttl, expiration.Nanoseconds())
  171. }
  172. newExpiration := time.Hour
  173. nd.UpdateTTL(time.Now().Add(newExpiration))
  174. if _, ttl := nd.expirationAndTTL(clockwork.NewFakeClock()); ttl > newExpiration.Nanoseconds() {
  175. t.Errorf("ttl = %d, want %d < %d", ttl, ttl, newExpiration.Nanoseconds())
  176. }
  177. }
  178. func TestNewDirChild(t *testing.T) {
  179. nd, child := newTestNodeDir()
  180. if err := nd.Add(child); err != nil {
  181. t.Errorf("nd.Add(child) err = %v, want = nil", err)
  182. } else {
  183. if len(nd.Children) == 0 {
  184. t.Errorf("len(nd.Children) = %d, want = 1", len(nd.Children))
  185. }
  186. }
  187. if err := child.Remove(true, true, nil); err != nil {
  188. t.Errorf("child.Remove err = %v, want = nil", err)
  189. } else {
  190. if len(nd.Children) != 0 {
  191. t.Errorf("len(nd.Children) = %d, want = 0", len(nd.Children))
  192. }
  193. }
  194. }
  195. func newTestNode() *node {
  196. nd := newKV(newStore(), key, val, 0, nil, time.Now().Add(expiration))
  197. return nd
  198. }
  199. func newTestNodeDir() (*node, *node) {
  200. s := newStore()
  201. nd := newDir(s, key, 0, nil, time.Now().Add(expiration))
  202. cKey, cVal := "hello", "world"
  203. child := newKV(s, cKey, cVal, 0, nd, time.Now().Add(expiration))
  204. return nd, child
  205. }