github.com/clly/consul@v1.4.5/agent/consul/state/tombstone_gc_test.go (about)

     1  package state
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestTombstoneGC_invalid(t *testing.T) {
     9  	_, err := NewTombstoneGC(0, 0)
    10  	if err == nil {
    11  		t.Fatalf("should fail")
    12  	}
    13  
    14  	_, err = NewTombstoneGC(time.Second, 0)
    15  	if err == nil {
    16  		t.Fatalf("should fail")
    17  	}
    18  
    19  	_, err = NewTombstoneGC(0, time.Second)
    20  	if err == nil {
    21  		t.Fatalf("should fail")
    22  	}
    23  }
    24  
    25  func TestTombstoneGC(t *testing.T) {
    26  	ttl := 20 * time.Millisecond
    27  	gran := 5 * time.Millisecond
    28  	gc, err := NewTombstoneGC(ttl, gran)
    29  	if err != nil {
    30  		t.Fatalf("err: %v", err)
    31  	}
    32  	gc.SetEnabled(true)
    33  
    34  	if gc.PendingExpiration() {
    35  		t.Fatalf("should not be pending")
    36  	}
    37  
    38  	start := time.Now()
    39  	gc.Hint(100)
    40  
    41  	if !gc.PendingExpiration() {
    42  		t.Fatalf("should be pending")
    43  	}
    44  
    45  	select {
    46  	case index := <-gc.ExpireCh():
    47  		end := time.Now()
    48  		if end.Sub(start) < ttl {
    49  			t.Fatalf("expired early")
    50  		}
    51  		if index != 100 {
    52  			t.Fatalf("bad index: %d", index)
    53  		}
    54  
    55  	case <-time.After(ttl * 2):
    56  		t.Fatalf("should get expiration")
    57  	}
    58  
    59  	start2 := time.Now()
    60  	gc.Hint(120)
    61  	gc.Hint(125)
    62  
    63  	// Check that we only have a single bin (this cross-checks #3670).
    64  	gc.Lock()
    65  	bins := len(gc.expires)
    66  	gc.Unlock()
    67  	if got, want := bins, 1; got != want {
    68  		t.Fatalf("got %d want %d", got, want)
    69  	}
    70  
    71  	if !gc.PendingExpiration() {
    72  		t.Fatalf("should be pending")
    73  	}
    74  
    75  	select {
    76  	case index := <-gc.ExpireCh():
    77  		end := time.Now()
    78  		if end.Sub(start2) < ttl {
    79  			t.Fatalf("expired early")
    80  		}
    81  		if index != 125 {
    82  			t.Fatalf("bad index: %d", index)
    83  		}
    84  
    85  	case <-time.After(ttl * 2):
    86  		t.Fatalf("should get expiration")
    87  	}
    88  }
    89  
    90  func TestTombstoneGC_Expire(t *testing.T) {
    91  	ttl := 10 * time.Millisecond
    92  	gran := 5 * time.Millisecond
    93  	gc, err := NewTombstoneGC(ttl, gran)
    94  	if err != nil {
    95  		t.Fatalf("err: %v", err)
    96  	}
    97  	gc.SetEnabled(true)
    98  
    99  	if gc.PendingExpiration() {
   100  		t.Fatalf("should not be pending")
   101  	}
   102  
   103  	gc.Hint(100)
   104  	gc.SetEnabled(false)
   105  
   106  	if gc.PendingExpiration() {
   107  		t.Fatalf("should not be pending")
   108  	}
   109  
   110  	select {
   111  	case <-gc.ExpireCh():
   112  		t.Fatalf("should be reset")
   113  	case <-time.After(ttl * 2):
   114  	}
   115  }