go.etcd.io/etcd@v3.3.27+incompatible/compactor/revision_test.go (about)

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