fixup handler tests

This commit is contained in:
Asim Aslam 2020-04-12 14:29:38 +01:00
parent d03a02f2e4
commit b08c636b44
5 changed files with 28 additions and 53 deletions

View File

@ -9,20 +9,16 @@ import (
"testing" "testing"
"time" "time"
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/api" "github.com/micro/go-micro/v2/api"
ahandler "github.com/micro/go-micro/v2/api/handler" "github.com/micro/go-micro/v2/api/handler"
apirpc "github.com/micro/go-micro/v2/api/handler/rpc" "github.com/micro/go-micro/v2/api/handler/rpc"
"github.com/micro/go-micro/v2/api/router" "github.com/micro/go-micro/v2/api/router"
rstatic "github.com/micro/go-micro/v2/api/router/static" rstatic "github.com/micro/go-micro/v2/api/router/static"
bmemory "github.com/micro/go-micro/v2/broker/memory"
"github.com/micro/go-micro/v2/client" "github.com/micro/go-micro/v2/client"
gcli "github.com/micro/go-micro/v2/client/grpc" gcli "github.com/micro/go-micro/v2/client/grpc"
rmemory "github.com/micro/go-micro/v2/registry/memory" rmemory "github.com/micro/go-micro/v2/registry/memory"
"github.com/micro/go-micro/v2/server" "github.com/micro/go-micro/v2/server"
gsrv "github.com/micro/go-micro/v2/server/grpc" gsrv "github.com/micro/go-micro/v2/server/grpc"
tgrpc "github.com/micro/go-micro/v2/transport/grpc"
pb "github.com/micro/go-micro/v2/server/grpc/proto" pb "github.com/micro/go-micro/v2/server/grpc/proto"
) )
@ -39,49 +35,33 @@ func (s *testServer) Call(ctx context.Context, req *pb.Request, rsp *pb.Response
func TestApiAndGRPC(t *testing.T) { func TestApiAndGRPC(t *testing.T) {
r := rmemory.NewRegistry() r := rmemory.NewRegistry()
b := bmemory.NewBroker()
tr := tgrpc.NewTransport() // create a new client
s := gsrv.NewServer( s := gsrv.NewServer(
server.Broker(b),
server.Name("foo"), server.Name("foo"),
server.Registry(r), server.Registry(r),
server.Transport(tr),
) )
// create a new server
c := gcli.NewClient( c := gcli.NewClient(
client.Registry(r), client.Registry(r),
client.Broker(b),
client.Transport(tr),
) )
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
defer cancel()
svc := micro.NewService(
micro.Server(s),
micro.Client(c),
micro.Broker(b),
micro.Registry(r),
micro.Transport(tr),
micro.Context(ctx))
h := &testServer{} h := &testServer{}
pb.RegisterTestHandler(s, h) pb.RegisterTestHandler(s, h)
go func() { if err := s.Start(); err != nil {
if err := svc.Run(); err != nil { t.Fatalf("failed to start: %v", err)
t.Fatalf("failed to start: %v", err)
}
}()
time.Sleep(1 * time.Second)
// check registration
services, err := r.GetService("foo")
if err != nil || len(services) == 0 {
t.Fatalf("failed to get service: %v # %d", err, len(services))
} }
defer s.Stop()
// create a new router
router := rstatic.NewRouter( router := rstatic.NewRouter(
router.WithHandler(apirpc.Handler), router.WithHandler(rpc.Handler),
router.WithRegistry(svc.Server().Options().Registry), router.WithRegistry(r),
) )
err = router.Register(&api.Endpoint{
err := router.Register(&api.Endpoint{
Name: "foo.Test.Call", Name: "foo.Test.Call",
Method: []string{"GET"}, Method: []string{"GET"},
Path: []string{"/api/v0/test/call/{name}"}, Path: []string{"/api/v0/test/call/{name}"},
@ -91,9 +71,9 @@ func TestApiAndGRPC(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
hrpc := apirpc.NewHandler( hrpc := rpc.NewHandler(
ahandler.WithService(svc), handler.WithClient(c),
ahandler.WithRouter(router), handler.WithRouter(router),
) )
hsrv := &http.Server{ hsrv := &http.Server{
@ -115,6 +95,7 @@ func TestApiAndGRPC(t *testing.T) {
t.Fatalf("Failed to created http.Request: %v", err) t.Fatalf("Failed to created http.Request: %v", err)
} }
defer rsp.Body.Close() defer rsp.Body.Close()
buf, err := ioutil.ReadAll(rsp.Body) buf, err := ioutil.ReadAll(rsp.Body)
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -124,9 +105,4 @@ func TestApiAndGRPC(t *testing.T) {
if string(buf) != jsonMsg { if string(buf) != jsonMsg {
t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg) t.Fatalf("invalid message received, parsing error %s != %s", buf, jsonMsg)
} }
select {
case <-ctx.Done():
return
}
} }

View File

@ -65,7 +65,7 @@ func (a *apiHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// create request and response // create request and response
c := a.opts.Service.Client() c := a.opts.Client
req := c.NewRequest(service.Name, service.Endpoint.Name, request) req := c.NewRequest(service.Name, service.Endpoint.Name, request)
rsp := &api.Response{} rsp := &api.Response{}

View File

@ -118,7 +118,7 @@ func (e *event) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// get client // get client
c := e.opts.Service.Client() c := e.opts.Client
// create publication // create publication
p := c.NewMessage(topic, ev) p := c.NewMessage(topic, ev)

View File

@ -1,8 +1,9 @@
package handler package handler
import ( import (
"github.com/micro/go-micro/v2"
"github.com/micro/go-micro/v2/api/router" "github.com/micro/go-micro/v2/api/router"
"github.com/micro/go-micro/v2/client"
"github.com/micro/go-micro/v2/client/grpc"
) )
var ( var (
@ -13,7 +14,7 @@ type Options struct {
MaxRecvSize int64 MaxRecvSize int64
Namespace string Namespace string
Router router.Router Router router.Router
Service micro.Service Client client.Client
} }
type Option func(o *Options) type Option func(o *Options)
@ -25,9 +26,8 @@ func NewOptions(opts ...Option) Options {
o(&options) o(&options)
} }
// create service if its blank if options.Client == nil {
if options.Service == nil { WithClient(grpc.NewClient())(&options)
WithService(micro.NewService())(&options)
} }
// set namespace if blank // set namespace if blank
@ -56,10 +56,9 @@ func WithRouter(r router.Router) Option {
} }
} }
// WithService specifies a micro.Service func WithClient(c client.Client) Option {
func WithService(s micro.Service) Option {
return func(o *Options) { return func(o *Options) {
o.Service = s o.Client = c
} }
} }

View File

@ -113,7 +113,7 @@ func (h *rpcHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
} }
// micro client // micro client
c := h.opts.Service.Client() c := h.opts.Client
// create context // create context
cx := ctx.FromRequest(r) cx := ctx.FromRequest(r)