MCPcopy Create free account
hub / github.com/chainloop-dev/chainloop / TestMemoryCache_GetSetDelete

Function TestMemoryCache_GetSetDelete

pkg/cache/memory_test.go:27–95  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25)
26
27func TestMemoryCache_GetSetDelete(t *testing.T) {
28 tests := []struct {
29 name string
30 run func(t *testing.T, c Cache[string])
31 }{
32 {
33 name: "get from empty cache returns miss",
34 run: func(t *testing.T, c Cache[string]) {
35 _, ok, err := c.Get(context.Background(), "missing")
36 require.NoError(t, err)
37 assert.False(t, ok)
38 },
39 },
40 {
41 name: "set then get returns value",
42 run: func(t *testing.T, c Cache[string]) {
43 ctx := context.Background()
44 require.NoError(t, c.Set(ctx, "key1", "value1"))
45 val, ok, err := c.Get(ctx, "key1")
46 require.NoError(t, err)
47 assert.True(t, ok)
48 assert.Equal(t, "value1", val)
49 },
50 },
51 {
52 name: "delete removes entry",
53 run: func(t *testing.T, c Cache[string]) {
54 ctx := context.Background()
55 require.NoError(t, c.Set(ctx, "key1", "value1"))
56 require.NoError(t, c.Delete(ctx, "key1"))
57 _, ok, err := c.Get(ctx, "key1")
58 require.NoError(t, err)
59 assert.False(t, ok)
60 },
61 },
62 {
63 name: "purge removes all entries",
64 run: func(t *testing.T, c Cache[string]) {
65 ctx := context.Background()
66 require.NoError(t, c.Set(ctx, "k1", "v1"))
67 require.NoError(t, c.Set(ctx, "k2", "v2"))
68 require.NoError(t, c.Purge(ctx))
69 _, ok1, _ := c.Get(ctx, "k1")
70 _, ok2, _ := c.Get(ctx, "k2")
71 assert.False(t, ok1)
72 assert.False(t, ok2)
73 },
74 },
75 {
76 name: "TTL expiration",
77 run: func(t *testing.T, c Cache[string]) {
78 ctx := context.Background()
79 require.NoError(t, c.Set(ctx, "ephemeral", "gone-soon"))
80 time.Sleep(100 * time.Millisecond)
81 _, ok, err := c.Get(ctx, "ephemeral")
82 require.NoError(t, err)
83 assert.False(t, ok)
84 },

Callers

nothing calls this directly

Calls 6

WithTTLFunction · 0.85
GetMethod · 0.65
SetMethod · 0.65
DeleteMethod · 0.65
PurgeMethod · 0.65
RunMethod · 0.45

Tested by

no test coverage detected