micro/client/mock/mock.go

151 lines
3.1 KiB
Go
Raw Normal View History

2018-12-01 12:56:21 +00:00
// Package mock provides a mock client for testing
2016-04-06 18:33:57 +01:00
package mock
import (
2018-03-03 11:53:52 +00:00
"context"
2016-04-06 18:33:57 +01:00
"fmt"
"reflect"
"sync"
"github.com/micro/go-micro/client"
"github.com/micro/go-micro/errors"
)
var (
_ client.Client = NewClient()
)
type MockResponse struct {
2019-01-10 21:25:31 +00:00
Endpoint string
2016-04-06 18:33:57 +01:00
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
}
2018-05-10 17:39:13 +01:00
func (m *MockClient) NewMessage(topic string, msg interface{}, opts ...client.MessageOption) client.Message {
return m.Client.NewMessage(topic, msg, opts...)
2016-04-06 18:33:57 +01:00
}
2019-01-10 21:25:31 +00:00
func (m *MockClient) NewRequest(service, endpoint string, req interface{}, reqOpts ...client.RequestOption) client.Request {
return m.Client.NewRequest(service, endpoint, req, reqOpts...)
2016-04-06 18:33:57 +01:00
}
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 {
2019-01-10 21:25:31 +00:00
if r.Endpoint != req.Endpoint() {
2016-04-06 18:33:57 +01:00
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)
}
2018-06-13 17:46:30 +02:00
response := r.Response
if t := reflect.TypeOf(r.Response); t.Kind() == reflect.Func {
2019-05-02 18:06:43 +02:00
var request []reflect.Value
switch t.NumIn() {
case 1:
// one input params: (req)
2019-05-02 18:06:43 +02:00
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
}
}
2018-06-13 17:46:30 +02:00
}
2016-04-06 18:33:57 +01:00
2018-06-13 17:46:30 +02:00
v.Set(reflect.ValueOf(response))
2016-04-06 18:33:57 +01:00
return nil
}
2019-01-10 21:25:31 +00:00
return fmt.Errorf("rpc: can't find service %s", req.Endpoint())
2016-04-06 18:33:57 +01:00
}
2018-04-14 18:15:09 +01:00
func (m *MockClient) Stream(ctx context.Context, req client.Request, opts ...client.CallOption) (client.Stream, error) {
2016-04-06 18:33:57 +01:00
m.Lock()
defer m.Unlock()
// TODO: mock stream
return nil, nil
}
2018-04-14 18:06:52 +01:00
func (m *MockClient) Publish(ctx context.Context, p client.Message, opts ...client.PublishOption) error {
2016-04-06 18:33:57 +01:00
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,
}
}