revision_test.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. rg.Wait(1)
  52. // nothing happens
  53. rg.SetRev(199) // will be 200
  54. expectedRevision = int64(190)
  55. fc.Advance(checkCompactionInterval)
  56. rg.Wait(1)
  57. a, err = compactable.Wait(1)
  58. if err != nil {
  59. t.Fatal(err)
  60. }
  61. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  62. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  63. }
  64. }
  65. func TestRevisionPause(t *testing.T) {
  66. fc := clockwork.NewFakeClock()
  67. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  68. rg := &fakeRevGetter{testutil.NewRecorderStream(), 99} // will be 100
  69. tb := &Revision{
  70. clock: fc,
  71. retention: 10,
  72. rg: rg,
  73. c: compactable,
  74. }
  75. tb.Run()
  76. tb.Pause()
  77. // tb will collect 3 hours of revisions but not compact since paused
  78. n := int(time.Hour / checkCompactionInterval)
  79. for i := 0; i < 3*n; i++ {
  80. fc.Advance(checkCompactionInterval)
  81. }
  82. // tb ends up waiting for the clock
  83. select {
  84. case a := <-compactable.Chan():
  85. t.Fatalf("unexpected action %v", a)
  86. case <-time.After(10 * time.Millisecond):
  87. }
  88. // tb resumes to being blocked on the clock
  89. tb.Resume()
  90. // unblock clock, will kick off a compaction at hour 3:05
  91. fc.Advance(checkCompactionInterval)
  92. rg.Wait(1)
  93. a, err := compactable.Wait(1)
  94. if err != nil {
  95. t.Fatal(err)
  96. }
  97. wreq := &pb.CompactionRequest{Revision: int64(90)}
  98. if !reflect.DeepEqual(a[0].Params[0], wreq) {
  99. t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
  100. }
  101. }