github.com/saintwish/kv@v1.0.4/kv1/kv1_test.go (about)

     1  package kv1
     2  
     3  import (
     4  	"fmt"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestSetGet_KeyString(t *testing.T) {
    10  	cache := New[string, string](time.Minute, 2048, 32)
    11  	cache.Set("unicorns", "are cool")
    12  
    13  	if res := cache.Get("unicorns"); res != "are cool" {
    14  		t.Errorf("Result was incorrect, got: %s, want: %s.", res, "are cool")
    15  	}
    16  }
    17  
    18  func TestSetGet_KeyInt(t *testing.T) {
    19  	cache := New[int, string](time.Minute, 2048, 32)
    20  	cache.Set(1337, "leet haxiors")
    21  
    22  	if res := cache.Get(1337); res != "leet haxiors" {
    23  		t.Errorf("Result was incorrect, got: %s, want: %s.", res, "leet haxiors")
    24  	}
    25  }
    26  
    27  func TestFlush(t *testing.T) {
    28  	cache := New[int, string](time.Minute, 2048, 32)
    29  	cache.SetOnEvicted(func(k int, v string){
    30  		fmt.Println(k)
    31  	})
    32  
    33  	cache.Set(1337, "leet haxiors")
    34  	cache.Set(1338, "leet haxiors1")
    35  	cache.Set(3434, "leet haxiors2")
    36  	cache.Set(5465, "leet haxiors3")
    37  
    38  	cache.Flush()
    39  }
    40  
    41  func TestExpired(t *testing.T) {
    42  	cache := New[int, string](200*time.Millisecond, 2048, 32)
    43  	cache.SetOnEvicted(func(k int, v string){
    44  		fmt.Printf("Evicted: %d\n", k)
    45  	})
    46  
    47  	cache.Set(1337, "leet haxiors")
    48  	cache.Set(1338, "leet haxiors1")
    49  	cache.Set(3434, "leet haxiors2")
    50  	cache.Set(5465, "leet haxiors3")
    51  
    52  	time.Sleep(700*time.Millisecond)
    53  
    54  	cache.DeleteExpired()
    55  }