revision_test.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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 := newRevision(fc, 10, rg, compactable)
  28. tb.Run()
  29. defer tb.Stop()
  30. fc.Advance(revInterval)
  31. rg.Wait(1)
  32. // nothing happens
  33. rg.SetRev(99) // will be 100
  34. expectedRevision := int64(90)
  35. fc.Advance(revInterval)
  36. rg.Wait(1)
  37. a, err := compactable.Wait(1)
  38. if err != nil {
  39. t.Fatal(err)
  40. }
  41. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  42. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  43. }
  44. // skip the same revision
  45. rg.SetRev(99) // will be 100
  46. rg.Wait(1)
  47. // nothing happens
  48. rg.SetRev(199) // will be 200
  49. expectedRevision = int64(190)
  50. fc.Advance(revInterval)
  51. rg.Wait(1)
  52. a, err = compactable.Wait(1)
  53. if err != nil {
  54. t.Fatal(err)
  55. }
  56. if !reflect.DeepEqual(a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision}) {
  57. t.Errorf("compact request = %v, want %v", a[0].Params[0], &pb.CompactionRequest{Revision: expectedRevision})
  58. }
  59. }
  60. func TestRevisionPause(t *testing.T) {
  61. fc := clockwork.NewFakeClock()
  62. rg := &fakeRevGetter{testutil.NewRecorderStream(), 99} // will be 100
  63. compactable := &fakeCompactable{testutil.NewRecorderStream()}
  64. tb := newRevision(fc, 10, rg, compactable)
  65. tb.Run()
  66. tb.Pause()
  67. // tb will collect 3 hours of revisions but not compact since paused
  68. n := int(time.Hour / revInterval)
  69. for i := 0; i < 3*n; i++ {
  70. fc.Advance(revInterval)
  71. }
  72. // tb ends up waiting for the clock
  73. select {
  74. case a := <-compactable.Chan():
  75. t.Fatalf("unexpected action %v", a)
  76. case <-time.After(10 * time.Millisecond):
  77. }
  78. // tb resumes to being blocked on the clock
  79. tb.Resume()
  80. // unblock clock, will kick off a compaction at hour 3:05
  81. fc.Advance(revInterval)
  82. rg.Wait(1)
  83. a, err := compactable.Wait(1)
  84. if err != nil {
  85. t.Fatal(err)
  86. }
  87. wreq := &pb.CompactionRequest{Revision: int64(90)}
  88. if !reflect.DeepEqual(a[0].Params[0], wreq) {
  89. t.Errorf("compact request = %v, want %v", a[0].Params[0], wreq.Revision)
  90. }
  91. }