batch_tx_test.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. "reflect"
  17. "testing"
  18. "time"
  19. bolt "github.com/coreos/bbolt"
  20. )
  21. func TestBatchTxPut(t *testing.T) {
  22. b, tmpPath := NewTmpBackend(time.Hour, 10000)
  23. defer cleanup(b, tmpPath)
  24. tx := b.batchTx
  25. tx.Lock()
  26. defer tx.Unlock()
  27. // create bucket
  28. tx.UnsafeCreateBucket([]byte("test"))
  29. // put
  30. v := []byte("bar")
  31. tx.UnsafePut([]byte("test"), []byte("foo"), v)
  32. // check put result before and after tx is committed
  33. for k := 0; k < 2; k++ {
  34. _, gv := tx.UnsafeRange([]byte("test"), []byte("foo"), nil, 0)
  35. if !reflect.DeepEqual(gv[0], v) {
  36. t.Errorf("v = %s, want %s", string(gv[0]), string(v))
  37. }
  38. tx.commit(false)
  39. }
  40. }
  41. func TestBatchTxRange(t *testing.T) {
  42. b, tmpPath := NewTmpBackend(time.Hour, 10000)
  43. defer cleanup(b, tmpPath)
  44. tx := b.batchTx
  45. tx.Lock()
  46. defer tx.Unlock()
  47. tx.UnsafeCreateBucket([]byte("test"))
  48. // put keys
  49. allKeys := [][]byte{[]byte("foo"), []byte("foo1"), []byte("foo2")}
  50. allVals := [][]byte{[]byte("bar"), []byte("bar1"), []byte("bar2")}
  51. for i := range allKeys {
  52. tx.UnsafePut([]byte("test"), allKeys[i], allVals[i])
  53. }
  54. tests := []struct {
  55. key []byte
  56. endKey []byte
  57. limit int64
  58. wkeys [][]byte
  59. wvals [][]byte
  60. }{
  61. // single key
  62. {
  63. []byte("foo"), nil, 0,
  64. allKeys[:1], allVals[:1],
  65. },
  66. // single key, bad
  67. {
  68. []byte("doo"), nil, 0,
  69. nil, nil,
  70. },
  71. // key range
  72. {
  73. []byte("foo"), []byte("foo1"), 0,
  74. allKeys[:1], allVals[:1],
  75. },
  76. // key range, get all keys
  77. {
  78. []byte("foo"), []byte("foo3"), 0,
  79. allKeys, allVals,
  80. },
  81. // key range, bad
  82. {
  83. []byte("goo"), []byte("goo3"), 0,
  84. nil, nil,
  85. },
  86. // key range with effective limit
  87. {
  88. []byte("foo"), []byte("foo3"), 1,
  89. allKeys[:1], allVals[:1],
  90. },
  91. // key range with limit
  92. {
  93. []byte("foo"), []byte("foo3"), 4,
  94. allKeys, allVals,
  95. },
  96. }
  97. for i, tt := range tests {
  98. keys, vals := tx.UnsafeRange([]byte("test"), tt.key, tt.endKey, tt.limit)
  99. if !reflect.DeepEqual(keys, tt.wkeys) {
  100. t.Errorf("#%d: keys = %+v, want %+v", i, keys, tt.wkeys)
  101. }
  102. if !reflect.DeepEqual(vals, tt.wvals) {
  103. t.Errorf("#%d: vals = %+v, want %+v", i, vals, tt.wvals)
  104. }
  105. }
  106. }
  107. func TestBatchTxDelete(t *testing.T) {
  108. b, tmpPath := NewTmpBackend(time.Hour, 10000)
  109. defer cleanup(b, tmpPath)
  110. tx := b.batchTx
  111. tx.Lock()
  112. defer tx.Unlock()
  113. tx.UnsafeCreateBucket([]byte("test"))
  114. tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar"))
  115. tx.UnsafeDelete([]byte("test"), []byte("foo"))
  116. // check put result before and after tx is committed
  117. for k := 0; k < 2; k++ {
  118. ks, _ := tx.UnsafeRange([]byte("test"), []byte("foo"), nil, 0)
  119. if len(ks) != 0 {
  120. t.Errorf("keys on foo = %v, want nil", ks)
  121. }
  122. tx.commit(false)
  123. }
  124. }
  125. func TestBatchTxCommit(t *testing.T) {
  126. b, tmpPath := NewTmpBackend(time.Hour, 10000)
  127. defer cleanup(b, tmpPath)
  128. tx := b.batchTx
  129. tx.Lock()
  130. tx.UnsafeCreateBucket([]byte("test"))
  131. tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar"))
  132. tx.Unlock()
  133. tx.Commit()
  134. // check whether put happens via db view
  135. b.db.View(func(tx *bolt.Tx) error {
  136. bucket := tx.Bucket([]byte("test"))
  137. if bucket == nil {
  138. t.Errorf("bucket test does not exit")
  139. return nil
  140. }
  141. v := bucket.Get([]byte("foo"))
  142. if v == nil {
  143. t.Errorf("foo key failed to written in backend")
  144. }
  145. return nil
  146. })
  147. }
  148. func TestBatchTxBatchLimitCommit(t *testing.T) {
  149. // start backend with batch limit 1 so one write can
  150. // trigger a commit
  151. b, tmpPath := NewTmpBackend(time.Hour, 1)
  152. defer cleanup(b, tmpPath)
  153. tx := b.batchTx
  154. tx.Lock()
  155. tx.UnsafeCreateBucket([]byte("test"))
  156. tx.UnsafePut([]byte("test"), []byte("foo"), []byte("bar"))
  157. tx.Unlock()
  158. // batch limit commit should have been triggered
  159. // check whether put happens via db view
  160. b.db.View(func(tx *bolt.Tx) error {
  161. bucket := tx.Bucket([]byte("test"))
  162. if bucket == nil {
  163. t.Errorf("bucket test does not exit")
  164. return nil
  165. }
  166. v := bucket.Get([]byte("foo"))
  167. if v == nil {
  168. t.Errorf("foo key failed to written in backend")
  169. }
  170. return nil
  171. })
  172. }