kvstore_compaction_test.go 2.3 KB

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