revision_test.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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 TestRevision(t *testing.T) {
  24. fc := clockwork.NewFakeClock()
  25. rg := &fakeRevGetter{testutil.NewRecorderStream(), 0}
  26. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  27. tb := &Revision{
  28. clock: fc,
  29. retention: 10,
  30. rg: rg,
  31. c: compactable,
  32. }
  33. tb.Run()
  34. defer tb.Stop()
  35. fc.Advance(checkCompactionInterval)
  36. rg.Wait(1)
  37. // nothing happens
  38. rg.SetRev(99) // will be 100
  39. expectedRevision := int64(90)
  40. fc.Advance(checkCompactionInterval)
  41. rg.Wait(1)
  42. a, err := compactable.Wait(1)
  43. if err != nil {
  44. t.Fatal(err)
  45. }
  46. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  47. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  48. }
  49. // skip the same revision
  50. rg.SetRev(99) // will be 100
  51. expectedRevision = int64(90)
  52. rg.Wait(1)
  53. // nothing happens
  54. rg.SetRev(199) // will be 200
  55. expectedRevision = int64(190)
  56. fc.Advance(checkCompactionInterval)
  57. rg.Wait(1)
  58. a, err = compactable.Wait(1)
  59. if err != nil {
  60. t.Fatal(err)
  61. }
  62. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  63. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  64. }
  65. }
  66. func TestRevisionPause(t *testing.T) {
  67. fc := clockwork.NewFakeClock()
  68. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  69. rg := &fakeRevGetter{testutil.NewRecorderStream(), 99} // will be 100
  70. tb := &Revision{
  71. clock: fc,
  72. retention: 10,
  73. rg: rg,
  74. c: compactable,
  75. }
  76. tb.Run()
  77. tb.Pause()
  78. // tb will collect 3 hours of revisions but not compact since paused
  79. n := int(time.Hour / checkCompactionInterval)
  80. for i := 0; i < 3*n; i++ {
  81. fc.Advance(checkCompactionInterval)
  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:05
  92. fc.Advance(checkCompactionInterval)
  93. rg.Wait(1)
  94. a, err := compactable.Wait(1)
  95. if err != nil {
  96. t.Fatal(err)
  97. }
  98. wreq := &pb.CompactionRequest{Revision: int64(90)}
  99. if !reflect.DeepEqual(a[0].Params[0], wreq) {
  100. t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
  101. }
  102. }