2015-10-14 09:22:03 +00:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDelay(t *testing.T) {
|
|
|
|
d := NewDelay()
|
|
|
|
|
|
|
|
// An unknown key should have a time in the past.
|
2019-11-25 17:57:35 +00:00
|
|
|
if exp := d.GetExpiration("nope", nil); !exp.Before(time.Now()) {
|
2015-10-14 09:22:03 +00:00
|
|
|
t.Fatalf("bad: %v", exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a key and set a short expiration.
|
|
|
|
now := time.Now()
|
|
|
|
delay := 250 * time.Millisecond
|
2019-11-25 17:57:35 +00:00
|
|
|
d.SetExpiration("bye", now, delay, nil)
|
|
|
|
if exp := d.GetExpiration("bye", nil); !exp.After(now) {
|
2015-10-14 09:22:03 +00:00
|
|
|
t.Fatalf("bad: %v", exp)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for the key to expire and check again.
|
|
|
|
time.Sleep(2 * delay)
|
2019-11-25 17:57:35 +00:00
|
|
|
if exp := d.GetExpiration("bye", nil); !exp.Before(now) {
|
2015-10-14 09:22:03 +00:00
|
|
|
t.Fatalf("bad: %v", exp)
|
|
|
|
}
|
|
|
|
}
|