Skip to content

Commit cd66151

Browse files
committed
more tests and minor changes
1 parent 4ebfcdf commit cd66151

File tree

4 files changed

+67
-9
lines changed

4 files changed

+67
-9
lines changed

pkg/storage/memory-lfu.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ func (lfu *LFUCache) LookUp(ctx context.Context, key string) (*model.Response, e
116116
lfu.update(val)
117117
node := val.Value.(*LfuNode)
118118
response := node.value
119-
if (time.Now().Unix() - node.timeStamp) > lfu.staleDuration {
120-
response.StaleValue = 0
119+
if (time.Now().Unix() - node.timeStamp) >= lfu.staleDuration {
120+
response.StaleValue = model.StaleValue
121121
} else {
122-
response.StaleValue = 1
122+
response.StaleValue = model.FreshValue
123123
}
124124

125125
proc <- response

pkg/storage/memory-lfu_test.go

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package storage
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/assert"
89
"neurocode.io/cache-offloader/pkg/model"
@@ -172,14 +173,42 @@ func TestLFUCacheCommandExeeeded(t *testing.T) {
172173

173174
func TestLFUStaleStatus(t *testing.T) {
174175
oneMegaByte := 1000000.0 / 1024 / 1024
175-
lfu := NewLRUCache(oneMegaByte, 0)
176+
lfu := NewLFUCache(oneMegaByte, 1)
176177
ctx := context.Background()
177178

178179
lfu.Store(ctx, "1", &model.Response{
179180
Status: 100,
180181
Body: []byte{1, 2, 3},
181182
})
182183

184+
resp, _ := lfu.LookUp(ctx, "1")
185+
assert.False(t, resp.IsStale())
186+
187+
time.Sleep(1 * time.Second)
188+
resp, _ = lfu.LookUp(ctx, "1")
189+
assert.True(t, resp.IsStale())
190+
}
191+
192+
func TestLFUStaleStatus2(t *testing.T) {
193+
oneMegaByte := 1000000.0 / 1024 / 1024
194+
lfu := NewLFUCache(oneMegaByte, 1)
195+
ctx := context.Background()
196+
197+
lfu.Store(ctx, "1", &model.Response{
198+
Status: 100,
199+
Body: []byte{1, 2, 3},
200+
})
201+
202+
time.Sleep(1 * time.Second)
203+
183204
resp, _ := lfu.LookUp(ctx, "1")
184205
assert.True(t, resp.IsStale())
206+
207+
lfu.Store(ctx, "1", &model.Response{
208+
Status: 100,
209+
Body: []byte{1, 2, 3},
210+
})
211+
212+
resp, _ = lfu.LookUp(ctx, "1")
213+
assert.False(t, resp.IsStale())
185214
}

pkg/storage/memory-lru.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ func (lru *LRUCache) LookUp(ctx context.Context, key string) (*model.Response, e
9494
node := value.Value.(*LRUNode)
9595
response := node.value
9696
if (time.Now().Unix() - node.timeStamp) >= lru.staleDuration {
97-
response.StaleValue = 0
97+
response.StaleValue = model.StaleValue
9898
} else {
99-
response.StaleValue = 1
99+
response.StaleValue = model.FreshValue
100100
}
101101

102102
proc <- response

pkg/storage/memory-lru_test.go

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package storage
33
import (
44
"context"
55
"testing"
6+
"time"
67

78
"github.com/stretchr/testify/assert"
89
"neurocode.io/cache-offloader/pkg/model"
@@ -193,14 +194,42 @@ func TestLRUCacheCommandExeeeded(t *testing.T) {
193194

194195
func TestLRUStaleStatus(t *testing.T) {
195196
oneMegaByte := 1000000.0 / 1024 / 1024
196-
lru := NewLRUCache(oneMegaByte, 0)
197+
lfu := NewLRUCache(oneMegaByte, 1)
197198
ctx := context.Background()
198199

199-
lru.Store(ctx, "1", &model.Response{
200+
lfu.Store(ctx, "1", &model.Response{
200201
Status: 100,
201202
Body: []byte{1, 2, 3},
202203
})
203204

204-
resp, _ := lru.LookUp(ctx, "1")
205+
resp, _ := lfu.LookUp(ctx, "1")
206+
assert.False(t, resp.IsStale())
207+
208+
time.Sleep(1 * time.Second)
209+
resp, _ = lfu.LookUp(ctx, "1")
205210
assert.True(t, resp.IsStale())
206211
}
212+
213+
func TestLRUStaleStatus2(t *testing.T) {
214+
oneMegaByte := 1000000.0 / 1024 / 1024
215+
lfu := NewLRUCache(oneMegaByte, 1)
216+
ctx := context.Background()
217+
218+
lfu.Store(ctx, "1", &model.Response{
219+
Status: 100,
220+
Body: []byte{1, 2, 3},
221+
})
222+
223+
time.Sleep(1 * time.Second)
224+
225+
resp, _ := lfu.LookUp(ctx, "1")
226+
assert.True(t, resp.IsStale())
227+
228+
lfu.Store(ctx, "1", &model.Response{
229+
Status: 100,
230+
Body: []byte{1, 2, 3},
231+
})
232+
233+
resp, _ = lfu.LookUp(ctx, "1")
234+
assert.False(t, resp.IsStale())
235+
}

0 commit comments

Comments
 (0)