kvstore_compaction_test.go 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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. 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, b, tmpPath)
  85. }
  86. }