backend_test.go 2.8 KB

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