go-micro.dev/v5@v5.12.0/client/cache_test.go (about) 1 package client 2 3 import ( 4 "context" 5 "testing" 6 "time" 7 8 "go-micro.dev/v5/metadata" 9 "go-micro.dev/v5/transport/headers" 10 ) 11 12 func TestCache(t *testing.T) { 13 ctx := context.TODO() 14 req := NewRequest("go.micro.service.foo", "Foo.Bar", nil) 15 16 t.Run("CacheMiss", func(t *testing.T) { 17 if _, ok := NewCache().Get(ctx, &req); ok { 18 t.Errorf("Expected to get no result from Get") 19 } 20 }) 21 22 t.Run("CacheHit", func(t *testing.T) { 23 c := NewCache() 24 25 rsp := "theresponse" 26 c.Set(ctx, &req, rsp, time.Minute) 27 28 if res, ok := c.Get(ctx, &req); !ok { 29 t.Errorf("Expected a result, got nothing") 30 } else if res != rsp { 31 t.Errorf("Expected '%v' result, got '%v'", rsp, res) 32 } 33 }) 34 } 35 36 func TestCacheKey(t *testing.T) { 37 ctx := context.TODO() 38 req1 := NewRequest("go.micro.service.foo", "Foo.Bar", nil) 39 req2 := NewRequest("go.micro.service.foo", "Foo.Baz", nil) 40 req3 := NewRequest("go.micro.service.foo", "Foo.Baz", "customquery") 41 42 t.Run("IdenticalRequests", func(t *testing.T) { 43 key1 := key(ctx, &req1) 44 key2 := key(ctx, &req1) 45 if key1 != key2 { 46 t.Errorf("Expected the keys to match for identical requests and context") 47 } 48 }) 49 50 t.Run("DifferentRequestEndpoints", func(t *testing.T) { 51 key1 := key(ctx, &req1) 52 key2 := key(ctx, &req2) 53 54 if key1 == key2 { 55 t.Errorf("Expected the keys to differ for different request endpoints") 56 } 57 }) 58 59 t.Run("DifferentRequestBody", func(t *testing.T) { 60 key1 := key(ctx, &req2) 61 key2 := key(ctx, &req3) 62 63 if key1 == key2 { 64 t.Errorf("Expected the keys to differ for different request bodies") 65 } 66 }) 67 68 t.Run("DifferentMetadata", func(t *testing.T) { 69 mdCtx := metadata.Set(context.TODO(), headers.Namespace, "bar") 70 key1 := key(mdCtx, &req1) 71 key2 := key(ctx, &req1) 72 73 if key1 == key2 { 74 t.Errorf("Expected the keys to differ for different metadata") 75 } 76 }) 77 }