2023-03-28 18:39:22 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2015-10-12 07:42:09 +00:00
|
|
|
package state
|
2014-12-11 01:17:29 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestTombstoneGC_invalid(t *testing.T) {
|
|
|
|
_, err := NewTombstoneGC(0, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = NewTombstoneGC(time.Second, 0)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = NewTombstoneGC(0, time.Second)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatalf("should fail")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTombstoneGC(t *testing.T) {
|
2017-11-09 06:18:37 +00:00
|
|
|
ttl := 20 * time.Millisecond
|
|
|
|
gran := 5 * time.Millisecond
|
2014-12-11 01:17:29 +00:00
|
|
|
gc, err := NewTombstoneGC(ttl, gran)
|
|
|
|
if err != nil {
|
2017-04-27 23:41:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-12-11 01:17:29 +00:00
|
|
|
}
|
2015-01-05 22:58:59 +00:00
|
|
|
gc.SetEnabled(true)
|
2014-12-11 01:17:29 +00:00
|
|
|
|
2014-12-15 22:22:32 +00:00
|
|
|
if gc.PendingExpiration() {
|
|
|
|
t.Fatalf("should not be pending")
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:17:29 +00:00
|
|
|
start := time.Now()
|
|
|
|
gc.Hint(100)
|
|
|
|
|
2014-12-15 22:22:32 +00:00
|
|
|
if !gc.PendingExpiration() {
|
|
|
|
t.Fatalf("should be pending")
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:17:29 +00:00
|
|
|
select {
|
|
|
|
case index := <-gc.ExpireCh():
|
|
|
|
end := time.Now()
|
|
|
|
if end.Sub(start) < ttl {
|
|
|
|
t.Fatalf("expired early")
|
|
|
|
}
|
|
|
|
if index != 100 {
|
|
|
|
t.Fatalf("bad index: %d", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(ttl * 2):
|
|
|
|
t.Fatalf("should get expiration")
|
|
|
|
}
|
|
|
|
|
2017-11-08 23:09:29 +00:00
|
|
|
start2 := time.Now()
|
|
|
|
gc.Hint(120)
|
|
|
|
gc.Hint(125)
|
|
|
|
|
2017-11-29 18:34:24 +00:00
|
|
|
// Check that we only have a single bin (this cross-checks #3670).
|
|
|
|
gc.Lock()
|
|
|
|
bins := len(gc.expires)
|
|
|
|
gc.Unlock()
|
|
|
|
if got, want := bins, 1; got != want {
|
|
|
|
t.Fatalf("got %d want %d", got, want)
|
|
|
|
}
|
|
|
|
|
2017-11-08 23:09:29 +00:00
|
|
|
if !gc.PendingExpiration() {
|
|
|
|
t.Fatalf("should be pending")
|
|
|
|
}
|
|
|
|
|
2014-12-11 01:17:29 +00:00
|
|
|
select {
|
|
|
|
case index := <-gc.ExpireCh():
|
|
|
|
end := time.Now()
|
|
|
|
if end.Sub(start2) < ttl {
|
|
|
|
t.Fatalf("expired early")
|
|
|
|
}
|
|
|
|
if index != 125 {
|
|
|
|
t.Fatalf("bad index: %d", index)
|
|
|
|
}
|
|
|
|
|
|
|
|
case <-time.After(ttl * 2):
|
|
|
|
t.Fatalf("should get expiration")
|
|
|
|
}
|
|
|
|
}
|
2014-12-11 06:33:26 +00:00
|
|
|
|
|
|
|
func TestTombstoneGC_Expire(t *testing.T) {
|
|
|
|
ttl := 10 * time.Millisecond
|
|
|
|
gran := 5 * time.Millisecond
|
|
|
|
gc, err := NewTombstoneGC(ttl, gran)
|
|
|
|
if err != nil {
|
2017-04-27 23:41:42 +00:00
|
|
|
t.Fatalf("err: %v", err)
|
2014-12-11 06:33:26 +00:00
|
|
|
}
|
2015-01-05 22:58:59 +00:00
|
|
|
gc.SetEnabled(true)
|
2014-12-11 06:33:26 +00:00
|
|
|
|
2014-12-15 22:22:32 +00:00
|
|
|
if gc.PendingExpiration() {
|
|
|
|
t.Fatalf("should not be pending")
|
|
|
|
}
|
|
|
|
|
2014-12-11 06:33:26 +00:00
|
|
|
gc.Hint(100)
|
2015-01-05 22:58:59 +00:00
|
|
|
gc.SetEnabled(false)
|
2014-12-11 06:33:26 +00:00
|
|
|
|
2014-12-15 22:22:32 +00:00
|
|
|
if gc.PendingExpiration() {
|
|
|
|
t.Fatalf("should not be pending")
|
|
|
|
}
|
|
|
|
|
2014-12-11 06:33:26 +00:00
|
|
|
select {
|
|
|
|
case <-gc.ExpireCh():
|
2015-09-11 19:24:54 +00:00
|
|
|
t.Fatalf("should be reset")
|
2017-11-08 21:43:39 +00:00
|
|
|
case <-time.After(ttl * 2):
|
2014-12-11 06:33:26 +00:00
|
|
|
}
|
|
|
|
}
|