store_test.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. package store
  2. import (
  3. "math/rand"
  4. "strconv"
  5. "testing"
  6. "time"
  7. )
  8. func TestCreateAndGet(t *testing.T) {
  9. s := newStore()
  10. s.Create("/foobar", "bar", false, Permanent, 1, 1)
  11. // already exist, create should fail
  12. _, err := s.Create("/foobar", "bar", false, Permanent, 1, 1)
  13. if err == nil {
  14. t.Fatal("Create should fail")
  15. }
  16. s.Delete("/foobar", true, 1, 1)
  17. s.Create("/foobar/foo", "bar", false, Permanent, 1, 1)
  18. // already exist, create should fail
  19. _, err = s.Create("/foobar", "bar", false, Permanent, 1, 1)
  20. if err == nil {
  21. t.Fatal("Create should fail")
  22. }
  23. s.Delete("/foobar", true, 1, 1)
  24. // this should create successfully
  25. createAndGet(s, "/foobar", t)
  26. createAndGet(s, "/foo/bar", t)
  27. createAndGet(s, "/foo/foo/bar", t)
  28. // meet file, create should fail
  29. _, err = s.Create("/foo/bar/bar", "bar", false, Permanent, 2, 1)
  30. if err == nil {
  31. t.Fatal("Create should fail")
  32. }
  33. // create a directory
  34. _, err = s.Create("/fooDir", "", false, Permanent, 3, 1)
  35. if err != nil {
  36. t.Fatal("Cannot create /fooDir")
  37. }
  38. e, err := s.Get("/fooDir", false, false, 3, 1)
  39. if err != nil || e.Dir != true {
  40. t.Fatal("Cannot create /fooDir ")
  41. }
  42. // create a file under directory
  43. _, err = s.Create("/fooDir/bar", "bar", false, Permanent, 4, 1)
  44. if err != nil {
  45. t.Fatal("Cannot create /fooDir/bar = bar")
  46. }
  47. }
  48. func TestUpdateFile(t *testing.T) {
  49. s := newStore()
  50. _, err := s.Create("/foo/bar", "bar", false, Permanent, 1, 1)
  51. if err != nil {
  52. t.Fatalf("cannot create %s=bar [%s]", "/foo/bar", err.Error())
  53. }
  54. _, err = s.update("/foo/bar", "barbar", Permanent, 2, 1)
  55. if err != nil {
  56. t.Fatalf("cannot update %s=barbar [%s]", "/foo/bar", err.Error())
  57. }
  58. e, err := s.Get("/foo/bar", false, false, 2, 1)
  59. if err != nil {
  60. t.Fatalf("cannot get %s [%s]", "/foo/bar", err.Error())
  61. }
  62. if e.Value != "barbar" {
  63. t.Fatalf("expect value of %s is barbar [%s]", "/foo/bar", e.Value)
  64. }
  65. // create a directory, update its ttl, to see if it will be deleted
  66. _, err = s.Create("/foo/foo", "", false, Permanent, 3, 1)
  67. if err != nil {
  68. t.Fatalf("cannot create dir [%s] [%s]", "/foo/foo", err.Error())
  69. }
  70. _, err = s.Create("/foo/foo/foo1", "bar1", false, Permanent, 4, 1)
  71. if err != nil {
  72. t.Fatal("cannot create [%s]", err.Error())
  73. }
  74. _, err = s.Create("/foo/foo/foo2", "", false, Permanent, 5, 1)
  75. if err != nil {
  76. t.Fatal("cannot create [%s]", err.Error())
  77. }
  78. _, err = s.Create("/foo/foo/foo2/boo", "boo1", false, Permanent, 6, 1)
  79. if err != nil {
  80. t.Fatal("cannot create [%s]", err.Error())
  81. }
  82. expire := time.Now().Add(time.Second * 2)
  83. _, err = s.update("/foo/foo", "", expire, 7, 1)
  84. if err != nil {
  85. t.Fatalf("cannot update dir [%s] [%s]", "/foo/foo", err.Error())
  86. }
  87. // sleep 50ms, it should still reach the node
  88. time.Sleep(time.Microsecond * 50)
  89. e, err = s.Get("/foo/foo", true, false, 7, 1)
  90. if err != nil || e.Key != "/foo/foo" {
  91. t.Fatalf("cannot get dir before expiration [%s]", err.Error())
  92. }
  93. if e.KVPairs[0].Key != "/foo/foo/foo1" || e.KVPairs[0].Value != "bar1" {
  94. t.Fatalf("cannot get sub node before expiration [%s]", err.Error())
  95. }
  96. if e.KVPairs[1].Key != "/foo/foo/foo2" || e.KVPairs[1].Dir != true {
  97. t.Fatalf("cannot get sub dir before expiration [%s]", err.Error())
  98. }
  99. // wait for expiration
  100. time.Sleep(time.Second * 3)
  101. e, err = s.Get("/foo/foo", true, false, 7, 1)
  102. if err == nil {
  103. t.Fatal("still can get dir after expiration [%s]")
  104. }
  105. _, err = s.Get("/foo/foo/foo1", true, false, 7, 1)
  106. if err == nil {
  107. t.Fatal("still can get sub node after expiration [%s]")
  108. }
  109. _, err = s.Get("/foo/foo/foo2", true, false, 7, 1)
  110. if err == nil {
  111. t.Fatal("still can get sub dir after expiration [%s]")
  112. }
  113. _, err = s.Get("/foo/foo/foo2/boo", true, false, 7, 1)
  114. if err == nil {
  115. t.Fatalf("still can get sub node of sub dir after expiration [%s]", err.Error())
  116. }
  117. }
  118. func TestListDirectory(t *testing.T) {
  119. s := newStore()
  120. // create dir /foo
  121. // set key-value /foo/foo=bar
  122. s.Create("/foo/foo", "bar", false, Permanent, 1, 1)
  123. // create dir /foo/fooDir
  124. // set key-value /foo/fooDir/foo=bar
  125. s.Create("/foo/fooDir/foo", "bar", false, Permanent, 2, 1)
  126. e, err := s.Get("/foo", true, false, 2, 1)
  127. if err != nil {
  128. t.Fatalf("%v", err)
  129. }
  130. if len(e.KVPairs) != 2 {
  131. t.Fatalf("wrong number of kv pairs [%d/2]", len(e.KVPairs))
  132. }
  133. if e.KVPairs[0].Key != "/foo/foo" || e.KVPairs[0].Value != "bar" {
  134. t.Fatalf("wrong kv [/foo/foo/ / %s] -> [bar / %s]", e.KVPairs[0].Key, e.KVPairs[0].Value)
  135. }
  136. if e.KVPairs[1].Key != "/foo/fooDir" || e.KVPairs[1].Dir != true {
  137. t.Fatalf("wrong kv [/foo/fooDir/ / %s] -> [true / %v]", e.KVPairs[1].Key, e.KVPairs[1].Dir)
  138. }
  139. if e.KVPairs[1].KVPairs[0].Key != "/foo/fooDir/foo" || e.KVPairs[1].KVPairs[0].Value != "bar" {
  140. t.Fatalf("wrong kv [/foo/fooDir/foo / %s] -> [bar / %v]", e.KVPairs[1].KVPairs[0].Key, e.KVPairs[1].KVPairs[0].Value)
  141. }
  142. // test hidden node
  143. // create dir /foo/_hidden
  144. // set key-value /foo/_hidden/foo -> bar
  145. s.Create("/foo/_hidden/foo", "bar", false, Permanent, 3, 1)
  146. e, _ = s.Get("/foo", false, false, 2, 1)
  147. if len(e.KVPairs) != 2 {
  148. t.Fatalf("hidden node is not hidden! %s", e.KVPairs[2].Key)
  149. }
  150. }
  151. func TestRemove(t *testing.T) {
  152. s := newStore()
  153. s.Create("/foo", "bar", false, Permanent, 1, 1)
  154. _, err := s.Delete("/foo", false, 1, 1)
  155. if err != nil {
  156. t.Fatalf("cannot delete %s [%s]", "/foo", err.Error())
  157. }
  158. _, err = s.Get("/foo", false, false, 1, 1)
  159. if err == nil || err.Error() != "Key Not Found" {
  160. t.Fatalf("can get the node after deletion")
  161. }
  162. s.Create("/foo/bar", "bar", false, Permanent, 1, 1)
  163. s.Create("/foo/car", "car", false, Permanent, 1, 1)
  164. s.Create("/foo/dar/dar", "dar", false, Permanent, 1, 1)
  165. _, err = s.Delete("/foo", false, 1, 1)
  166. if err == nil {
  167. t.Fatalf("should not be able to delete a directory without recursive")
  168. }
  169. _, err = s.Delete("/foo", true, 1, 1)
  170. if err != nil {
  171. t.Fatalf("cannot delete %s [%s]", "/foo", err.Error())
  172. }
  173. _, err = s.Get("/foo", false, false, 1, 1)
  174. if err == nil || err.Error() != "Key Not Found" {
  175. t.Fatalf("can get the node after deletion ")
  176. }
  177. }
  178. func TestExpire(t *testing.T) {
  179. s := newStore()
  180. expire := time.Now().Add(time.Second)
  181. s.Create("/foo", "bar", false, expire, 1, 1)
  182. _, err := s.Get("/foo", false, false, 1, 1)
  183. if err != nil {
  184. t.Fatalf("can not get the node")
  185. }
  186. time.Sleep(time.Second * 2)
  187. _, err = s.Get("/foo", false, false, 1, 1)
  188. if err == nil {
  189. t.Fatalf("can get the node after expiration time")
  190. }
  191. // test if we can reach the node before expiration
  192. expire = time.Now().Add(time.Second)
  193. s.Create("/foo", "bar", false, expire, 1, 1)
  194. time.Sleep(time.Millisecond * 50)
  195. _, err = s.Get("/foo", false, false, 1, 1)
  196. if err != nil {
  197. t.Fatalf("cannot get the node before expiration", err.Error())
  198. }
  199. expire = time.Now().Add(time.Second)
  200. s.Create("/foo", "bar", false, expire, 1, 1)
  201. _, err = s.Delete("/foo", false, 1, 1)
  202. if err != nil {
  203. t.Fatalf("cannot delete the node before expiration", err.Error())
  204. }
  205. }
  206. func TestCompareAndSwap(t *testing.T) { // TODO prevValue == nil ?
  207. s := newStore()
  208. s.Create("/foo", "bar", false, Permanent, 1, 1)
  209. // test on wrong previous value
  210. _, err := s.CompareAndSwap("/foo", "barbar", 0, "car", Permanent, 2, 1)
  211. if err == nil {
  212. t.Fatal("test and set should fail barbar != bar")
  213. }
  214. // test on value
  215. e, err := s.CompareAndSwap("/foo", "bar", 0, "car", Permanent, 3, 1)
  216. if err != nil {
  217. t.Fatal("test and set should succeed bar == bar")
  218. }
  219. if e.PrevValue != "bar" || e.Value != "car" {
  220. t.Fatalf("[%v/%v] [%v/%v]", e.PrevValue, "bar", e.Value, "car")
  221. }
  222. // test on index
  223. e, err = s.CompareAndSwap("/foo", "", 3, "bar", Permanent, 4, 1)
  224. if err != nil {
  225. t.Fatal("test and set should succeed index 3 == 3")
  226. }
  227. if e.PrevValue != "car" || e.Value != "bar" {
  228. t.Fatalf("[%v/%v] [%v/%v]", e.PrevValue, "car", e.Value, "bar")
  229. }
  230. }
  231. func TestWatch(t *testing.T) {
  232. s := newStore()
  233. // watch at a deeper path
  234. c, _ := s.Watch("/foo/foo/foo", false, 0, 0, 1)
  235. s.Create("/foo/foo/foo", "bar", false, Permanent, 1, 1)
  236. e := nonblockingRetrive(c)
  237. if e.Key != "/foo/foo/foo" || e.Action != Create {
  238. t.Fatal("watch for Create node fails ", e)
  239. }
  240. c, _ = s.Watch("/foo/foo/foo", false, 0, 1, 1)
  241. s.update("/foo/foo/foo", "car", Permanent, 2, 1)
  242. e = nonblockingRetrive(c)
  243. if e.Key != "/foo/foo/foo" || e.Action != Update {
  244. t.Fatal("watch for Update node fails ", e)
  245. }
  246. c, _ = s.Watch("/foo/foo/foo", false, 0, 2, 1)
  247. s.CompareAndSwap("/foo/foo/foo", "car", 0, "bar", Permanent, 3, 1)
  248. e = nonblockingRetrive(c)
  249. if e.Key != "/foo/foo/foo" || e.Action != CompareAndSwap {
  250. t.Fatal("watch for CompareAndSwap node fails")
  251. }
  252. c, _ = s.Watch("/foo/foo/foo", false, 0, 3, 1)
  253. s.Delete("/foo", true, 4, 1) //recursively delete
  254. e = nonblockingRetrive(c)
  255. if e.Key != "/foo" || e.Action != Delete {
  256. t.Fatal("watch for Delete node fails ", e)
  257. }
  258. // watch at a prefix
  259. c, _ = s.Watch("/foo", true, 0, 4, 1)
  260. s.Create("/foo/foo/boo", "bar", false, Permanent, 5, 1)
  261. e = nonblockingRetrive(c)
  262. if e.Key != "/foo/foo/boo" || e.Action != Create {
  263. t.Fatal("watch for Create subdirectory fails")
  264. }
  265. c, _ = s.Watch("/foo", true, 0, 5, 1)
  266. s.update("/foo/foo/boo", "foo", Permanent, 6, 1)
  267. e = nonblockingRetrive(c)
  268. if e.Key != "/foo/foo/boo" || e.Action != Update {
  269. t.Fatal("watch for Update subdirectory fails")
  270. }
  271. c, _ = s.Watch("/foo", true, 0, 6, 1)
  272. s.CompareAndSwap("/foo/foo/boo", "foo", 0, "bar", Permanent, 7, 1)
  273. e = nonblockingRetrive(c)
  274. if e.Key != "/foo/foo/boo" || e.Action != CompareAndSwap {
  275. t.Fatal("watch for CompareAndSwap subdirectory fails")
  276. }
  277. c, _ = s.Watch("/foo", true, 0, 7, 1)
  278. s.Delete("/foo/foo/boo", false, 8, 1)
  279. e = nonblockingRetrive(c)
  280. if e == nil || e.Key != "/foo/foo/boo" || e.Action != Delete {
  281. t.Fatal("watch for Delete subdirectory fails")
  282. }
  283. // watch expire
  284. s.Create("/foo/foo/boo", "foo", false, time.Now().Add(time.Second*1), 9, 1)
  285. c, _ = s.Watch("/foo", true, 0, 9, 1)
  286. time.Sleep(time.Second * 2)
  287. e = nonblockingRetrive(c)
  288. if e.Key != "/foo/foo/boo" || e.Action != Expire || e.Index != 9 {
  289. t.Fatal("watch for Expiration of Create() subdirectory fails ", e)
  290. }
  291. s.Create("/foo/foo/boo", "foo", false, Permanent, 10, 1)
  292. s.update("/foo/foo/boo", "bar", time.Now().Add(time.Second*1), 11, 1)
  293. c, _ = s.Watch("/foo", true, 0, 11, 1)
  294. time.Sleep(time.Second * 2)
  295. e = nonblockingRetrive(c)
  296. if e.Key != "/foo/foo/boo" || e.Action != Expire || e.Index != 11 {
  297. t.Fatal("watch for Expiration of Update() subdirectory fails ", e)
  298. }
  299. s.Create("/foo/foo/boo", "foo", false, Permanent, 12, 1)
  300. s.CompareAndSwap("/foo/foo/boo", "foo", 0, "bar", time.Now().Add(time.Second*1), 13, 1)
  301. c, _ = s.Watch("/foo", true, 0, 13, 1)
  302. time.Sleep(time.Second * 2)
  303. e = nonblockingRetrive(c)
  304. if e.Key != "/foo/foo/boo" || e.Action != Expire || e.Index != 13 {
  305. t.Fatal("watch for Expiration of CompareAndSwap() subdirectory fails ", e)
  306. }
  307. }
  308. func TestSort(t *testing.T) {
  309. s := newStore()
  310. // simulating random creation
  311. keys := GenKeys(80, 4)
  312. i := uint64(1)
  313. for _, k := range keys {
  314. _, err := s.Create(k, "bar", false, Permanent, i, 1)
  315. if err != nil {
  316. panic(err)
  317. } else {
  318. i++
  319. }
  320. }
  321. e, err := s.Get("/foo", true, true, i, 1)
  322. if err != nil {
  323. t.Fatalf("get dir nodes failed [%s]", err.Error())
  324. }
  325. for i, k := range e.KVPairs[:len(e.KVPairs)-1] {
  326. if k.Key >= e.KVPairs[i+1].Key {
  327. t.Fatalf("sort failed, [%s] should be placed after [%s]", k.Key, e.KVPairs[i+1].Key)
  328. }
  329. if k.Dir {
  330. recursiveTestSort(k, t)
  331. }
  332. }
  333. if k := e.KVPairs[len(e.KVPairs)-1]; k.Dir {
  334. recursiveTestSort(k, t)
  335. }
  336. }
  337. func TestSaveAndRecover(t *testing.T) {
  338. s := newStore()
  339. // simulating random creation
  340. keys := GenKeys(8, 4)
  341. i := uint64(1)
  342. for _, k := range keys {
  343. _, err := s.Create(k, "bar", false, Permanent, i, 1)
  344. if err != nil {
  345. panic(err)
  346. } else {
  347. i++
  348. }
  349. }
  350. // create a node with expiration
  351. // test if we can reach the node before expiration
  352. expire := time.Now().Add(time.Second)
  353. s.Create("/foo/foo", "bar", false, expire, 1, 1)
  354. b, err := s.Save()
  355. cloneFs := newStore()
  356. time.Sleep(2 * time.Second)
  357. cloneFs.Recovery(b)
  358. for i, k := range keys {
  359. _, err := cloneFs.Get(k, false, false, uint64(i), 1)
  360. if err != nil {
  361. panic(err)
  362. }
  363. }
  364. // lock to avoid racing with Expire()
  365. s.worldLock.RLock()
  366. defer s.worldLock.RUnlock()
  367. if s.WatcherHub.EventHistory.StartIndex != cloneFs.WatcherHub.EventHistory.StartIndex {
  368. t.Fatalf("Error recovered event history start index[%v/%v]",
  369. s.WatcherHub.EventHistory.StartIndex, cloneFs.WatcherHub.EventHistory.StartIndex)
  370. }
  371. for i = 0; int(i) < cloneFs.WatcherHub.EventHistory.Queue.Size; i++ {
  372. if s.WatcherHub.EventHistory.Queue.Events[i].Key !=
  373. cloneFs.WatcherHub.EventHistory.Queue.Events[i].Key {
  374. t.Fatal("Error recovered event history")
  375. }
  376. }
  377. _, err = s.Get("/foo/foo", false, false, 1, 1)
  378. if err == nil || err.Error() != "Key Not Found" {
  379. t.Fatalf("can get the node after deletion ")
  380. }
  381. }
  382. // GenKeys randomly generate num of keys with max depth
  383. func GenKeys(num int, depth int) []string {
  384. rand.Seed(time.Now().UnixNano())
  385. keys := make([]string, num)
  386. for i := 0; i < num; i++ {
  387. keys[i] = "/foo"
  388. depth := rand.Intn(depth) + 1
  389. for j := 0; j < depth; j++ {
  390. keys[i] += "/" + strconv.Itoa(rand.Int())
  391. }
  392. }
  393. return keys
  394. }
  395. func createAndGet(s *store, path string, t *testing.T) {
  396. _, err := s.Create(path, "bar", false, Permanent, 1, 1)
  397. if err != nil {
  398. t.Fatalf("cannot create %s=bar [%s]", path, err.Error())
  399. }
  400. e, err := s.Get(path, false, false, 1, 1)
  401. if err != nil {
  402. t.Fatalf("cannot get %s [%s]", path, err.Error())
  403. }
  404. if e.Value != "bar" {
  405. t.Fatalf("expect value of %s is bar [%s]", path, e.Value)
  406. }
  407. }
  408. func recursiveTestSort(k KeyValuePair, t *testing.T) {
  409. for i, v := range k.KVPairs[:len(k.KVPairs)-1] {
  410. if v.Key >= k.KVPairs[i+1].Key {
  411. t.Fatalf("sort failed, [%s] should be placed after [%s]", v.Key, k.KVPairs[i+1].Key)
  412. }
  413. if v.Dir {
  414. recursiveTestSort(v, t)
  415. }
  416. }
  417. if v := k.KVPairs[len(k.KVPairs)-1]; v.Dir {
  418. recursiveTestSort(v, t)
  419. }
  420. }
  421. func nonblockingRetrive(c <-chan *Event) *Event {
  422. select {
  423. case e := <-c:
  424. return e
  425. default:
  426. return nil
  427. }
  428. }