store_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638
  1. /*
  2. Copyright 2013 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. "time"
  17. etcdErr "github.com/coreos/etcd/error"
  18. "github.com/stretchr/testify/assert"
  19. )
  20. // Ensure that the store can retrieve an existing value.
  21. func TestStoreGetValue(t *testing.T) {
  22. s := newStore()
  23. s.Create("/foo", false, "bar", false, Permanent)
  24. e, err := s.Get("/foo", false, false)
  25. assert.Nil(t, err, "")
  26. assert.Equal(t, e.Action, "get", "")
  27. assert.Equal(t, e.Node.Key, "/foo", "")
  28. assert.Equal(t, e.Node.Value, "bar", "")
  29. }
  30. // Ensure that the store can recrusively retrieve a directory listing.
  31. // Note that hidden files should not be returned.
  32. func TestStoreGetDirectory(t *testing.T) {
  33. s := newStore()
  34. s.Create("/foo", true, "", false, Permanent)
  35. s.Create("/foo/bar", false, "X", false, Permanent)
  36. s.Create("/foo/_hidden", false, "*", false, Permanent)
  37. s.Create("/foo/baz", true, "", false, Permanent)
  38. s.Create("/foo/baz/bat", false, "Y", false, Permanent)
  39. s.Create("/foo/baz/_hidden", false, "*", false, Permanent)
  40. s.Create("/foo/baz/ttl", false, "Y", false, time.Now().Add(time.Second*3))
  41. e, err := s.Get("/foo", true, false)
  42. assert.Nil(t, err, "")
  43. assert.Equal(t, e.Action, "get", "")
  44. assert.Equal(t, e.Node.Key, "/foo", "")
  45. assert.Equal(t, len(e.Node.Nodes), 2, "")
  46. assert.Equal(t, e.Node.Nodes[0].Key, "/foo/bar", "")
  47. assert.Equal(t, e.Node.Nodes[0].Value, "X", "")
  48. assert.Equal(t, e.Node.Nodes[0].Dir, false, "")
  49. assert.Equal(t, e.Node.Nodes[1].Key, "/foo/baz", "")
  50. assert.Equal(t, e.Node.Nodes[1].Dir, true, "")
  51. assert.Equal(t, len(e.Node.Nodes[1].Nodes), 2, "")
  52. assert.Equal(t, e.Node.Nodes[1].Nodes[0].Key, "/foo/baz/bat", "")
  53. assert.Equal(t, e.Node.Nodes[1].Nodes[0].Value, "Y", "")
  54. assert.Equal(t, e.Node.Nodes[1].Nodes[0].Dir, false, "")
  55. assert.Equal(t, e.Node.Nodes[1].Nodes[1].Key, "/foo/baz/ttl", "")
  56. assert.Equal(t, e.Node.Nodes[1].Nodes[1].Value, "Y", "")
  57. assert.Equal(t, e.Node.Nodes[1].Nodes[1].Dir, false, "")
  58. assert.Equal(t, e.Node.Nodes[1].Nodes[1].TTL, 3, "")
  59. }
  60. // Ensure that the store can retrieve a directory in sorted order.
  61. func TestStoreGetSorted(t *testing.T) {
  62. s := newStore()
  63. s.Create("/foo", true, "", false, Permanent)
  64. s.Create("/foo/x", false, "0", false, Permanent)
  65. s.Create("/foo/z", false, "0", false, Permanent)
  66. s.Create("/foo/y", true, "", false, Permanent)
  67. s.Create("/foo/y/a", false, "0", false, Permanent)
  68. s.Create("/foo/y/b", false, "0", false, Permanent)
  69. e, err := s.Get("/foo", true, true)
  70. assert.Nil(t, err, "")
  71. assert.Equal(t, e.Node.Nodes[0].Key, "/foo/x", "")
  72. assert.Equal(t, e.Node.Nodes[1].Key, "/foo/y", "")
  73. assert.Equal(t, e.Node.Nodes[1].Nodes[0].Key, "/foo/y/a", "")
  74. assert.Equal(t, e.Node.Nodes[1].Nodes[1].Key, "/foo/y/b", "")
  75. assert.Equal(t, e.Node.Nodes[2].Key, "/foo/z", "")
  76. }
  77. func TestSet(t *testing.T) {
  78. s := newStore()
  79. // Set /foo=""
  80. e, err := s.Set("/foo", false, "", Permanent)
  81. assert.Nil(t, err, "")
  82. assert.Equal(t, e.Action, "set", "")
  83. assert.Equal(t, e.Node.Key, "/foo", "")
  84. assert.False(t, e.Node.Dir, "")
  85. assert.Equal(t, e.Node.PrevValue, "", "")
  86. assert.Equal(t, e.Node.Value, "", "")
  87. assert.Nil(t, e.Node.Nodes, "")
  88. assert.Nil(t, e.Node.Expiration, "")
  89. assert.Equal(t, e.Node.TTL, 0, "")
  90. assert.Equal(t, e.Node.ModifiedIndex, uint64(1), "")
  91. // Set /foo="bar"
  92. e, err = s.Set("/foo", false, "bar", Permanent)
  93. assert.Nil(t, err, "")
  94. assert.Equal(t, e.Action, "set", "")
  95. assert.Equal(t, e.Node.Key, "/foo", "")
  96. assert.False(t, e.Node.Dir, "")
  97. assert.Equal(t, e.Node.PrevValue, "", "")
  98. assert.Equal(t, e.Node.Value, "bar", "")
  99. assert.Nil(t, e.Node.Nodes, "")
  100. assert.Nil(t, e.Node.Expiration, "")
  101. assert.Equal(t, e.Node.TTL, 0, "")
  102. assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
  103. // Set /dir as a directory
  104. e, err = s.Set("/dir", true, "", Permanent)
  105. assert.Nil(t, err, "")
  106. assert.Equal(t, e.Action, "set", "")
  107. assert.Equal(t, e.Node.Key, "/dir", "")
  108. assert.True(t, e.Node.Dir, "")
  109. assert.Equal(t, e.Node.PrevValue, "", "")
  110. assert.Equal(t, e.Node.Value, "", "")
  111. assert.Nil(t, e.Node.Nodes, "")
  112. assert.Nil(t, e.Node.Expiration, "")
  113. assert.Equal(t, e.Node.TTL, 0, "")
  114. assert.Equal(t, e.Node.ModifiedIndex, uint64(3), "")
  115. }
  116. // Ensure that the store can create a new key if it doesn't already exist.
  117. func TestStoreCreateValue(t *testing.T) {
  118. s := newStore()
  119. // Create /foo=bar
  120. e, err := s.Create("/foo", false, "bar", false, Permanent)
  121. assert.Nil(t, err, "")
  122. assert.Equal(t, e.Action, "create", "")
  123. assert.Equal(t, e.Node.Key, "/foo", "")
  124. assert.False(t, e.Node.Dir, "")
  125. assert.Equal(t, e.Node.PrevValue, "", "")
  126. assert.Equal(t, e.Node.Value, "bar", "")
  127. assert.Nil(t, e.Node.Nodes, "")
  128. assert.Nil(t, e.Node.Expiration, "")
  129. assert.Equal(t, e.Node.TTL, 0, "")
  130. assert.Equal(t, e.Node.ModifiedIndex, uint64(1), "")
  131. // Create /empty=""
  132. e, err = s.Create("/empty", false, "", false, Permanent)
  133. assert.Nil(t, err, "")
  134. assert.Equal(t, e.Action, "create", "")
  135. assert.Equal(t, e.Node.Key, "/empty", "")
  136. assert.False(t, e.Node.Dir, "")
  137. assert.Equal(t, e.Node.PrevValue, "", "")
  138. assert.Equal(t, e.Node.Value, "", "")
  139. assert.Nil(t, e.Node.Nodes, "")
  140. assert.Nil(t, e.Node.Expiration, "")
  141. assert.Equal(t, e.Node.TTL, 0, "")
  142. assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
  143. }
  144. // Ensure that the store can create a new directory if it doesn't already exist.
  145. func TestStoreCreateDirectory(t *testing.T) {
  146. s := newStore()
  147. e, err := s.Create("/foo", true, "", false, Permanent)
  148. assert.Nil(t, err, "")
  149. assert.Equal(t, e.Action, "create", "")
  150. assert.Equal(t, e.Node.Key, "/foo", "")
  151. assert.True(t, e.Node.Dir, "")
  152. }
  153. // Ensure that the store fails to create a key if it already exists.
  154. func TestStoreCreateFailsIfExists(t *testing.T) {
  155. s := newStore()
  156. // create /foo as dir
  157. s.Create("/foo", true, "", false, Permanent)
  158. // create /foo as dir again
  159. e, _err := s.Create("/foo", true, "", false, Permanent)
  160. err := _err.(*etcdErr.Error)
  161. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNodeExist, "")
  162. assert.Equal(t, err.Message, "Key already exists", "")
  163. assert.Equal(t, err.Cause, "/foo", "")
  164. assert.Equal(t, err.Index, uint64(1), "")
  165. assert.Nil(t, e, 0, "")
  166. }
  167. // Ensure that the store can update a key if it already exists.
  168. func TestStoreUpdateValue(t *testing.T) {
  169. s := newStore()
  170. // create /foo=bar
  171. s.Create("/foo", false, "bar", false, Permanent)
  172. // update /foo="bzr"
  173. e, err := s.Update("/foo", "baz", Permanent)
  174. assert.Nil(t, err, "")
  175. assert.Equal(t, e.Action, "update", "")
  176. assert.Equal(t, e.Node.Key, "/foo", "")
  177. assert.False(t, e.Node.Dir, "")
  178. assert.Equal(t, e.Node.PrevValue, "bar", "")
  179. assert.Equal(t, e.Node.Value, "baz", "")
  180. assert.Equal(t, e.Node.TTL, 0, "")
  181. assert.Equal(t, e.Node.ModifiedIndex, uint64(2), "")
  182. e, _ = s.Get("/foo", false, false)
  183. assert.Equal(t, e.Node.Value, "baz", "")
  184. // update /foo=""
  185. e, err = s.Update("/foo", "", Permanent)
  186. assert.Nil(t, err, "")
  187. assert.Equal(t, e.Action, "update", "")
  188. assert.Equal(t, e.Node.Key, "/foo", "")
  189. assert.False(t, e.Node.Dir, "")
  190. assert.Equal(t, e.Node.PrevValue, "baz", "")
  191. assert.Equal(t, e.Node.Value, "", "")
  192. assert.Equal(t, e.Node.TTL, 0, "")
  193. assert.Equal(t, e.Node.ModifiedIndex, uint64(3), "")
  194. e, _ = s.Get("/foo", false, false)
  195. assert.Equal(t, e.Node.Value, "", "")
  196. }
  197. // Ensure that the store cannot update a directory.
  198. func TestStoreUpdateFailsIfDirectory(t *testing.T) {
  199. s := newStore()
  200. s.Create("/foo", true, "", false, Permanent)
  201. e, _err := s.Update("/foo", "baz", Permanent)
  202. err := _err.(*etcdErr.Error)
  203. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  204. assert.Equal(t, err.Message, "Not a file", "")
  205. assert.Equal(t, err.Cause, "/foo", "")
  206. assert.Nil(t, e, "")
  207. }
  208. // Ensure that the store can update the TTL on a value.
  209. func TestStoreUpdateValueTTL(t *testing.T) {
  210. s := newStore()
  211. c := make(chan bool)
  212. defer func() {
  213. c <- true
  214. }()
  215. go mockSyncService(s.DeleteExpiredKeys, c)
  216. s.Create("/foo", false, "bar", false, Permanent)
  217. _, err := s.Update("/foo", "baz", time.Now().Add(500*time.Millisecond))
  218. e, _ := s.Get("/foo", false, false)
  219. assert.Equal(t, e.Node.Value, "baz", "")
  220. time.Sleep(600 * time.Millisecond)
  221. e, err = s.Get("/foo", false, false)
  222. assert.Nil(t, e, "")
  223. assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
  224. }
  225. // Ensure that the store can update the TTL on a directory.
  226. func TestStoreUpdateDirTTL(t *testing.T) {
  227. s := newStore()
  228. c := make(chan bool)
  229. defer func() {
  230. c <- true
  231. }()
  232. go mockSyncService(s.DeleteExpiredKeys, c)
  233. s.Create("/foo", true, "", false, Permanent)
  234. s.Create("/foo/bar", false, "baz", false, Permanent)
  235. _, err := s.Update("/foo", "", time.Now().Add(500*time.Millisecond))
  236. e, _ := s.Get("/foo/bar", false, false)
  237. assert.Equal(t, e.Node.Value, "baz", "")
  238. time.Sleep(600 * time.Millisecond)
  239. e, err = s.Get("/foo/bar", false, false)
  240. assert.Nil(t, e, "")
  241. assert.Equal(t, err.(*etcdErr.Error).ErrorCode, etcdErr.EcodeKeyNotFound, "")
  242. }
  243. // Ensure that the store can delete a value.
  244. func TestStoreDeleteValue(t *testing.T) {
  245. s := newStore()
  246. s.Create("/foo", false, "bar", false, Permanent)
  247. e, err := s.Delete("/foo", false, false)
  248. assert.Nil(t, err, "")
  249. assert.Equal(t, e.Action, "delete", "")
  250. }
  251. // Ensure that the store can delete a directory if recursive is specified.
  252. func TestStoreDeleteDiretory(t *testing.T) {
  253. s := newStore()
  254. // create directory /foo
  255. s.Create("/foo", true, "", false, Permanent)
  256. // delete /foo with dir = true and recursive = false
  257. // this should succeed, since the directory is empty
  258. e, err := s.Delete("/foo", true, false)
  259. assert.Nil(t, err, "")
  260. assert.Equal(t, e.Action, "delete", "")
  261. // create directory /foo and directory /foo/bar
  262. s.Create("/foo/bar", true, "", false, Permanent)
  263. // delete /foo with dir = true and recursive = false
  264. // this should fail, since the directory is not empty
  265. _, err = s.Delete("/foo", true, false)
  266. assert.NotNil(t, err, "")
  267. // delete /foo with dir=false and recursive = true
  268. // this should succeed, since recursive implies dir=true
  269. // and recursively delete should be able to delete all
  270. // items under the given directory
  271. e, err = s.Delete("/foo", false, true)
  272. assert.Nil(t, err, "")
  273. assert.Equal(t, e.Action, "delete", "")
  274. }
  275. // Ensure that the store cannot delete a directory if both of recursive
  276. // and dir are not specified.
  277. func TestStoreDeleteDiretoryFailsIfNonRecursiveAndDir(t *testing.T) {
  278. s := newStore()
  279. s.Create("/foo", true, "", false, Permanent)
  280. e, _err := s.Delete("/foo", false, false)
  281. err := _err.(*etcdErr.Error)
  282. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  283. assert.Equal(t, err.Message, "Not a file", "")
  284. assert.Nil(t, e, "")
  285. }
  286. func TestRootRdOnly(t *testing.T) {
  287. s := newStore()
  288. _, err := s.Set("/", true, "", Permanent)
  289. assert.NotNil(t, err, "")
  290. _, err = s.Delete("/", true, true)
  291. assert.NotNil(t, err, "")
  292. _, err = s.Create("/", true, "", false, Permanent)
  293. assert.NotNil(t, err, "")
  294. _, err = s.Update("/", "", Permanent)
  295. assert.NotNil(t, err, "")
  296. _, err = s.CompareAndSwap("/", "", 0, "", Permanent)
  297. assert.NotNil(t, err, "")
  298. }
  299. func TestStoreCompareAndDeletePrevValue(t *testing.T) {
  300. s := newStore()
  301. s.Create("/foo", false, "bar", false, Permanent)
  302. e, err := s.CompareAndDelete("/foo", "bar", 0)
  303. assert.Nil(t, err, "")
  304. assert.Equal(t, e.Action, "compareAndDelete", "")
  305. assert.Equal(t, e.Node.Key, "/foo", "")
  306. }
  307. func TestStoreCompareAndDeletePrevValueFailsIfNotMatch(t *testing.T) {
  308. s := newStore()
  309. s.Create("/foo", false, "bar", false, Permanent)
  310. e, _err := s.CompareAndDelete("/foo", "baz", 0)
  311. err := _err.(*etcdErr.Error)
  312. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  313. assert.Equal(t, err.Message, "Compare failed", "")
  314. assert.Nil(t, e, "")
  315. e, _ = s.Get("/foo", false, false)
  316. assert.Equal(t, e.Node.Value, "bar", "")
  317. }
  318. func TestStoreCompareAndDeletePrevIndex(t *testing.T) {
  319. s := newStore()
  320. s.Create("/foo", false, "bar", false, Permanent)
  321. e, err := s.CompareAndDelete("/foo", "", 1)
  322. assert.Nil(t, err, "")
  323. assert.Equal(t, e.Action, "compareAndDelete", "")
  324. }
  325. func TestStoreCompareAndDeletePrevIndexFailsIfNotMatch(t *testing.T) {
  326. s := newStore()
  327. s.Create("/foo", false, "bar", false, Permanent)
  328. e, _err := s.CompareAndDelete("/foo", "", 100)
  329. assert.NotNil(t, _err, "")
  330. err := _err.(*etcdErr.Error)
  331. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  332. assert.Equal(t, err.Message, "Compare failed", "")
  333. assert.Nil(t, e, "")
  334. e, _ = s.Get("/foo", false, false)
  335. assert.Equal(t, e.Node.Value, "bar", "")
  336. }
  337. // Ensure that the store cannot delete a directory.
  338. func TestStoreCompareAndDeleteDiretoryFail(t *testing.T) {
  339. s := newStore()
  340. s.Create("/foo", true, "", false, Permanent)
  341. _, _err := s.CompareAndDelete("/foo", "", 0)
  342. assert.NotNil(t, _err, "")
  343. err := _err.(*etcdErr.Error)
  344. assert.Equal(t, err.ErrorCode, etcdErr.EcodeNotFile, "")
  345. }
  346. // Ensure that the store can conditionally update a key if it has a previous value.
  347. func TestStoreCompareAndSwapPrevValue(t *testing.T) {
  348. s := newStore()
  349. s.Create("/foo", false, "bar", false, Permanent)
  350. e, err := s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
  351. assert.Nil(t, err, "")
  352. assert.Equal(t, e.Action, "compareAndSwap", "")
  353. assert.Equal(t, e.Node.PrevValue, "bar", "")
  354. assert.Equal(t, e.Node.Value, "baz", "")
  355. e, _ = s.Get("/foo", false, false)
  356. assert.Equal(t, e.Node.Value, "baz", "")
  357. }
  358. // Ensure that the store cannot conditionally update a key if it has the wrong previous value.
  359. func TestStoreCompareAndSwapPrevValueFailsIfNotMatch(t *testing.T) {
  360. s := newStore()
  361. s.Create("/foo", false, "bar", false, Permanent)
  362. e, _err := s.CompareAndSwap("/foo", "wrong_value", 0, "baz", Permanent)
  363. err := _err.(*etcdErr.Error)
  364. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  365. assert.Equal(t, err.Message, "Compare failed", "")
  366. assert.Nil(t, e, "")
  367. e, _ = s.Get("/foo", false, false)
  368. assert.Equal(t, e.Node.Value, "bar", "")
  369. }
  370. // Ensure that the store can conditionally update a key if it has a previous index.
  371. func TestStoreCompareAndSwapPrevIndex(t *testing.T) {
  372. s := newStore()
  373. s.Create("/foo", false, "bar", false, Permanent)
  374. e, err := s.CompareAndSwap("/foo", "", 1, "baz", Permanent)
  375. assert.Nil(t, err, "")
  376. assert.Equal(t, e.Action, "compareAndSwap", "")
  377. assert.Equal(t, e.Node.PrevValue, "bar", "")
  378. assert.Equal(t, e.Node.Value, "baz", "")
  379. e, _ = s.Get("/foo", false, false)
  380. assert.Equal(t, e.Node.Value, "baz", "")
  381. }
  382. // Ensure that the store cannot conditionally update a key if it has the wrong previous index.
  383. func TestStoreCompareAndSwapPrevIndexFailsIfNotMatch(t *testing.T) {
  384. s := newStore()
  385. s.Create("/foo", false, "bar", false, Permanent)
  386. e, _err := s.CompareAndSwap("/foo", "", 100, "baz", Permanent)
  387. err := _err.(*etcdErr.Error)
  388. assert.Equal(t, err.ErrorCode, etcdErr.EcodeTestFailed, "")
  389. assert.Equal(t, err.Message, "Compare failed", "")
  390. assert.Nil(t, e, "")
  391. e, _ = s.Get("/foo", false, false)
  392. assert.Equal(t, e.Node.Value, "bar", "")
  393. }
  394. // Ensure that the store can watch for key creation.
  395. func TestStoreWatchCreate(t *testing.T) {
  396. s := newStore()
  397. c, _ := s.Watch("/foo", false, 0)
  398. s.Create("/foo", false, "bar", false, Permanent)
  399. e := nbselect(c)
  400. assert.Equal(t, e.Action, "create", "")
  401. assert.Equal(t, e.Node.Key, "/foo", "")
  402. e = nbselect(c)
  403. assert.Nil(t, e, "")
  404. }
  405. // Ensure that the store can watch for recursive key creation.
  406. func TestStoreWatchRecursiveCreate(t *testing.T) {
  407. s := newStore()
  408. c, _ := s.Watch("/foo", true, 0)
  409. s.Create("/foo/bar", false, "baz", false, Permanent)
  410. e := nbselect(c)
  411. assert.Equal(t, e.Action, "create", "")
  412. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  413. }
  414. // Ensure that the store can watch for key updates.
  415. func TestStoreWatchUpdate(t *testing.T) {
  416. s := newStore()
  417. s.Create("/foo", false, "bar", false, Permanent)
  418. c, _ := s.Watch("/foo", false, 0)
  419. s.Update("/foo", "baz", Permanent)
  420. e := nbselect(c)
  421. assert.Equal(t, e.Action, "update", "")
  422. assert.Equal(t, e.Node.Key, "/foo", "")
  423. }
  424. // Ensure that the store can watch for recursive key updates.
  425. func TestStoreWatchRecursiveUpdate(t *testing.T) {
  426. s := newStore()
  427. s.Create("/foo/bar", false, "baz", false, Permanent)
  428. c, _ := s.Watch("/foo", true, 0)
  429. s.Update("/foo/bar", "baz", Permanent)
  430. e := nbselect(c)
  431. assert.Equal(t, e.Action, "update", "")
  432. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  433. }
  434. // Ensure that the store can watch for key deletions.
  435. func TestStoreWatchDelete(t *testing.T) {
  436. s := newStore()
  437. s.Create("/foo", false, "bar", false, Permanent)
  438. c, _ := s.Watch("/foo", false, 0)
  439. s.Delete("/foo", false, false)
  440. e := nbselect(c)
  441. assert.Equal(t, e.Action, "delete", "")
  442. assert.Equal(t, e.Node.Key, "/foo", "")
  443. }
  444. // Ensure that the store can watch for recursive key deletions.
  445. func TestStoreWatchRecursiveDelete(t *testing.T) {
  446. s := newStore()
  447. s.Create("/foo/bar", false, "baz", false, Permanent)
  448. c, _ := s.Watch("/foo", true, 0)
  449. s.Delete("/foo/bar", false, false)
  450. e := nbselect(c)
  451. assert.Equal(t, e.Action, "delete", "")
  452. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  453. }
  454. // Ensure that the store can watch for CAS updates.
  455. func TestStoreWatchCompareAndSwap(t *testing.T) {
  456. s := newStore()
  457. s.Create("/foo", false, "bar", false, Permanent)
  458. c, _ := s.Watch("/foo", false, 0)
  459. s.CompareAndSwap("/foo", "bar", 0, "baz", Permanent)
  460. e := nbselect(c)
  461. assert.Equal(t, e.Action, "compareAndSwap", "")
  462. assert.Equal(t, e.Node.Key, "/foo", "")
  463. }
  464. // Ensure that the store can watch for recursive CAS updates.
  465. func TestStoreWatchRecursiveCompareAndSwap(t *testing.T) {
  466. s := newStore()
  467. s.Create("/foo/bar", false, "baz", false, Permanent)
  468. c, _ := s.Watch("/foo", true, 0)
  469. s.CompareAndSwap("/foo/bar", "baz", 0, "bat", Permanent)
  470. e := nbselect(c)
  471. assert.Equal(t, e.Action, "compareAndSwap", "")
  472. assert.Equal(t, e.Node.Key, "/foo/bar", "")
  473. }
  474. // Ensure that the store can watch for key expiration.
  475. func TestStoreWatchExpire(t *testing.T) {
  476. s := newStore()
  477. stopChan := make(chan bool)
  478. defer func() {
  479. stopChan <- true
  480. }()
  481. go mockSyncService(s.DeleteExpiredKeys, stopChan)
  482. s.Create("/foo", false, "bar", false, time.Now().Add(500*time.Millisecond))
  483. s.Create("/foofoo", false, "barbarbar", false, time.Now().Add(500*time.Millisecond))
  484. c, _ := s.Watch("/", true, 0)
  485. e := nbselect(c)
  486. assert.Nil(t, e, "")
  487. time.Sleep(600 * time.Millisecond)
  488. e = nbselect(c)
  489. assert.Equal(t, e.Action, "expire", "")
  490. assert.Equal(t, e.Node.Key, "/foo", "")
  491. c, _ = s.Watch("/", true, 4)
  492. e = nbselect(c)
  493. assert.Equal(t, e.Action, "expire", "")
  494. assert.Equal(t, e.Node.Key, "/foofoo", "")
  495. }
  496. // Ensure that the store can recover from a previously saved state.
  497. func TestStoreRecover(t *testing.T) {
  498. s := newStore()
  499. s.Create("/foo", true, "", false, Permanent)
  500. s.Create("/foo/x", false, "bar", false, Permanent)
  501. s.Create("/foo/y", false, "baz", false, Permanent)
  502. b, err := s.Save()
  503. s2 := newStore()
  504. s2.Recovery(b)
  505. e, err := s.Get("/foo/x", false, false)
  506. assert.Nil(t, err, "")
  507. assert.Equal(t, e.Node.Value, "bar", "")
  508. e, err = s.Get("/foo/y", false, false)
  509. assert.Nil(t, err, "")
  510. assert.Equal(t, e.Node.Value, "baz", "")
  511. }
  512. // Ensure that the store can recover from a previously saved state that includes an expiring key.
  513. func TestStoreRecoverWithExpiration(t *testing.T) {
  514. s := newStore()
  515. c := make(chan bool)
  516. defer func() {
  517. c <- true
  518. }()
  519. go mockSyncService(s.DeleteExpiredKeys, c)
  520. s.Create("/foo", true, "", false, Permanent)
  521. s.Create("/foo/x", false, "bar", false, Permanent)
  522. s.Create("/foo/y", false, "baz", false, time.Now().Add(5*time.Millisecond))
  523. b, err := s.Save()
  524. time.Sleep(10 * time.Millisecond)
  525. s2 := newStore()
  526. c2 := make(chan bool)
  527. defer func() {
  528. c2 <- true
  529. }()
  530. go mockSyncService(s2.DeleteExpiredKeys, c2)
  531. s2.Recovery(b)
  532. time.Sleep(600 * time.Millisecond)
  533. e, err := s.Get("/foo/x", false, false)
  534. assert.Nil(t, err, "")
  535. assert.Equal(t, e.Node.Value, "bar", "")
  536. e, err = s.Get("/foo/y", false, false)
  537. assert.NotNil(t, err, "")
  538. assert.Nil(t, e, "")
  539. }
  540. // Performs a non-blocking select on an event channel.
  541. func nbselect(c <-chan *Event) *Event {
  542. select {
  543. case e := <-c:
  544. return e
  545. default:
  546. return nil
  547. }
  548. }
  549. func mockSyncService(f func(now time.Time), c chan bool) {
  550. ticker := time.Tick(time.Millisecond * 500)
  551. for {
  552. select {
  553. case <-c:
  554. return
  555. case now := <-ticker:
  556. f(now)
  557. }
  558. }
  559. }