kvstore_compaction_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 storage
  15. import (
  16. "reflect"
  17. "testing"
  18. "github.com/coreos/etcd/lease"
  19. "github.com/coreos/etcd/storage/backend"
  20. )
  21. func TestScheduleCompaction(t *testing.T) {
  22. revs := []revision{{1, 0}, {2, 0}, {3, 0}}
  23. tests := []struct {
  24. rev int64
  25. keep map[revision]struct{}
  26. wrevs []revision
  27. }{
  28. // compact at 1 and discard all history
  29. {
  30. 1,
  31. nil,
  32. revs[1:],
  33. },
  34. // compact at 3 and discard all history
  35. {
  36. 3,
  37. nil,
  38. nil,
  39. },
  40. // compact at 1 and keeps history one step earlier
  41. {
  42. 1,
  43. map[revision]struct{}{
  44. revision{main: 1}: {},
  45. },
  46. revs,
  47. },
  48. // compact at 1 and keeps history two steps earlier
  49. {
  50. 3,
  51. map[revision]struct{}{
  52. revision{main: 2}: {},
  53. revision{main: 3}: {},
  54. },
  55. revs[1:],
  56. },
  57. }
  58. for i, tt := range tests {
  59. b, tmpPath := backend.NewDefaultTmpBackend()
  60. s := NewStore(b, &lease.FakeLessor{})
  61. tx := s.b.BatchTx()
  62. tx.Lock()
  63. ibytes := newRevBytes()
  64. for _, rev := range revs {
  65. revToBytes(rev, ibytes)
  66. tx.UnsafePut(keyBucketName, ibytes, []byte("bar"))
  67. }
  68. tx.Unlock()
  69. // call `s.wg.Add(1)` to match the `s.wg.Done()` call in scheduleCompaction
  70. // to avoid panic from wait group
  71. s.wg.Add(1)
  72. s.scheduleCompaction(tt.rev, tt.keep)
  73. tx.Lock()
  74. for _, rev := range tt.wrevs {
  75. revToBytes(rev, ibytes)
  76. keys, _ := tx.UnsafeRange(keyBucketName, ibytes, nil, 0)
  77. if len(keys) != 1 {
  78. t.Errorf("#%d: range on %v = %d, want 1", i, rev, len(keys))
  79. }
  80. }
  81. _, vals := tx.UnsafeRange(metaBucketName, finishedCompactKeyName, nil, 0)
  82. revToBytes(revision{main: tt.rev}, ibytes)
  83. if w := [][]byte{ibytes}; !reflect.DeepEqual(vals, w) {
  84. t.Errorf("#%d: vals on %v = %+v, want %+v", i, finishedCompactKeyName, vals, w)
  85. }
  86. tx.Unlock()
  87. cleanup(s, b, tmpPath)
  88. }
  89. }