backend_test.go 3.0 KB

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