backend_test.go 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. package backend
  2. import (
  3. "io/ioutil"
  4. "log"
  5. "os"
  6. "path"
  7. "testing"
  8. "time"
  9. "github.com/coreos/etcd/Godeps/_workspace/src/github.com/boltdb/bolt"
  10. "github.com/coreos/etcd/pkg/testutil"
  11. )
  12. var tmpPath string
  13. func init() {
  14. dir, err := ioutil.TempDir(os.TempDir(), "etcd_backend_test")
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. tmpPath = path.Join(dir, "database")
  19. }
  20. func TestBackendClose(t *testing.T) {
  21. b := newBackend(tmpPath, time.Hour, 10000)
  22. defer os.Remove(tmpPath)
  23. // check close could work
  24. done := make(chan struct{})
  25. go func() {
  26. err := b.Close()
  27. if err != nil {
  28. t.Errorf("close error = %v, want nil", err)
  29. }
  30. done <- struct{}{}
  31. }()
  32. select {
  33. case <-done:
  34. case <-time.After(time.Second):
  35. t.Errorf("failed to close database in 1s")
  36. }
  37. }
  38. func TestBackendSnapshot(t *testing.T) {
  39. b := New(tmpPath, time.Hour, 10000)
  40. defer cleanup(b, tmpPath)
  41. tx := b.BatchTx()
  42. tx.Lock()
  43. tx.UnsafeCreateBucket([]byte("test"))
  44. tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar"))
  45. tx.Unlock()
  46. b.ForceCommit()
  47. // write snapshot to a new file
  48. f, err := ioutil.TempFile(os.TempDir(), "etcd_backend_test")
  49. if err != nil {
  50. t.Fatal(err)
  51. }
  52. _, err = b.Snapshot(f)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. f.Close()
  57. // bootstrap new backend from the snapshot
  58. nb := New(f.Name(), time.Hour, 10000)
  59. defer cleanup(nb, f.Name())
  60. newTx := b.BatchTx()
  61. newTx.Lock()
  62. ks, _ := newTx.UnsafeRange([]byte("test"), []byte("foo"), []byte("goo"), 0)
  63. if len(ks) != 1 {
  64. t.Errorf("len(kvs) = %d, want 1", len(ks))
  65. }
  66. newTx.Unlock()
  67. }
  68. func TestBackendBatchIntervalCommit(t *testing.T) {
  69. // start backend with super short batch interval
  70. b := newBackend(tmpPath, time.Nanosecond, 10000)
  71. defer cleanup(b, tmpPath)
  72. tx := b.BatchTx()
  73. tx.Lock()
  74. tx.UnsafeCreateBucket([]byte("test"))
  75. tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar"))
  76. tx.Unlock()
  77. // give time for batch interval commit to happen
  78. time.Sleep(time.Nanosecond)
  79. testutil.WaitSchedule()
  80. // check whether put happens via db view
  81. b.db.View(func(tx *bolt.Tx) error {
  82. bucket := tx.Bucket([]byte("test"))
  83. if bucket == nil {
  84. t.Errorf("bucket test does not exit")
  85. return nil
  86. }
  87. v := bucket.Get([]byte("foo"))
  88. if v == nil {
  89. t.Errorf("foo key failed to written in backend")
  90. }
  91. return nil
  92. })
  93. }
  94. func cleanup(b Backend, path string) {
  95. b.Close()
  96. os.Remove(path)
  97. }