github.phpd.cn/hashicorp/consul@v1.4.5/agent/consul/state/delay_test.go (about)

     1  package state
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestDelay(t *testing.T) {
     9  	d := NewDelay()
    10  
    11  	// An unknown key should have a time in the past.
    12  	if exp := d.GetExpiration("nope"); !exp.Before(time.Now()) {
    13  		t.Fatalf("bad: %v", exp)
    14  	}
    15  
    16  	// Add a key and set a short expiration.
    17  	now := time.Now()
    18  	delay := 250 * time.Millisecond
    19  	d.SetExpiration("bye", now, delay)
    20  	if exp := d.GetExpiration("bye"); !exp.After(now) {
    21  		t.Fatalf("bad: %v", exp)
    22  	}
    23  
    24  	// Wait for the key to expire and check again.
    25  	time.Sleep(2 * delay)
    26  	if exp := d.GetExpiration("bye"); !exp.Before(now) {
    27  		t.Fatalf("bad: %v", exp)
    28  	}
    29  }