compactor_test.go 3.6 KB

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