...

Source file src/go.etcd.io/etcd/server/v3/etcdserver/cindex/cindex_test.go

Documentation: go.etcd.io/etcd/server/v3/etcdserver/cindex

     1  // Copyright 2015 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 cindex
    16  
    17  import (
    18  	"math/rand"
    19  	"testing"
    20  	"time"
    21  
    22  	"github.com/stretchr/testify/assert"
    23  	"go.etcd.io/etcd/server/v3/mvcc/backend"
    24  	betesting "go.etcd.io/etcd/server/v3/mvcc/backend/testing"
    25  )
    26  
    27  // TestConsistentIndex ensures that LoadConsistentIndex/Save/ConsistentIndex and backend.BatchTx can work well together.
    28  func TestConsistentIndex(t *testing.T) {
    29  
    30  	be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
    31  	ci := NewConsistentIndex(be)
    32  
    33  	tx := be.BatchTx()
    34  	if tx == nil {
    35  		t.Fatal("batch tx is nil")
    36  	}
    37  	tx.Lock()
    38  
    39  	UnsafeCreateMetaBucket(tx)
    40  	tx.Unlock()
    41  	be.ForceCommit()
    42  	r := uint64(7890123)
    43  	term := uint64(234)
    44  	ci.SetConsistentIndex(r, term)
    45  	index := ci.ConsistentIndex()
    46  	if index != r {
    47  		t.Errorf("expected %d,got %d", r, index)
    48  	}
    49  	tx.Lock()
    50  	ci.UnsafeSave(tx)
    51  	tx.Unlock()
    52  	be.ForceCommit()
    53  	be.Close()
    54  
    55  	b := backend.NewDefaultBackend(tmpPath)
    56  	defer b.Close()
    57  	ci.SetBackend(b)
    58  	index = ci.ConsistentIndex()
    59  	assert.Equal(t, r, index)
    60  
    61  	ci = NewConsistentIndex(b)
    62  	index = ci.ConsistentIndex()
    63  	assert.Equal(t, r, index)
    64  }
    65  
    66  func TestConsistentIndexDecrease(t *testing.T) {
    67  	initIndex := uint64(100)
    68  	initTerm := uint64(10)
    69  
    70  	tcs := []struct {
    71  		name  string
    72  		index uint64
    73  		term  uint64
    74  	}{
    75  		{
    76  			name:  "Decrease term",
    77  			index: initIndex + 1,
    78  			term:  initTerm - 1,
    79  		},
    80  		{
    81  			name:  "Decrease CI",
    82  			index: initIndex - 1,
    83  			term:  initTerm + 1,
    84  		},
    85  		{
    86  			name:  "Decrease CI and term",
    87  			index: initIndex - 1,
    88  			term:  initTerm - 1,
    89  		},
    90  	}
    91  	for _, tc := range tcs {
    92  		t.Run(tc.name, func(t *testing.T) {
    93  			be, tmpPath := betesting.NewTmpBackend(t, time.Microsecond, 10)
    94  			tx := be.BatchTx()
    95  			tx.Lock()
    96  			UnsafeCreateMetaBucket(tx)
    97  			UnsafeUpdateConsistentIndex(tx, initIndex, initTerm)
    98  			tx.Unlock()
    99  			be.ForceCommit()
   100  			be.Close()
   101  
   102  			be = backend.NewDefaultBackend(tmpPath)
   103  			defer be.Close()
   104  			ci := NewConsistentIndex(be)
   105  			ci.SetConsistentIndex(tc.index, tc.term)
   106  			tx = be.BatchTx()
   107  			tx.Lock()
   108  			ci.UnsafeSave(tx)
   109  			tx.Unlock()
   110  			assert.Equal(t, tc.index, ci.ConsistentIndex())
   111  
   112  			ci = NewConsistentIndex(be)
   113  			assert.Equal(t, tc.index, ci.ConsistentIndex())
   114  		})
   115  	}
   116  }
   117  
   118  func TestFakeConsistentIndex(t *testing.T) {
   119  
   120  	r := rand.Uint64()
   121  	ci := NewFakeConsistentIndex(r)
   122  	index := ci.ConsistentIndex()
   123  	if index != r {
   124  		t.Errorf("expected %d,got %d", r, index)
   125  	}
   126  	r = rand.Uint64()
   127  	ci.SetConsistentIndex(r, 5)
   128  	index = ci.ConsistentIndex()
   129  	if index != r {
   130  		t.Errorf("expected %d,got %d", r, index)
   131  	}
   132  
   133  }
   134  

View as plain text