...
1 package utils_test
2
3 import (
4 "testing"
5
6 "github.com/okta/okta-jwt-verifier-golang/utils"
7 )
8
9 type Value struct {
10 key string
11 }
12
13 func TestNewDefaultCache(t *testing.T) {
14 lookup := func(key string) (interface{}, error) {
15 return &Value{key: key}, nil
16 }
17
18 cache, err := utils.NewDefaultCache(lookup)
19 if err != nil {
20 t.Fatalf("unexpected error: %v", err)
21 }
22
23 first, firstErr := cache.Get("first")
24 if firstErr != nil {
25 t.Fatalf("Expected no error, got %v", firstErr)
26 }
27 if _, ok := first.(*Value); !ok {
28 t.Error("Expected first to be a *Value")
29 }
30
31 second, secondErr := cache.Get("second")
32 if secondErr != nil {
33 t.Fatalf("Expected no error, got %v", secondErr)
34 }
35 if _, ok := second.(*Value); !ok {
36 t.Error("Expected second to be a *Value")
37 }
38
39 if first == second {
40 t.Error("Expected first and second to be different")
41 }
42
43 firstAgain, firstAgainErr := cache.Get("first")
44 if firstAgainErr != nil {
45 t.Fatalf("Expected no error, got %v", firstAgainErr)
46 }
47 if first != firstAgain {
48 t.Error("Expected cached value to be the same")
49 }
50 }
51
View as plain text