periodic_test.go 3.1 KB

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