compactor_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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 compactor
  15. import (
  16. "reflect"
  17. "testing"
  18. "time"
  19. pb "github.com/coreos/etcd/etcdserver/etcdserverpb"
  20. "github.com/coreos/etcd/pkg/testutil"
  21. "github.com/jonboulle/clockwork"
  22. "golang.org/x/net/context"
  23. )
  24. func TestPeriodic(t *testing.T) {
  25. retentionHours := 2
  26. fc := clockwork.NewFakeClock()
  27. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  28. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  29. tb := &Periodic{
  30. clock: fc,
  31. periodInHour: retentionHours,
  32. rg: rg,
  33. c: compactable,
  34. }
  35. tb.Run()
  36. defer tb.Stop()
  37. n := int(time.Hour / checkCompactionInterval)
  38. // collect 5 hours of revisions
  39. for i := 0; i < 5; i++ {
  40. // advance one hour, one revision for each interval
  41. for j := 0; j < n; j++ {
  42. rg.Wait(1)
  43. fc.Advance(checkCompactionInterval)
  44. }
  45. // compaction doesn't happen til 2 hours elapses
  46. if i+1 < retentionHours {
  47. continue
  48. }
  49. a, err := compactable.Wait(1)
  50. if err != nil {
  51. t.Fatal(err)
  52. }
  53. expectedRevision := int64(1 + (i+1)*n - retentionHours*n)
  54. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  55. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  56. }
  57. }
  58. // unblock the rev getter, so we can stop the compactor routine.
  59. _, err := rg.Wait(1)
  60. if err != nil {
  61. t.Fatal(err)
  62. }
  63. }
  64. func TestPeriodicPause(t *testing.T) {
  65. fc := clockwork.NewFakeClock()
  66. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  67. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  68. tb := &Periodic{
  69. clock: fc,
  70. periodInHour: 1,
  71. rg: rg,
  72. c: compactable,
  73. }
  74. tb.Run()
  75. tb.Pause()
  76. // tb will collect 3 hours of revisions but not compact since paused
  77. n := int(time.Hour / checkCompactionInterval)
  78. for i := 0; i < 3*n; i++ {
  79. rg.Wait(1)
  80. fc.Advance(checkCompactionInterval)
  81. }
  82. // tb ends up waiting for the clock
  83. select {
  84. case a := <-compactable.Chan():
  85. t.Fatalf("unexpected action %v", a)
  86. case <-time.After(10 * time.Millisecond):
  87. }
  88. // tb resumes to being blocked on the clock
  89. tb.Resume()
  90. // unblock clock, will kick off a compaction at hour 3:05
  91. rg.Wait(1)
  92. fc.Advance(checkCompactionInterval)
  93. a, err := compactable.Wait(1)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. // compact the revision from hour 2:05
  98. wreq := &pb.CompactionRequest{Revision: int64(1 + 2*n + 1)}
  99. if !reflect.DeepEqual(a[0].Params[0], wreq) {
  100. t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
  101. }
  102. }
  103. type fakeCompactable struct {
  104. testutil.Recorder
  105. }
  106. func (fc *fakeCompactable) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
  107. fc.Record(testutil.Action{Name: "c", Params: []interface{}{r}})
  108. return &pb.CompactionResponse{}, nil
  109. }
  110. type fakeRevGetter struct {
  111. testutil.Recorder
  112. rev int64
  113. }
  114. func (fr *fakeRevGetter) Rev() int64 {
  115. fr.Record(testutil.Action{Name: "g"})
  116. fr.rev++
  117. return fr.rev
  118. }