2020-09-29 11:15:29 +03:00
|
|
|
package opentracing_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
opentracing "github.com/opentracing/opentracing-go"
|
|
|
|
"github.com/opentracing/opentracing-go/mocktracer"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-01-16 02:49:58 +03:00
|
|
|
cli "github.com/unistack-org/micro-client-grpc/v3"
|
|
|
|
jsoncodec "github.com/unistack-org/micro-codec-json/v3"
|
2021-01-29 16:47:26 +03:00
|
|
|
rrouter "github.com/unistack-org/micro-router-register/v3"
|
2021-01-16 02:49:58 +03:00
|
|
|
srv "github.com/unistack-org/micro-server-grpc/v3"
|
|
|
|
otwrapper "github.com/unistack-org/micro-wrapper-trace-opentracing/v3"
|
2020-09-29 11:15:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/broker"
|
|
|
|
"github.com/unistack-org/micro/v3/client"
|
|
|
|
"github.com/unistack-org/micro/v3/errors"
|
2021-02-12 20:50:02 +03:00
|
|
|
"github.com/unistack-org/micro/v3/logger"
|
|
|
|
"github.com/unistack-org/micro/v3/register"
|
2020-09-29 11:15:29 +03:00
|
|
|
"github.com/unistack-org/micro/v3/router"
|
|
|
|
"github.com/unistack-org/micro/v3/server"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Test interface {
|
|
|
|
Method(ctx context.Context, in *TestRequest, opts ...client.CallOption) (*TestResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestRequest struct {
|
|
|
|
IsError bool
|
|
|
|
}
|
|
|
|
type TestResponse struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type testHandler struct{}
|
|
|
|
|
|
|
|
func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestResponse) error {
|
|
|
|
if req.IsError {
|
|
|
|
return errors.BadRequest("bad", "test error")
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.Message = "passed"
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
2021-02-12 20:50:02 +03:00
|
|
|
logger.DefaultLogger = logger.NewLogger(logger.WithLevel(logger.ErrorLevel))
|
2020-09-29 11:15:29 +03:00
|
|
|
// setup
|
|
|
|
assert := assert.New(t)
|
|
|
|
for name, tt := range map[string]struct {
|
|
|
|
message string
|
|
|
|
isError bool
|
|
|
|
wantMessage string
|
|
|
|
wantStatus string
|
|
|
|
}{
|
|
|
|
"OK": {
|
|
|
|
message: "passed",
|
|
|
|
isError: false,
|
|
|
|
wantMessage: "passed",
|
|
|
|
wantStatus: "OK",
|
|
|
|
},
|
|
|
|
"Invalid": {
|
|
|
|
message: "",
|
|
|
|
isError: true,
|
|
|
|
wantMessage: "",
|
|
|
|
wantStatus: "InvalidArgument",
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
tracer := mocktracer.New()
|
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
reg := register.NewRegister()
|
|
|
|
brk := broker.NewBroker(broker.Register(reg))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
serverName := "micro.server.name"
|
|
|
|
serverID := "id-1234567890"
|
|
|
|
serverVersion := "1.0.0"
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
rt := rrouter.NewRouter(router.Register(reg))
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
c := cli.NewClient(
|
2020-11-26 08:57:54 +03:00
|
|
|
client.Codec("application/grpc+json", jsoncodec.NewCodec()),
|
|
|
|
client.Codec("application/json", jsoncodec.NewCodec()),
|
2020-09-29 11:15:29 +03:00
|
|
|
client.Router(rt),
|
2021-02-12 20:50:02 +03:00
|
|
|
client.Wrap(otwrapper.NewClientWrapper(otwrapper.WithTracer(tracer))),
|
2020-09-29 11:15:29 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
s := srv.NewServer(
|
2020-11-26 08:57:54 +03:00
|
|
|
server.Codec("application/grpc+json", jsoncodec.NewCodec()),
|
|
|
|
server.Codec("application/json", jsoncodec.NewCodec()),
|
2020-09-29 11:15:29 +03:00
|
|
|
server.Name(serverName),
|
|
|
|
server.Version(serverVersion),
|
|
|
|
server.Id(serverID),
|
2021-01-29 16:47:26 +03:00
|
|
|
server.Register(reg),
|
2020-09-29 11:15:29 +03:00
|
|
|
server.Broker(brk),
|
|
|
|
server.WrapSubscriber(otwrapper.NewServerSubscriberWrapper(otwrapper.WithTracer(tracer))),
|
|
|
|
server.WrapHandler(otwrapper.NewServerHandlerWrapper(otwrapper.WithTracer(tracer))),
|
|
|
|
)
|
|
|
|
if err := s.Init(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
*testHandler
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
if err := s.Handle(s.NewHandler(&Test{new(testHandler)})); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2020-09-29 11:15:29 +03:00
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error starting server: %v", err)
|
|
|
|
}
|
|
|
|
|
2021-02-12 20:50:02 +03:00
|
|
|
ctx, span, err := otwrapper.StartSpanFromOutgoingContext(context.Background(), tracer, "root")
|
2020-09-29 11:15:29 +03:00
|
|
|
assert.NoError(err)
|
|
|
|
|
|
|
|
req := c.NewRequest(serverName, "Test.Method", &TestRequest{IsError: tt.isError}, client.WithContentType("application/json"))
|
|
|
|
rsp := TestResponse{}
|
|
|
|
err = c.Call(ctx, req, &rsp)
|
|
|
|
if tt.isError {
|
|
|
|
assert.Error(err)
|
|
|
|
} else {
|
|
|
|
assert.NoError(err)
|
|
|
|
}
|
|
|
|
assert.Equal(rsp.Message, tt.message)
|
|
|
|
|
|
|
|
span.Finish()
|
|
|
|
|
|
|
|
spans := tracer.FinishedSpans()
|
|
|
|
assert.Len(spans, 3)
|
|
|
|
|
|
|
|
var rootSpan opentracing.Span
|
|
|
|
for _, s := range spans {
|
|
|
|
// order of traces in buffer is not garanteed
|
|
|
|
switch s.OperationName {
|
|
|
|
case "root":
|
|
|
|
rootSpan = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range spans {
|
|
|
|
assert.Equal(rootSpan.Context().(mocktracer.MockSpanContext).TraceID, s.Context().(mocktracer.MockSpanContext).TraceID)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|