all repos — volatile @ v0.0.1

A (dead) simple volatile data storage written in Go.

volatile_test.go (view raw)

  1package volatile
  2
  3import (
  4	"testing"
  5	"time"
  6)
  7
  8func TestVolatile_SetGet(t *testing.T) {
  9	cache := NewVolatile[string, string](2*time.Second, 1*time.Second)
 10
 11	key := "key1"
 12	value := "value1"
 13	cache.Set(key, &value)
 14
 15	got, err := cache.Get(key)
 16	if err != nil {
 17		t.Fatalf("unexpected error: %v", err)
 18	}
 19	if *got != value {
 20		t.Errorf("got %v, want %v", *got, value)
 21	}
 22
 23	if !cache.Has(key) {
 24		t.Errorf("expected key %v to exist", key)
 25	}
 26}
 27
 28func TestVolatile_Remove(t *testing.T) {
 29	cache := NewVolatile[string, string](2*time.Second, 1*time.Second)
 30
 31	key := "key1"
 32	value := "value1"
 33	cache.Set(key, &value)
 34
 35	got, err := cache.Remove(key)
 36	if err != nil {
 37		t.Fatalf("unexpected error: %v", err)
 38	}
 39	if *got != value {
 40		t.Errorf("got %v, want %v", *got, value)
 41	}
 42
 43	if cache.Has(key) {
 44		t.Errorf("expected key %v to be removed", key)
 45	}
 46}
 47
 48func TestVolatile_Length(t *testing.T) {
 49	cache := NewVolatile[string, int](100*time.Millisecond, 50*time.Millisecond)
 50
 51	if cache.Length() != 0 {
 52		t.Errorf("expected length to be 0, got %d", cache.Length())
 53	}
 54
 55	v1 := 1
 56	v2 := 2
 57
 58	cache.Set("key1", &v1)
 59	cache.Set("key2", &v2)
 60
 61	if cache.Length() != 2 {
 62		t.Errorf("expected length to be 2, got %d", cache.Length())
 63	}
 64
 65	// Wait for the elements to expire and be cleaned up
 66	time.Sleep(300 * time.Millisecond)
 67
 68	if cache.Length() != 0 {
 69		t.Errorf("expected length to be 0 after expiration, got %d", cache.Length())
 70	}
 71}
 72
 73func TestVolatile_Clear(t *testing.T) {
 74	cache := NewVolatile[string, int](2*time.Second, 1*time.Second)
 75
 76	v1 := 1
 77	v2 := 2
 78	v3 := 3
 79
 80	// Insert multiple items
 81	cache.Set("key1", &v1)
 82	cache.Set("key2", &v2)
 83	cache.Set("key3", &v3)
 84
 85	// Ensure items are set
 86	if !cache.Has("key1") || !cache.Has("key2") || !cache.Has("key3") {
 87		t.Fatal("expected keys to be set")
 88	}
 89
 90	// Clear the cache
 91	cache.Clear()
 92
 93	// Ensure all items are cleared
 94	if cache.Has("key1") || cache.Has("key2") || cache.Has("key3") {
 95		t.Fatal("expected all keys to be cleared")
 96	}
 97
 98	// Ensure map is empty
 99	if len(cache.data) != 0 {
100		t.Errorf("expected map to be empty, got %d elements", len(cache.data))
101	}
102}
103
104func TestVolatile_Clean(t *testing.T) {
105	cache := NewVolatile[string, string](100*time.Millisecond, 50*time.Millisecond)
106
107	key := "key1"
108	value := "value1"
109	cache.Set(key, &value)
110
111	time.Sleep(150 * time.Millisecond) // Wait for the element to expire
112
113	if cache.Has(key) {
114		t.Errorf("expected key %v to be expired and removed", key)
115	}
116
117	_, err := cache.Get(key)
118	if err == nil {
119		t.Errorf("expected error when getting expired key %v", key)
120	}
121}
122
123func TestVolatile_AutomaticCleanup(t *testing.T) {
124	cache := NewVolatile[string, string](100*time.Millisecond, 50*time.Millisecond)
125
126	key1 := "key1"
127	value1 := "value1"
128	cache.Set(key1, &value1)
129
130	key2 := "key2"
131	value2 := "value2"
132	cache.Set(key2, &value2)
133
134	time.Sleep(150 * time.Millisecond) // Wait for the elements to expire
135
136	if cache.Has(key1) || cache.Has(key2) {
137		t.Errorf("expected all keys to be expired and removed")
138	}
139}