github.com/avenga/couper@v1.12.2/cache/memory_test.go (about)

     1  package cache_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/avenga/couper/cache"
     9  	"github.com/avenga/couper/internal/test"
    10  )
    11  
    12  func TestCache_All(t *testing.T) {
    13  	log, _ := test.NewLogger()
    14  	logger := log.WithContext(context.Background())
    15  
    16  	quitCh := make(chan struct{})
    17  	defer close(quitCh)
    18  	ms := cache.New(logger, quitCh)
    19  
    20  	if v := ms.Get("key"); v != nil {
    21  		t.Errorf("Nil expected, given %q", v)
    22  	}
    23  
    24  	go ms.Set("key", "val", 2)
    25  	go ms.Set("del", "del", 30)
    26  	go func() {
    27  		ms.Get("key")
    28  	}()
    29  
    30  	time.Sleep(300 * time.Millisecond)
    31  
    32  	if v := ms.Get("key"); v != "val" {
    33  		t.Errorf("Expected 'val', given %q", v)
    34  	}
    35  	if v := ms.Get("del"); v != "del" {
    36  		t.Errorf("Expected 'del', given %q", v)
    37  	}
    38  
    39  	time.Sleep(1700 * time.Millisecond)
    40  
    41  	if v := ms.Get("key"); v != nil {
    42  		t.Errorf("Nil expected, given %q", v)
    43  	}
    44  	if v := ms.Get("del"); v != "del" {
    45  		t.Errorf("Expected 'del', given %q", v)
    46  	}
    47  }