1
0
mirror of https://github.com/alice-lg/alice-lg.git synced 2024-05-11 05:55:03 +00:00
alice-lg-alice-lg/pkg/caches/routes_test.go

93 lines
1.8 KiB
Go
Raw Normal View History

2018-07-11 14:56:51 +02:00
package caches
import (
2021-04-15 19:06:34 +02:00
"github.com/alice-lg/alice-lg/pkg/api"
2018-07-11 14:56:51 +02:00
"testing"
"time"
)
func TestRoutesCacheSetGet(t *testing.T) {
2018-07-11 15:09:57 +02:00
cache := NewRoutesCache(false, 2)
2018-07-11 14:56:51 +02:00
2018-07-11 15:09:57 +02:00
response := &api.RoutesResponse{
2021-10-15 17:17:11 +02:00
Response: api.Response{
2021-10-27 18:04:35 +00:00
Meta: &api.Meta{
2021-10-15 17:17:11 +02:00
TTL: time.Now().UTC().Add(23 * time.Millisecond),
},
2018-07-11 15:09:57 +02:00
},
}
2021-10-15 17:17:11 +02:00
nID := "neighbor_42"
2018-07-11 15:09:57 +02:00
2021-10-15 17:17:11 +02:00
if cache.Get(nID) != nil {
2018-07-11 15:09:57 +02:00
t.Error("There should not be anything cached yet!")
}
2021-10-15 17:17:11 +02:00
cache.Set(nID, response)
2018-07-11 15:09:57 +02:00
2021-10-15 17:17:11 +02:00
fromCache := cache.Get(nID)
2018-07-11 15:09:57 +02:00
if fromCache != response {
t.Error("Expected", response, "got", fromCache)
}
time.Sleep(33 * time.Millisecond)
2021-10-15 17:17:11 +02:00
fromCache = cache.Get(nID)
2018-07-11 15:09:57 +02:00
if fromCache != nil {
t.Error("Expected empty cache result, got:", fromCache)
}
}
func TestRoutesCacheLru(t *testing.T) {
cache := NewRoutesCache(false, 2)
response := &api.RoutesResponse{
2021-10-15 17:17:11 +02:00
Response: api.Response{
2021-10-27 18:04:35 +00:00
Meta: &api.Meta{
2021-10-15 17:17:11 +02:00
TTL: time.Now().UTC().Add(23 * time.Millisecond),
},
2018-07-11 15:09:57 +02:00
},
}
cache.Set("n1", response)
cache.Set("n2", response)
cache.Set("n3", response)
cache.Set("n2", response)
// n1 should be removed as last used
if len(cache.responses) != 2 {
t.Error("There should not be more than 2 responses. Got:",
len(cache.responses),
)
}
_, ok := cache.responses["n1"]
if ok {
t.Error("n1 should not be part of the key set")
}
// MRU is now n2, LRU: n3, let's access n3 and set n1 again
if cache.accessedAt.LRU() != "n3" {
t.Log("Expected n3 to be LRU")
}
cache.Get("n3")
cache.Set("n1", response)
// n2 should not be part of the key set
_, ok = cache.responses["n1"]
if !ok {
t.Error("n1 should be part of the key set")
}
_, ok = cache.responses["n3"]
if !ok {
t.Error("n3 should be part of the key set")
}
_, ok = cache.responses["n2"]
if !ok {
t.Error("n2 should NOT be part of the key set")
}
2018-07-11 14:56:51 +02:00
}