etcd/lease/lessor_test.go

175 lines
4.3 KiB
Go
Raw Normal View History

2015-11-08 22:15:39 +03:00
// Copyright 2015 CoreOS, Inc.
//
// 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 lease
import (
"io/ioutil"
"os"
"path"
2015-11-08 22:15:39 +03:00
"reflect"
"testing"
"time"
"github.com/coreos/etcd/storage/backend"
2015-11-08 22:15:39 +03:00
)
// TestLessorGrant ensures Lessor can grant wanted lease.
// The granted lease should have a unique ID with a term
// that is greater than minLeaseTerm.
func TestLessorGrant(t *testing.T) {
dir, be := NewTestBackend(t)
defer os.RemoveAll(dir)
defer be.Close()
le := newLessor(1, be, &fakeDeleteable{})
le.Promote()
2015-11-08 22:15:39 +03:00
l := le.Grant(1)
gl := le.get(l.ID)
2015-11-08 22:15:39 +03:00
if !reflect.DeepEqual(gl, l) {
t.Errorf("lease = %v, want %v", gl, l)
}
if l.expiry.Sub(time.Now()) < time.Duration(minLeaseTTL)*time.Second-time.Second {
t.Errorf("term = %v, want at least %v", l.expiry.Sub(time.Now()), time.Duration(minLeaseTTL)*time.Second-time.Second)
2015-11-08 22:15:39 +03:00
}
nl := le.Grant(1)
if nl.ID == l.ID {
t.Errorf("new lease.id = %x, want != %x", nl.ID, l.ID)
2015-11-08 22:15:39 +03:00
}
be.BatchTx().Lock()
_, vs := be.BatchTx().UnsafeRange(leaseBucketName, int64ToBytes(int64(l.ID)), nil, 0)
if len(vs) != 1 {
t.Errorf("len(vs) = %d, want 1", len(vs))
}
be.BatchTx().Unlock()
2015-11-08 22:15:39 +03:00
}
// TestLessorRevoke ensures Lessor can revoke a lease.
// The items in the revoked lease should be removed from
// the DeleteableKV.
2015-11-08 22:15:39 +03:00
// The revoked lease cannot be got from Lessor again.
func TestLessorRevoke(t *testing.T) {
dir, be := NewTestBackend(t)
defer os.RemoveAll(dir)
defer be.Close()
fd := &fakeDeleteable{}
le := newLessor(1, be, fd)
2015-11-08 22:15:39 +03:00
// grant a lease with long term (100 seconds) to
// avoid early termination during the test.
l := le.Grant(100)
2015-11-08 22:15:39 +03:00
items := []leaseItem{
{"foo"},
{"bar"},
}
err := le.Attach(l.ID, items)
if err != nil {
t.Fatalf("failed to attach items to the lease: %v", err)
}
err = le.Revoke(l.ID)
2015-11-08 22:15:39 +03:00
if err != nil {
t.Fatal("failed to revoke lease:", err)
}
if le.get(l.ID) != nil {
t.Errorf("got revoked lease %x", l.ID)
2015-11-08 22:15:39 +03:00
}
wdeleted := []string{"foo_", "bar_"}
if !reflect.DeepEqual(fd.deleted, wdeleted) {
t.Errorf("deleted= %v, want %v", fd.deleted, wdeleted)
}
be.BatchTx().Lock()
_, vs := be.BatchTx().UnsafeRange(leaseBucketName, int64ToBytes(int64(l.ID)), nil, 0)
if len(vs) != 0 {
t.Errorf("len(vs) = %d, want 0", len(vs))
}
be.BatchTx().Unlock()
2015-11-08 22:15:39 +03:00
}
// TestLessorRenew ensures Lessor can renew an existing lease.
func TestLessorRenew(t *testing.T) {
dir, be := NewTestBackend(t)
defer be.Close()
defer os.RemoveAll(dir)
2015-11-08 22:15:39 +03:00
le := newLessor(1, be, &fakeDeleteable{})
2016-01-09 00:17:54 +03:00
le.Promote()
l := le.Grant(5)
// manually change the ttl field
l.TTL = 10
2016-01-09 00:17:54 +03:00
err := le.Renew(l.ID)
if err != nil {
t.Fatalf("failed to renew lease (%v)", err)
}
l = le.get(l.ID)
2015-11-08 22:15:39 +03:00
if l.expiry.Sub(time.Now()) < 9*time.Second {
t.Errorf("failed to renew the lease")
2015-11-08 22:15:39 +03:00
}
}
2016-01-05 21:57:59 +03:00
// TestLessorRecover ensures Lessor recovers leases from
// persist backend.
func TestLessorRecover(t *testing.T) {
dir, be := NewTestBackend(t)
defer os.RemoveAll(dir)
defer be.Close()
le := newLessor(1, be, &fakeDeleteable{})
2016-01-05 21:57:59 +03:00
l1 := le.Grant(10)
l2 := le.Grant(20)
// Create a new lessor with the same backend
nle := newLessor(1, be, &fakeDeleteable{})
nl1 := nle.get(l1.ID)
if nl1 == nil || nl1.TTL != l1.TTL {
t.Errorf("nl1 = %v, want nl1.TTL= %d", nl1.TTL, l1.TTL)
2016-01-05 21:57:59 +03:00
}
nl2 := nle.get(l2.ID)
if nl2 == nil || nl2.TTL != l2.TTL {
t.Errorf("nl2 = %v, want nl2.TTL= %d", nl2.TTL, l2.TTL)
2016-01-05 21:57:59 +03:00
}
}
type fakeDeleteable struct {
deleted []string
}
func (fd *fakeDeleteable) DeleteRange(key, end []byte) (int64, int64) {
fd.deleted = append(fd.deleted, string(key)+"_"+string(end))
return 0, 0
}
func NewTestBackend(t *testing.T) (string, backend.Backend) {
tmpPath, err := ioutil.TempDir("", "lease")
if err != nil {
t.Fatalf("failed to create tmpdir (%v)", err)
}
return tmpPath, backend.New(path.Join(tmpPath, "be"), time.Second, 10000)
}