2019-11-16 21:48:24 +03:00
|
|
|
package wrapper
|
2016-04-15 18:45:59 +03:00
|
|
|
|
|
|
|
import (
|
2018-03-03 14:53:52 +03:00
|
|
|
"context"
|
2020-05-24 22:26:37 +03:00
|
|
|
"reflect"
|
2016-04-15 18:45:59 +03:00
|
|
|
"testing"
|
2020-05-24 20:05:23 +03:00
|
|
|
"time"
|
2016-04-15 18:45:59 +03:00
|
|
|
|
2020-05-24 20:05:23 +03:00
|
|
|
"github.com/micro/go-micro/v2/client"
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/metadata"
|
2016-04-15 18:45:59 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestWrapper(t *testing.T) {
|
|
|
|
testData := []struct {
|
|
|
|
existing metadata.Metadata
|
|
|
|
headers metadata.Metadata
|
|
|
|
overwrite bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
existing: metadata.Metadata{},
|
|
|
|
headers: metadata.Metadata{
|
2019-12-31 16:45:49 +03:00
|
|
|
"Foo": "bar",
|
2016-04-15 18:45:59 +03:00
|
|
|
},
|
|
|
|
overwrite: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
existing: metadata.Metadata{
|
2019-12-31 16:45:49 +03:00
|
|
|
"Foo": "bar",
|
2016-04-15 18:45:59 +03:00
|
|
|
},
|
|
|
|
headers: metadata.Metadata{
|
2019-12-31 16:45:49 +03:00
|
|
|
"Foo": "baz",
|
2016-04-15 18:45:59 +03:00
|
|
|
},
|
|
|
|
overwrite: false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, d := range testData {
|
2020-04-29 17:37:02 +03:00
|
|
|
c := &fromServiceWrapper{
|
2016-04-15 18:45:59 +03:00
|
|
|
headers: d.headers,
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx := metadata.NewContext(context.Background(), d.existing)
|
2016-11-02 20:21:53 +03:00
|
|
|
ctx = c.setHeaders(ctx)
|
2016-04-15 18:45:59 +03:00
|
|
|
md, _ := metadata.FromContext(ctx)
|
|
|
|
|
|
|
|
for k, v := range d.headers {
|
|
|
|
if d.overwrite && md[k] != v {
|
|
|
|
t.Fatalf("Expected %s=%s got %s=%s", k, v, k, md[k])
|
|
|
|
}
|
|
|
|
if !d.overwrite && md[k] != d.existing[k] {
|
|
|
|
t.Fatalf("Expected %s=%s got %s=%s", k, d.existing[k], k, md[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-05-24 20:05:23 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
type testClient struct {
|
|
|
|
callCount int
|
|
|
|
callRsp interface{}
|
|
|
|
client.Client
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *testClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
|
|
|
|
c.callCount++
|
2020-05-24 22:26:37 +03:00
|
|
|
|
|
|
|
if c.callRsp != nil {
|
|
|
|
val := reflect.ValueOf(rsp).Elem()
|
|
|
|
val.Set(reflect.ValueOf(c.callRsp).Elem())
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:05:23 +03:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-24 22:26:37 +03:00
|
|
|
type testRsp struct {
|
|
|
|
value string
|
2020-05-24 20:05:23 +03:00
|
|
|
}
|
2020-05-24 22:26:37 +03:00
|
|
|
|
2020-05-24 20:05:23 +03:00
|
|
|
func TestCacheWrapper(t *testing.T) {
|
|
|
|
req := client.NewRequest("go.micro.service.foo", "Foo.Bar", nil)
|
|
|
|
|
|
|
|
t.Run("NilCache", func(t *testing.T) {
|
|
|
|
cli := new(testClient)
|
2020-05-24 22:26:37 +03:00
|
|
|
|
|
|
|
w := CacheClient(func() *client.Cache {
|
|
|
|
return nil
|
|
|
|
}, cli)
|
2020-05-24 20:05:23 +03:00
|
|
|
|
|
|
|
// perfroming two requests should increment the call count by two indicating the cache wasn't
|
|
|
|
// used even though the WithCache option was passed.
|
|
|
|
w.Call(context.TODO(), req, nil, client.WithCache(time.Minute))
|
|
|
|
w.Call(context.TODO(), req, nil, client.WithCache(time.Minute))
|
|
|
|
|
|
|
|
if cli.callCount != 2 {
|
|
|
|
t.Errorf("Expected the client to have been called twice")
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("OptionNotSet", func(t *testing.T) {
|
|
|
|
cli := new(testClient)
|
2020-05-24 22:26:37 +03:00
|
|
|
cache := client.NewCache()
|
|
|
|
|
|
|
|
w := CacheClient(func() *client.Cache {
|
|
|
|
return cache
|
|
|
|
}, cli)
|
2020-05-24 20:05:23 +03:00
|
|
|
|
|
|
|
// perfroming two requests should increment the call count by two since we didn't pass the WithCache
|
|
|
|
// option to Call.
|
|
|
|
w.Call(context.TODO(), req, nil)
|
|
|
|
w.Call(context.TODO(), req, nil)
|
|
|
|
|
|
|
|
if cli.callCount != 2 {
|
|
|
|
t.Errorf("Expected the client to have been called twice")
|
|
|
|
}
|
|
|
|
})
|
2016-04-15 18:45:59 +03:00
|
|
|
|
2020-05-24 20:05:23 +03:00
|
|
|
t.Run("OptionSet", func(t *testing.T) {
|
2020-05-24 22:26:37 +03:00
|
|
|
val := "foo"
|
|
|
|
cli := &testClient{callRsp: &testRsp{value: val}}
|
|
|
|
cache := client.NewCache()
|
|
|
|
|
|
|
|
w := CacheClient(func() *client.Cache {
|
|
|
|
return cache
|
|
|
|
}, cli)
|
2020-05-24 20:05:23 +03:00
|
|
|
|
|
|
|
// perfroming two requests should increment the call count by once since the second request should
|
2020-05-24 22:26:37 +03:00
|
|
|
// have used the cache. The correct value should be set on both responses and no errors should
|
|
|
|
// be returned.
|
|
|
|
rsp1 := &testRsp{}
|
|
|
|
rsp2 := &testRsp{}
|
|
|
|
err1 := w.Call(context.TODO(), req, rsp1, client.WithCache(time.Minute))
|
|
|
|
err2 := w.Call(context.TODO(), req, rsp2, client.WithCache(time.Minute))
|
2020-05-24 20:05:23 +03:00
|
|
|
|
|
|
|
if err1 != nil {
|
|
|
|
t.Errorf("Expected nil error, got %v", err1)
|
|
|
|
}
|
|
|
|
if err2 != nil {
|
|
|
|
t.Errorf("Expected nil error, got %v", err2)
|
|
|
|
}
|
2020-05-24 22:26:37 +03:00
|
|
|
|
|
|
|
if rsp1.value != val {
|
|
|
|
t.Errorf("Expected %v to be assigned to the value, got %v", val, rsp1.value)
|
|
|
|
}
|
|
|
|
if rsp2.value != val {
|
|
|
|
t.Errorf("Expected %v to be assigned to the value, got %v", val, rsp2.value)
|
|
|
|
}
|
|
|
|
|
2020-05-24 20:05:23 +03:00
|
|
|
if cli.callCount != 1 {
|
|
|
|
t.Errorf("Expected the client to be called 1 time, was actually called %v time(s)", cli.callCount)
|
|
|
|
}
|
|
|
|
})
|
2016-04-15 18:45:59 +03:00
|
|
|
}
|