revision_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. // Copyright 2017 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 v3compactor
  15. import (
  16. "reflect"
  17. "testing"
  18. "time"
  19. pb "go.etcd.io/etcd/etcdserver/etcdserverpb"
  20. "go.etcd.io/etcd/pkg/testutil"
  21. "github.com/jonboulle/clockwork"
  22. "go.uber.org/zap"
  23. )
  24. func TestRevision(t *testing.T) {
  25. fc := clockwork.NewFakeClock()
  26. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  27. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  28. tb := newRevision(zap.NewExample(), fc, 10, rg, compactable)
  29. tb.Run()
  30. defer tb.Stop()
  31. fc.Advance(revInterval)
  32. rg.Wait(1)
  33. // nothing happens
  34. rg.SetRev(99) // will be 100
  35. expectedRevision := int64(90)
  36. fc.Advance(revInterval)
  37. rg.Wait(1)
  38. a, err := compactable.Wait(1)
  39. if err != nil {
  40. t.Fatal(err)
  41. }
  42. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  43. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  44. }
  45. // skip the same revision
  46. rg.SetRev(99) // will be 100
  47. rg.Wait(1)
  48. // nothing happens
  49. rg.SetRev(199) // will be 200
  50. expectedRevision = int64(190)
  51. fc.Advance(revInterval)
  52. rg.Wait(1)
  53. a, err = compactable.Wait(1)
  54. if err != nil {
  55. t.Fatal(err)
  56. }
  57. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  58. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  59. }
  60. }
  61. func TestRevisionPause(t *testing.T) {
  62. fc := clockwork.NewFakeClock()
  63. rg := &fakeRevGetter{testutil.NewRecorderStream(), 99} // will be 100
  64. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  65. tb := newRevision(zap.NewExample(), fc, 10, rg, compactable)
  66. tb.Run()
  67. tb.Pause()
  68. // tb will collect 3 hours of revisions but not compact since paused
  69. n := int(time.Hour / revInterval)
  70. for i := 0; i < 3*n; i++ {
  71. fc.Advance(revInterval)
  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:05
  82. fc.Advance(revInterval)
  83. rg.Wait(1)
  84. a, err := compactable.Wait(1)
  85. if err != nil {
  86. t.Fatal(err)
  87. }
  88. wreq := &pb.CompactionRequest{Revision: int64(90)}
  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. }