periodic_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. )
  23. func TestPeriodic(t *testing.T) {
  24. retentionHours := 2
  25. retentionDuration := time.Duration(retentionHours) * time.Hour
  26. fc := clockwork.NewFakeClock()
  27. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  28. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  29. tb := newPeriodic(fc, retentionDuration, rg, compactable)
  30. tb.Run()
  31. defer tb.Stop()
  32. checkCompactInterval := retentionDuration / time.Duration(periodDivisor)
  33. n := periodDivisor
  34. // simulate 5 hours worth of intervals.
  35. for i := 0; i < n/retentionHours*5; i++ {
  36. rg.Wait(1)
  37. fc.Advance(checkCompactInterval)
  38. // compaction doesn't happen til 2 hours elapses.
  39. if i < n {
  40. continue
  41. }
  42. // after 2 hours, compaction happens at every checkCompactInterval.
  43. a, err := compactable.Wait(1)
  44. if err != nil {
  45. t.Fatal(err)
  46. }
  47. expectedRevision := int64(i + 1 - n)
  48. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  49. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  50. }
  51. }
  52. // unblock the rev getter, so we can stop the compactor routine.
  53. _, err := rg.Wait(1)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. }
  58. func TestPeriodicPause(t *testing.T) {
  59. fc := clockwork.NewFakeClock()
  60. retentionDuration := time.Hour
  61. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  62. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  63. tb := newPeriodic(fc, retentionDuration, rg, compactable)
  64. tb.Run()
  65. tb.Pause()
  66. // tb will collect 3 hours of revisions but not compact since paused
  67. checkCompactInterval := retentionDuration / time.Duration(periodDivisor)
  68. n := periodDivisor
  69. for i := 0; i < 3*n; i++ {
  70. rg.Wait(1)
  71. fc.Advance(checkCompactInterval)
  72. }
  73. // tb ends up waiting for the clock
  74. select {
  75. case a := <-compactable.Chan():
  76. t.Fatalf("unexpected action %v", a)
  77. case <-time.After(10 * time.Millisecond):
  78. }
  79. // tb resumes to being blocked on the clock
  80. tb.Resume()
  81. // unblock clock, will kick off a compaction at hour 3:06
  82. rg.Wait(1)
  83. fc.Advance(checkCompactInterval)
  84. a, err := compactable.Wait(1)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. // compact the revision from hour 2:06
  89. wreq := &pb.CompactionRequest{Revision: int64(1 + 2*n + 1)}
  90. if !reflect.DeepEqual(a[0].Params[0], wreq) {
  91. t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
  92. }
  93. }