Skip to content

Commit

Permalink
etcdserver/cindex: add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
tangcong committed Mar 16, 2020
1 parent 57d26ac commit 305ceb9
Showing 1 changed file with 71 additions and 0 deletions.
71 changes: 71 additions & 0 deletions etcdserver/cindex/cindex_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
// Copyright 2015 The etcd Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package cindex

import (
"go.etcd.io/etcd/mvcc/backend"
"math/rand"
"os"
"testing"
"time"
)

// TestConsistentIndex ensures that LoadConsistentIndex/Save/ConsistentIndex and backend.BatchTx can work well together.
func TestConsistentIndex(t *testing.T) {

be, tmpPath := backend.NewTmpBackend(time.Microsecond, 10)
defer os.Remove(tmpPath)
ci := NewConsistentIndex()

tx := be.BatchTx()
if tx == nil {
t.Fatal("batch tx is nil")
}
tx.Lock()
tx.UnsafeCreateBucket(metaBucketName)
tx.Unlock()
be.ForceCommit()
ci.LoadConsistentIndex(be.BatchTx())
r := rand.Uint64()
ci.SetConsistentIndex(r)
index := ci.ConsistentIndex()
if index != r {
t.Errorf("expected %d,got %d", r, index)
}
ci.Save(be.BatchTx())
be.ForceCommit()

ci = NewConsistentIndex()
index = ci.LoadConsistentIndex(be.BatchTx())
if index != r {
t.Errorf("expected %d,got %d", r, index)
}
index = ci.ConsistentIndex()
if index != r {
t.Errorf("expected %d,got %d", r, index)
}
be.Close()

b := backend.NewDefaultBackend(tmpPath)
ci = NewConsistentIndex()
index = ci.LoadConsistentIndex(b.BatchTx())
if index != r {
t.Errorf("expected %d,got %d", r, index)
}
index = ci.ConsistentIndex()
if index != r {
t.Errorf("expected %d,got %d", r, index)
}
}

0 comments on commit 305ceb9

Please sign in to comment.