54 lines
1.1 KiB
Go
54 lines
1.1 KiB
Go
package helper
|
|
|
|
import (
|
|
"os"
|
|
"testing"
|
|
"time"
|
|
)
|
|
|
|
func TestGetEnv(t *testing.T) {
|
|
os.Setenv("GO_TEST_VALUE", "123")
|
|
env := GetEnv("GO_TEST_VALUE", "")
|
|
if env != "123" {
|
|
t.Error("GetEnv() cannot find value")
|
|
}
|
|
}
|
|
|
|
func TestGetEnvEmpty(t *testing.T) {
|
|
env := GetEnv("GO_TEST_NOVALUE", "123")
|
|
if env != "123" {
|
|
t.Errorf("GetEnv() did not use default value: want=%s got=%s", "123", env)
|
|
}
|
|
}
|
|
|
|
func TestCacheCreate(t *testing.T) {
|
|
cacheFetch := func(key string) (any, error) {
|
|
return "123", nil
|
|
}
|
|
|
|
cache := NewCache(1*time.Second, cacheFetch)
|
|
if cache.ttl != 1*time.Second {
|
|
t.Error("Error creating cache")
|
|
}
|
|
}
|
|
|
|
func TestCacheFunction(t *testing.T) {
|
|
counter := 1
|
|
cacheFetch := func(key string) (any, error) {
|
|
counter += 1
|
|
return counter, nil
|
|
}
|
|
|
|
cache := NewCache(1*time.Millisecond, cacheFetch)
|
|
valInit, err := cache.Get("TEST")
|
|
valCache, err := cache.Get("TEST")
|
|
time.Sleep(1 * time.Millisecond)
|
|
valNoCache, err := cache.Get("TEST")
|
|
if err != nil {
|
|
t.Errorf("Error getting key from Cache: %e", err)
|
|
}
|
|
if valInit != valCache || valCache != 2 || valNoCache != 3 {
|
|
t.Error("Caching does not resprect ttl.")
|
|
}
|
|
}
|