kvstore_compaction_test.go 2.2 KB

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