store_test.go 14 KB

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