|
|
@@ -26,39 +26,36 @@ import (
|
|
|
|
|
|
func TestPeriodic(t *testing.T) {
|
|
|
retentionHours := 2
|
|
|
+ retentionDuration := time.Duration(retentionHours) * time.Hour
|
|
|
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
|
|
|
compactable := &fakeCompactable{testutil.NewRecorderStream()}
|
|
|
tb := &Periodic{
|
|
|
- clock: fc,
|
|
|
- periodInHour: retentionHours,
|
|
|
- rg: rg,
|
|
|
- c: compactable,
|
|
|
+ clock: fc,
|
|
|
+ period: retentionDuration,
|
|
|
+ rg: rg,
|
|
|
+ c: compactable,
|
|
|
}
|
|
|
|
|
|
tb.Run()
|
|
|
defer tb.Stop()
|
|
|
-
|
|
|
- n := int(time.Hour / checkCompactionInterval)
|
|
|
- // collect 5 hours of revisions
|
|
|
- for i := 0; i < 5; i++ {
|
|
|
- // advance one hour, one revision for each interval
|
|
|
- for j := 0; j < n; j++ {
|
|
|
- rg.Wait(1)
|
|
|
- fc.Advance(checkCompactionInterval)
|
|
|
- }
|
|
|
-
|
|
|
- // compaction doesn't happen til 2 hours elapses
|
|
|
- if i+1 < retentionHours {
|
|
|
+ checkCompactInterval := retentionDuration / time.Duration(periodDivisor)
|
|
|
+ n := periodDivisor
|
|
|
+ // simulate 5 hours worth of intervals.
|
|
|
+ for i := 0; i < n/retentionHours*5; i++ {
|
|
|
+ rg.Wait(1)
|
|
|
+ fc.Advance(checkCompactInterval)
|
|
|
+ // compaction doesn't happen til 2 hours elapses.
|
|
|
+ if i < n {
|
|
|
continue
|
|
|
}
|
|
|
-
|
|
|
+ // after 2 hours, compaction happens at every checkCompactInterval.
|
|
|
a, err := compactable.Wait(1)
|
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
|
}
|
|
|
- expectedRevision := int64(1 + (i+1)*n - retentionHours*n)
|
|
|
+ expectedRevision := int64(i + 1 - n)
|
|
|
if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
|
|
|
t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
|
|
|
}
|
|
|
@@ -75,21 +72,23 @@ func TestPeriodicPause(t *testing.T) {
|
|
|
fc := clockwork.NewFakeClock()
|
|
|
compactable := &fakeCompactable{testutil.NewRecorderStream()}
|
|
|
rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
|
|
|
+ retentionDuration := time.Hour
|
|
|
tb := &Periodic{
|
|
|
- clock: fc,
|
|
|
- periodInHour: 1,
|
|
|
- rg: rg,
|
|
|
- c: compactable,
|
|
|
+ clock: fc,
|
|
|
+ period: retentionDuration,
|
|
|
+ rg: rg,
|
|
|
+ c: compactable,
|
|
|
}
|
|
|
|
|
|
tb.Run()
|
|
|
tb.Pause()
|
|
|
|
|
|
// tb will collect 3 hours of revisions but not compact since paused
|
|
|
- n := int(time.Hour / checkCompactionInterval)
|
|
|
+ checkCompactInterval := retentionDuration / time.Duration(periodDivisor)
|
|
|
+ n := periodDivisor
|
|
|
for i := 0; i < 3*n; i++ {
|
|
|
rg.Wait(1)
|
|
|
- fc.Advance(checkCompactionInterval)
|
|
|
+ fc.Advance(checkCompactInterval)
|
|
|
}
|
|
|
// tb ends up waiting for the clock
|
|
|
|
|
|
@@ -102,14 +101,14 @@ func TestPeriodicPause(t *testing.T) {
|
|
|
// tb resumes to being blocked on the clock
|
|
|
tb.Resume()
|
|
|
|
|
|
- // unblock clock, will kick off a compaction at hour 3:05
|
|
|
+ // unblock clock, will kick off a compaction at hour 3:06
|
|
|
rg.Wait(1)
|
|
|
- fc.Advance(checkCompactionInterval)
|
|
|
+ fc.Advance(checkCompactInterval)
|
|
|
a, err := compactable.Wait(1)
|
|
|
if err != nil {
|
|
|
t.Fatal(err)
|
|
|
}
|
|
|
- // compact the revision from hour 2:05
|
|
|
+ // compact the revision from hour 2:06
|
|
|
wreq := &pb.CompactionRequest{Revision: int64(1 + 2*n + 1)}
|
|
|
if !reflect.DeepEqual(a[0].Params[0], wreq) {
|
|
|
t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
|