backend_test.go 2.9 KB

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