mirror of
https://github.com/alice-lg/birdwatcher.git
synced 2025-03-09 00:00:05 +01:00
Add test for memory cache backend
Improve error handling in case value can not be retrieved. Either return the value and nil, or a value and an error.
This commit is contained in:
parent
3e0bfbb0f5
commit
75380807de
3 changed files with 83 additions and 10 deletions
11
bird/bird.go
11
bird/bird.go
|
@ -68,14 +68,13 @@ func toCache(key string, val Parsed) bool {
|
|||
*/
|
||||
func fromCache(key string) (Parsed, bool) {
|
||||
val, err := cache.Get(key)
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return val, false
|
||||
} else if IsSpecial(val) { // cache may return NilParse e.g. if ttl is expired
|
||||
return val, false
|
||||
} else {
|
||||
if err == nil {
|
||||
return val, true
|
||||
} else {
|
||||
return val, false
|
||||
}
|
||||
//DEBUG log.Println(err)
|
||||
|
||||
}
|
||||
|
||||
// Determines the key in the cache, where the result of specific functions are stored.
|
||||
|
|
|
@ -23,17 +23,17 @@ func (c *MemoryCache) Get(key string) (Parsed, error) {
|
|||
c.RLock()
|
||||
val, ok := c.m[key]
|
||||
c.RUnlock()
|
||||
if !ok {
|
||||
return NilParse, errors.New("Could not retrive key" + key + "from MemoryCache.")
|
||||
if !ok { // cache miss
|
||||
return NilParse, errors.New("Failed to retrive key '" + key + "' from MemoryCache.")
|
||||
}
|
||||
|
||||
ttl, correct := val["ttl"].(time.Time)
|
||||
if !correct {
|
||||
return NilParse, errors.New("Invalid TTL value for key" + key)
|
||||
return NilParse, errors.New("Invalid TTL value for key '" + key + "'")
|
||||
}
|
||||
|
||||
if ttl.Before(time.Now()) {
|
||||
return NilParse, nil // TTL expired
|
||||
return val, errors.New("TTL expired for key '" + key + "'") // TTL expired
|
||||
} else {
|
||||
return val, nil // cache hit
|
||||
}
|
||||
|
|
74
bird/memory_cache_test.go
Normal file
74
bird/memory_cache_test.go
Normal file
|
@ -0,0 +1,74 @@
|
|||
package bird
|
||||
|
||||
import (
|
||||
"testing"
|
||||
)
|
||||
|
||||
func Test_MemoryCacheAccess(t *testing.T) {
|
||||
|
||||
cache, err := NewMemoryCache()
|
||||
|
||||
parsed := Parsed{
|
||||
"foo": 23,
|
||||
"bar": 42,
|
||||
"baz": true,
|
||||
}
|
||||
|
||||
t.Log("Setting memory cache...")
|
||||
err = cache.Set("testkey", parsed, 5)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log("Fetching from memory cache...")
|
||||
parsed, err = cache.Get("testkey")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
t.Log(parsed)
|
||||
}
|
||||
|
||||
func Test_MemoryCacheAccessKeyMissing(t *testing.T) {
|
||||
|
||||
cache, err := NewMemoryCache()
|
||||
|
||||
parsed, err := cache.Get("test_missing_key")
|
||||
if !IsSpecial(parsed) {
|
||||
t.Error(err)
|
||||
}
|
||||
t.Log("Cache error:", err)
|
||||
t.Log(parsed)
|
||||
}
|
||||
|
||||
func Test_MemoryCacheRoutes(t *testing.T) {
|
||||
f, err := openFile("routes_bird1_ipv4.sample")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
defer f.Close()
|
||||
|
||||
parsed := parseRoutes(f)
|
||||
_, ok := parsed["routes"].([]Parsed)
|
||||
if !ok {
|
||||
t.Fatal("Error getting routes")
|
||||
}
|
||||
|
||||
cache, err := NewMemoryCache()
|
||||
|
||||
err = cache.Set("routes_protocol_test", parsed, 5)
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
}
|
||||
|
||||
parsed, err = cache.Get("routes_protocol_test")
|
||||
if err != nil {
|
||||
t.Error(err)
|
||||
return
|
||||
}
|
||||
routes, ok := parsed["routes"].([]Parsed)
|
||||
if !ok {
|
||||
t.Error("Error getting routes")
|
||||
}
|
||||
t.Log("Retrieved routes:", len(routes))
|
||||
}
|
Loading…
Add table
Reference in a new issue