aboutsummaryrefslogtreecommitdiff
path: root/decaymap/decaymap_test.go
blob: c1830ed4378257f2adad3677d4d3f971075a9a49 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
package decaymap

import (
	"testing"
	"time"
)

func TestImpl(t *testing.T) {
	dm := New[string, string]()

	dm.Set("test", "hi", 5*time.Minute)

	val, ok := dm.Get("test")
	if !ok {
		t.Error("somehow the test key was not set")
	}

	if val != "hi" {
		t.Errorf("wanted value %q, got: %q", "hi", val)
	}

	ok = dm.expire("test")
	if !ok {
		t.Error("somehow could not force-expire the test key")
	}

	_, ok = dm.Get("test")
	if ok {
		t.Error("got value even though it was supposed to be expired")
	}
}

func TestCleanup(t *testing.T) {
	dm := New[string, string]()

	dm.Set("test1", "hi1", 1*time.Second)
	dm.Set("test2", "hi2", 2*time.Second)
	dm.Set("test3", "hi3", 3*time.Second)

	dm.expire("test1") // Force expire test1
	dm.expire("test2") // Force expire test2

	dm.Cleanup()

	finalLen := dm.Len() // Get the length after cleanup

	if finalLen != 1 { // "test3" should be the only one left
		t.Errorf("Cleanup failed to remove expired entries. Expected length 1, got %d", finalLen)
	}

	if _, ok := dm.Get("test1"); ok { // Verify Get still behaves correctly after Cleanup
		t.Error("test1 should not be found after cleanup")
	}
	if _, ok := dm.Get("test2"); ok {
		t.Error("test2 should not be found after cleanup")
	}
	if val, ok := dm.Get("test3"); !ok || val != "hi3" {
		t.Error("test3 should still be found after cleanup")
	}
}