backend_test.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. // Copyright 2015 The etcd Authors
  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. "fmt"
  17. "io/ioutil"
  18. "os"
  19. "testing"
  20. "time"
  21. "github.com/boltdb/bolt"
  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(10 * time.Second):
  38. t.Errorf("failed to close database in 10s")
  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. pc := b.Commits()
  78. tx := b.BatchTx()
  79. tx.Lock()
  80. tx.UnsafeCreateBucket([]byte("test"))
  81. tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar"))
  82. tx.Unlock()
  83. for i := 0; i < 10; i++ {
  84. if b.Commits() >= pc+1 {
  85. break
  86. }
  87. time.Sleep(time.Duration(i*100) * time.Millisecond)
  88. }
  89. // check whether put happens via db view
  90. b.db.View(func(tx *bolt.Tx) error {
  91. bucket := tx.Bucket([]byte("test"))
  92. if bucket == nil {
  93. t.Errorf("bucket test does not exit")
  94. return nil
  95. }
  96. v := bucket.Get([]byte("foo"))
  97. if v == nil {
  98. t.Errorf("foo key failed to written in backend")
  99. }
  100. return nil
  101. })
  102. }
  103. func TestBackendDefrag(t *testing.T) {
  104. b, tmpPath := NewDefaultTmpBackend()
  105. defer cleanup(b, tmpPath)
  106. tx := b.BatchTx()
  107. tx.Lock()
  108. tx.UnsafeCreateBucket([]byte("test"))
  109. for i := 0; i < defragLimit+100; i++ {
  110. tx.UnsafePut([]byte("test"), []byte(fmt.Sprintf("foo_%d", i)), []byte("bar"))
  111. }
  112. tx.Unlock()
  113. b.ForceCommit()
  114. // remove some keys to ensure the disk space will be reclaimed after defrag
  115. tx = b.BatchTx()
  116. tx.Lock()
  117. for i := 0; i < 50; i++ {
  118. tx.UnsafeDelete([]byte("test"), []byte(fmt.Sprintf("foo_%d", i)))
  119. }
  120. tx.Unlock()
  121. b.ForceCommit()
  122. size := b.Size()
  123. // shrink and check hash
  124. oh, err := b.Hash(nil)
  125. if err != nil {
  126. t.Fatal(err)
  127. }
  128. err = b.Defrag()
  129. if err != nil {
  130. t.Fatal(err)
  131. }
  132. nh, err := b.Hash(nil)
  133. if err != nil {
  134. t.Fatal(err)
  135. }
  136. if oh != nh {
  137. t.Errorf("hash = %v, want %v", nh, oh)
  138. }
  139. nsize := b.Size()
  140. if nsize >= size {
  141. t.Errorf("new size = %v, want < %d", nsize, size)
  142. }
  143. // try put more keys after shrink.
  144. tx = b.BatchTx()
  145. tx.Lock()
  146. tx.UnsafeCreateBucket([]byte("test"))
  147. tx.UnsafePut([]byte("test"), []byte("more"), []byte("bar"))
  148. tx.Unlock()
  149. b.ForceCommit()
  150. }
  151. func cleanup(b Backend, path string) {
  152. b.Close()
  153. os.Remove(path)
  154. }