|
|
@@ -24,7 +24,7 @@ import (
|
|
|
// The granted lease should have a unique ID with a term
|
|
|
// that is greater than minLeaseTerm.
|
|
|
func TestLessorGrant(t *testing.T) {
|
|
|
- le := NewLessor(1)
|
|
|
+ le := NewLessor(1, &fakeDeleteable{})
|
|
|
|
|
|
l := le.Grant(time.Now().Add(time.Second))
|
|
|
gl := le.get(l.id)
|
|
|
@@ -43,15 +43,28 @@ func TestLessorGrant(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
// TestLessorRevoke ensures Lessor can revoke a lease.
|
|
|
+// The items in the revoked lease should be removed from
|
|
|
+// the DeleteableKV.
|
|
|
// The revoked lease cannot be got from Lessor again.
|
|
|
func TestLessorRevoke(t *testing.T) {
|
|
|
- le := NewLessor(1)
|
|
|
+ fd := &fakeDeleteable{}
|
|
|
+ le := NewLessor(1, fd)
|
|
|
|
|
|
// grant a lease with long term (100 seconds) to
|
|
|
// avoid early termination during the test.
|
|
|
l := le.Grant(time.Now().Add(100 * time.Second))
|
|
|
|
|
|
- err := le.Revoke(l.id)
|
|
|
+ items := []leaseItem{
|
|
|
+ {"foo", ""},
|
|
|
+ {"bar", "zar"},
|
|
|
+ }
|
|
|
+
|
|
|
+ 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)
|
|
|
if err != nil {
|
|
|
t.Fatal("failed to revoke lease:", err)
|
|
|
}
|
|
|
@@ -59,11 +72,16 @@ func TestLessorRevoke(t *testing.T) {
|
|
|
if le.get(l.id) != nil {
|
|
|
t.Errorf("got revoked lease %x", l.id)
|
|
|
}
|
|
|
+
|
|
|
+ wdeleted := []string{"foo_", "bar_zar"}
|
|
|
+ if !reflect.DeepEqual(fd.deleted, wdeleted) {
|
|
|
+ t.Errorf("deleted= %v, want %v", fd.deleted, wdeleted)
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
// TestLessorRenew ensures Lessor can renew an existing lease.
|
|
|
func TestLessorRenew(t *testing.T) {
|
|
|
- le := NewLessor(1)
|
|
|
+ le := NewLessor(1, &fakeDeleteable{})
|
|
|
l := le.Grant(time.Now().Add(5 * time.Second))
|
|
|
|
|
|
le.Renew(l.id, time.Now().Add(100*time.Second))
|
|
|
@@ -73,3 +91,12 @@ func TestLessorRenew(t *testing.T) {
|
|
|
t.Errorf("failed to renew the lease for 100 seconds")
|
|
|
}
|
|
|
}
|
|
|
+
|
|
|
+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
|
|
|
+}
|