From 305ceb9ddd59ce97a8810bd20b9df644a916e482 Mon Sep 17 00:00:00 2001 From: tangcong Date: Mon, 16 Mar 2020 10:23:26 +0800 Subject: [PATCH] etcdserver/cindex: add unit test --- etcdserver/cindex/cindex_test.go | 71 ++++++++++++++++++++++++++++++++ 1 file changed, 71 insertions(+) create mode 100644 etcdserver/cindex/cindex_test.go diff --git a/etcdserver/cindex/cindex_test.go b/etcdserver/cindex/cindex_test.go new file mode 100644 index 000000000000..625adc59762d --- /dev/null +++ b/etcdserver/cindex/cindex_test.go @@ -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) + } +}