remove mock client

This commit is contained in:
Asim Aslam 2020-01-19 17:24:22 +00:00
parent 093bcedfe7
commit a47ff65529
4 changed files with 0 additions and 250 deletions

View File

@ -1,16 +0,0 @@
package mock
import (
"context"
)
type responseKey struct{}
func fromContext(ctx context.Context) (map[string][]MockResponse, bool) {
r, ok := ctx.Value(responseKey{}).(map[string][]MockResponse)
return r, ok
}
func newContext(ctx context.Context, r map[string][]MockResponse) context.Context {
return context.WithValue(ctx, responseKey{}, r)
}

View File

@ -1,150 +0,0 @@
// Package mock provides a mock client for testing
package mock
import (
"context"
"fmt"
"reflect"
"sync"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
)
var (
_ client.Client = NewClient()
)
type MockResponse struct {
Endpoint string
Response interface{}
Error error
}
type MockClient struct {
Client client.Client
Opts client.Options
sync.Mutex
Response map[string][]MockResponse
}
func (m *MockClient) Init(opts ...client.Option) error {
m.Lock()
defer m.Unlock()
for _, opt := range opts {
opt(&m.Opts)
}
r, ok := fromContext(m.Opts.Context)
if !ok {
r = make(map[string][]MockResponse)
}
m.Response = r
return nil
}
func (m *MockClient) Options() client.Options {
return m.Opts
}
func (m *MockClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
return m.Client.NewMessage(topic, msg, opts...)
}
func (m *MockClient) NewRequest(service, endpoint string, req interface{}, reqOpts ...client.RequestOption) client.Request {
return m.Client.NewRequest(service, endpoint, req, reqOpts...)
}
func (m *MockClient) Call(ctx context.Context, req client.Request, rsp interface{}, opts ...client.CallOption) error {
m.Lock()
defer m.Unlock()
response, ok := m.Response[req.Service()]
if !ok {
return errors.NotFound("go.micro.client.mock", "service not found")
}
for _, r := range response {
if r.Endpoint != req.Endpoint() {
continue
}
if r.Error != nil {
return r.Error
}
v := reflect.ValueOf(rsp)
if t := reflect.TypeOf(rsp); t.Kind() == reflect.Ptr {
v = reflect.Indirect(v)
}
response := r.Response
if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func {
var request []reflect.Value
switch t.NumIn() {
case 1:
// one input params: (req)
request = append(request, reflect.ValueOf(req.Body()))
case 2:
// two input params: (ctx, req)
request = append(request, reflect.ValueOf(ctx), reflect.ValueOf(req.Body()))
}
responseValue := reflect.ValueOf(r.Response).Call(request)
response = responseValue[0].Interface()
if len(responseValue) == 2 {
// make it possible to return error in response function
respErr, ok := responseValue[1].Interface().(error)
if ok && respErr != nil {
return respErr
}
}
}
v.Set(reflect.ValueOf(response))
return nil
}
return fmt.Errorf("rpc: can't find service %s", req.Endpoint())
}
func (m *MockClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
m.Lock()
defer m.Unlock()
// TODO: mock stream
return nil, nil
}
func (m *MockClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
return nil
}
func (m *MockClient) String() string {
return "mock"
}
func NewClient(opts ...client.Option) *MockClient {
options := client.Options{
Context: context.TODO(),
}
for _, opt := range opts {
opt(&options)
}
r, ok := fromContext(options.Context)
if !ok {
r = make(map[string][]MockResponse)
}
return &MockClient{
Client: client.DefaultClient,
Opts: options,
Response: r,
}
}

View File

@ -1,67 +0,0 @@
package mock
import (
"context"
"fmt"
"testing"
"github.com/micro/go-micro/errors"
)
func TestClient(t *testing.T) {
type TestResponse struct {
Param string
}
response := []MockResponse{
{Endpoint: "Foo.Bar", Response: map[string]interface{}{"foo": "bar"}},
{Endpoint: "Foo.Struct", Response: &TestResponse{Param: "aparam"}},
{Endpoint: "Foo.Fail", Error: errors.InternalServerError("go.mock", "failed")},
{Endpoint: "Foo.Func", Response: func() string { return "string" }},
{Endpoint: "Foo.FuncStruct", Response: func() *TestResponse { return &TestResponse{Param: "aparam"} }},
{Endpoint: "Foo.FuncWithReqBody", Response: func(req interface{}) string {
if req.(map[string]string)["foo"] == "bar" {
return "string"
}
return "wrong"
}},
{Endpoint: "Foo.FuncWithRequestContextAndResponse", Response: func(ctx context.Context, req interface{}) string {
return "something"
}},
{Endpoint: "Foo.FuncWithRequestContextAndResponseError", Response: func(ctx context.Context, req interface{}) (string, error) {
return "something", fmt.Errorf("mock error")
}},
}
c := NewClient(Response("go.mock", response))
for _, r := range response {
req := c.NewRequest("go.mock", r.Endpoint, map[string]string{"foo": "bar"})
var rsp interface{}
err := c.Call(context.TODO(), req, &rsp)
if err != r.Error {
if r.Endpoint != "Foo.FuncWithRequestContextAndResponseError" {
t.Fatalf("Expecter error %v got %v", r.Error, err)
}
}
t.Log(rsp)
if r.Endpoint == "Foo.FuncWithReqBody" {
req := c.NewRequest("go.mock", r.Endpoint, map[string]string{"foo": "wrong"})
var rsp interface{}
err := c.Call(context.TODO(), req, &rsp)
if err != r.Error {
t.Fatalf("Expecter error %v got %v", r.Error, err)
}
if rsp.(string) != "wrong" {
t.Fatalf("Expecter response 'wrong' got %v", rsp)
}
t.Log(rsp)
}
}
}

View File

@ -1,17 +0,0 @@
package mock
import (
"github.com/micro/go-micro/client"
)
// Response sets the response methods for a service
func Response(service string, response []MockResponse) client.Option {
return func(o *client.Options) {
r, ok := fromContext(o.Context)
if !ok {
r = make(map[string][]MockResponse)
}
r[service] = response
o.Context = newContext(o.Context, r)
}
}