compactor_test.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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, one revision for each interval
  40. for j := 0; j < n; j++ {
  41. fc.Advance(checkCompactionInterval)
  42. rg.Wait(1)
  43. }
  44. // ready to acknowledge hour "i"; unblock clock
  45. fc.Advance(checkCompactionInterval)
  46. a, err := compactable.Wait(1)
  47. if err != nil {
  48. t.Fatal(err)
  49. }
  50. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: int64(i*n) + 1}) {
  51. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: int64(i*n) + 1})
  52. }
  53. }
  54. }
  55. func TestPeriodicPause(t *testing.T) {
  56. fc := clockwork.NewFakeClock()
  57. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  58. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  59. tb := &Periodic{
  60. clock: fc,
  61. periodInHour: 1,
  62. rg: rg,
  63. c: compactable,
  64. }
  65. tb.Run()
  66. tb.Pause()
  67. // tb will collect 3 hours of revisions but not compact since paused
  68. n := int(time.Hour / checkCompactionInterval)
  69. for i := 0; i < 3*n; i++ {
  70. fc.Advance(checkCompactionInterval)
  71. rg.Wait(1)
  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
  82. fc.Advance(checkCompactionInterval)
  83. a, err := compactable.Wait(1)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. // compact the revision from hour 2
  88. wreq := &pb.CompactionRequest{Revision: int64(2*n + 1)}
  89. if !reflect.DeepEqual(a[0].Params[0], wreq) {
  90. t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
  91. }
  92. }
  93. type fakeCompactable struct {
  94. testutil.Recorder
  95. }
  96. func (fc *fakeCompactable) Compact(ctx context.Context, r *pb.CompactionRequest) (*pb.CompactionResponse, error) {
  97. fc.Record(testutil.Action{Name: "c", Params: []interface{}{r}})
  98. return &pb.CompactionResponse{}, nil
  99. }
  100. type fakeRevGetter struct {
  101. testutil.Recorder
  102. rev int64
  103. }
  104. func (fr *fakeRevGetter) Rev() int64 {
  105. fr.Record(testutil.Action{Name: "g"})
  106. fr.rev++
  107. return fr.rev
  108. }