|
| 1 | +package frontend |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + "github.com/coder/quartz" |
| 8 | + "github.com/stretchr/testify/require" |
| 9 | +) |
| 10 | + |
| 11 | +func TestTTLCache_Get(t *testing.T) { |
| 12 | + c := NewTTLCache[string, string](time.Minute) |
| 13 | + clock := quartz.NewMock(t) |
| 14 | + c.clock = clock |
| 15 | + // The value should be absent. |
| 16 | + value, ok := c.Get("foo") |
| 17 | + require.Equal(t, "", value) |
| 18 | + require.False(t, ok) |
| 19 | + // Set the value and it should be present. |
| 20 | + c.Set("foo", "bar") |
| 21 | + value, ok = c.Get("foo") |
| 22 | + require.Equal(t, "bar", value) |
| 23 | + require.True(t, ok) |
| 24 | + // Advance the time to be 1 second before the expiration time. |
| 25 | + clock.Advance(59 * time.Second) |
| 26 | + value, ok = c.Get("foo") |
| 27 | + require.Equal(t, "bar", value) |
| 28 | + require.True(t, ok) |
| 29 | + // Advance the time to be equal to the expiration time, the value should |
| 30 | + // be absent. |
| 31 | + clock.Advance(time.Second) |
| 32 | + value, ok = c.Get("foo") |
| 33 | + require.Equal(t, "", value) |
| 34 | + require.False(t, ok) |
| 35 | + // Advance the time past the expiration time, the value should still be |
| 36 | + // absent. |
| 37 | + clock.Advance(time.Second) |
| 38 | + value, ok = c.Get("foo") |
| 39 | + require.Equal(t, "", value) |
| 40 | + require.False(t, ok) |
| 41 | +} |
| 42 | + |
| 43 | +func TestTTLCache_Set(t *testing.T) { |
| 44 | + c := NewTTLCache[string, string](time.Minute) |
| 45 | + clock := quartz.NewMock(t) |
| 46 | + c.clock = clock |
| 47 | + c.Set("foo", "bar") |
| 48 | + item1, ok := c.items["foo"] |
| 49 | + require.True(t, ok) |
| 50 | + require.Equal(t, c.clock.Now().Add(time.Minute), item1.expiresAt) |
| 51 | + // Set should refresh the expiration time. |
| 52 | + clock.Advance(time.Second) |
| 53 | + c.Set("foo", "bar") |
| 54 | + item2, ok := c.items["foo"] |
| 55 | + require.True(t, ok) |
| 56 | + require.Greater(t, item2.expiresAt, item1.expiresAt) |
| 57 | + require.Equal(t, item2.expiresAt, item1.expiresAt.Add(time.Second)) |
| 58 | + // Set should replace the value. |
| 59 | + c.Set("foo", "baz") |
| 60 | + value, ok := c.Get("foo") |
| 61 | + require.True(t, ok) |
| 62 | + require.Equal(t, "baz", value) |
| 63 | +} |
| 64 | + |
| 65 | +func TestTTLCache_Delete(t *testing.T) { |
| 66 | + c := NewTTLCache[string, string](time.Minute) |
| 67 | + clock := quartz.NewMock(t) |
| 68 | + c.clock = clock |
| 69 | + // Set the value and it should be present. |
| 70 | + c.Set("foo", "bar") |
| 71 | + value, ok := c.Get("foo") |
| 72 | + require.True(t, ok) |
| 73 | + require.Equal(t, "bar", value) |
| 74 | + // Delete the value, it should be absent. |
| 75 | + c.Delete("foo") |
| 76 | + value, ok = c.Get("foo") |
| 77 | + require.False(t, ok) |
| 78 | + require.Equal(t, "", value) |
| 79 | +} |
| 80 | + |
| 81 | +func TestTTLCache_Reset(t *testing.T) { |
| 82 | + c := NewTTLCache[string, string](time.Minute) |
| 83 | + clock := quartz.NewMock(t) |
| 84 | + c.clock = clock |
| 85 | + // Set two values, both should be present. |
| 86 | + c.Set("foo", "bar") |
| 87 | + value, ok := c.Get("foo") |
| 88 | + require.True(t, ok) |
| 89 | + require.Equal(t, "bar", value) |
| 90 | + c.Set("bar", "baz") |
| 91 | + value, ok = c.Get("bar") |
| 92 | + require.True(t, ok) |
| 93 | + require.Equal(t, "baz", value) |
| 94 | + // Reset the cache, all should be absent. |
| 95 | + c.Reset() |
| 96 | + value, ok = c.Get("foo") |
| 97 | + require.False(t, ok) |
| 98 | + require.Equal(t, "", value) |
| 99 | + value, ok = c.Get("bar") |
| 100 | + require.False(t, ok) |
| 101 | + require.Equal(t, "", value) |
| 102 | + // Should be able to set values following a reset. |
| 103 | + c.Set("baz", "qux") |
| 104 | + value, ok = c.Get("baz") |
| 105 | + require.True(t, ok) |
| 106 | + require.Equal(t, "qux", value) |
| 107 | +} |
| 108 | + |
| 109 | +func TestTTLCache_RemoveExpiredItems(t *testing.T) { |
| 110 | + c := NewTTLCache[string, string](time.Minute) |
| 111 | + clock := quartz.NewMock(t) |
| 112 | + c.clock = clock |
| 113 | + c.Set("foo", "bar") |
| 114 | + _, ok := c.items["foo"] |
| 115 | + require.True(t, ok) |
| 116 | + // Advance the clock and update foo, it should not be removed. |
| 117 | + clock.Advance(time.Minute) |
| 118 | + c.Set("foo", "bar") |
| 119 | + _, ok = c.items["foo"] |
| 120 | + require.True(t, ok) |
| 121 | + // Advance the clock again but this time set bar, foo should be removed. |
| 122 | + clock.Advance(time.Minute) |
| 123 | + c.Set("bar", "baz") |
| 124 | + _, ok = c.items["foo"] |
| 125 | + require.False(t, ok) |
| 126 | + _, ok = c.items["bar"] |
| 127 | + require.True(t, ok) |
| 128 | +} |
| 129 | + |
| 130 | +func TestNopCache(t *testing.T) { |
| 131 | + c := NewNopCache[string, string]() |
| 132 | + // The value should be absent. |
| 133 | + value, ok := c.Get("foo") |
| 134 | + require.Equal(t, "", value) |
| 135 | + require.False(t, ok) |
| 136 | + // Despite setting the value, it should still be absent. |
| 137 | + c.Set("foo", "bar") |
| 138 | + value, ok = c.Get("foo") |
| 139 | + require.Equal(t, "", value) |
| 140 | + require.False(t, ok) |
| 141 | +} |
0 commit comments