2021-10-27 01:24:35 +03:00
|
|
|
//go:build ignore
|
2020-10-30 23:27:33 +03:00
|
|
|
// +build ignore
|
|
|
|
|
|
|
|
package datadog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
2021-10-27 01:24:35 +03:00
|
|
|
rrouter "go.unistack.org/micro-router-register/v3"
|
|
|
|
"go.unistack.org/micro/register/memory"
|
|
|
|
"go.unistack.org/micro/v3/client"
|
|
|
|
microerr "go.unistack.org/micro/v3/errors"
|
|
|
|
"go.unistack.org/micro/v3/router"
|
|
|
|
"go.unistack.org/micro/v3/server"
|
2020-10-30 23:27:33 +03:00
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/ext"
|
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/mocktracer"
|
|
|
|
"gopkg.in/DataDog/dd-trace-go.v1/ddtrace/tracer"
|
|
|
|
|
2021-10-27 01:24:35 +03:00
|
|
|
cli "go.unistack.org/micro/v3/client"
|
|
|
|
srv "go.unistack.org/micro/v3/server"
|
2020-10-30 23:27:33 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
type Test interface {
|
|
|
|
Method(ctx context.Context, in *TestRequest, opts ...client.CallOption) (*TestResponse, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
type TestRequest struct {
|
|
|
|
IsError bool
|
|
|
|
}
|
2021-10-27 01:24:35 +03:00
|
|
|
|
2020-10-30 23:27:33 +03:00
|
|
|
type TestResponse struct {
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
type testHandler struct{}
|
|
|
|
|
|
|
|
func (t *testHandler) Method(ctx context.Context, req *TestRequest, rsp *TestResponse) error {
|
|
|
|
if req.IsError {
|
|
|
|
return microerr.BadRequest("bad", "test error")
|
|
|
|
}
|
|
|
|
|
|
|
|
rsp.Message = "passed"
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestClient(t *testing.T) {
|
|
|
|
// 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) {
|
|
|
|
mt := mocktracer.Start()
|
|
|
|
defer mt.Stop()
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
register := memory.NewRegister()
|
2020-10-30 23:27:33 +03:00
|
|
|
|
|
|
|
serverName := "micro.server.name"
|
|
|
|
serverID := "id-1234567890"
|
|
|
|
serverVersion := "1.0.0"
|
|
|
|
|
|
|
|
c := cli.NewClient(
|
2021-01-29 16:47:26 +03:00
|
|
|
client.Router(rrouter.NewRouter(router.Register(register))),
|
2020-10-30 23:27:33 +03:00
|
|
|
client.WrapCall(NewCallWrapper()),
|
|
|
|
)
|
|
|
|
|
|
|
|
s := srv.NewServer(
|
|
|
|
server.Name(serverName),
|
|
|
|
server.Version(serverVersion),
|
|
|
|
server.Id(serverID),
|
2021-01-29 16:47:26 +03:00
|
|
|
server.Register(register),
|
2020-10-30 23:27:33 +03:00
|
|
|
server.WrapSubscriber(NewSubscriberWrapper()),
|
|
|
|
server.WrapHandler(NewHandlerWrapper()),
|
|
|
|
)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
*testHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Handle(s.NewHandler(&Test{new(testHandler)}))
|
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error starting server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
span, ctx := StartSpanFromContext(context.Background(), "root", tracer.ServiceName("root"), tracer.ResourceName("root"))
|
|
|
|
|
|
|
|
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 := mt.FinishedSpans()
|
|
|
|
assert.Len(spans, 3)
|
|
|
|
|
|
|
|
var serverSpan, clientSpan, rootSpan mocktracer.Span
|
|
|
|
for _, s := range spans {
|
|
|
|
// order of traces in buffer is not garanteed
|
|
|
|
switch s.OperationName() {
|
|
|
|
case "micro.server":
|
|
|
|
serverSpan = s
|
|
|
|
case "micro.client":
|
|
|
|
clientSpan = s
|
|
|
|
case "root":
|
|
|
|
rootSpan = s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
assert.NotNil(serverSpan)
|
|
|
|
assert.NotNil(clientSpan)
|
|
|
|
assert.NotNil(rootSpan)
|
|
|
|
|
|
|
|
assert.Equal(rootSpan.TraceID(), clientSpan.TraceID())
|
|
|
|
assert.Equal(serverSpan.Tag(tagStatus), tt.wantStatus)
|
|
|
|
assert.Equal("Test.Method", serverSpan.Tag(ext.ResourceName))
|
|
|
|
assert.Equal(rootSpan.TraceID(), serverSpan.TraceID())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRace(t *testing.T) {
|
|
|
|
// setup
|
|
|
|
assert := assert.New(t)
|
|
|
|
|
|
|
|
mt := mocktracer.Start()
|
|
|
|
defer mt.Stop()
|
|
|
|
|
2021-01-29 16:47:26 +03:00
|
|
|
register := memory.NewRegister()
|
2020-10-30 23:27:33 +03:00
|
|
|
|
|
|
|
serverName := "micro.server.name"
|
|
|
|
serverID := "id-1234567890"
|
|
|
|
serverVersion := "1.0.0"
|
|
|
|
|
|
|
|
c := cli.NewClient(
|
2021-01-29 16:47:26 +03:00
|
|
|
client.Router(rrouter.NewRouter(router.Register(register))),
|
2020-10-30 23:27:33 +03:00
|
|
|
client.WrapCall(NewCallWrapper()),
|
|
|
|
)
|
|
|
|
|
|
|
|
s := srv.NewServer(
|
|
|
|
server.Name(serverName),
|
|
|
|
server.Version(serverVersion),
|
|
|
|
server.Id(serverID),
|
2021-01-29 16:47:26 +03:00
|
|
|
server.Register(register),
|
2020-10-30 23:27:33 +03:00
|
|
|
server.WrapSubscriber(NewSubscriberWrapper()),
|
|
|
|
server.WrapHandler(NewHandlerWrapper()),
|
|
|
|
)
|
|
|
|
defer s.Stop()
|
|
|
|
|
|
|
|
type Test struct {
|
|
|
|
*testHandler
|
|
|
|
}
|
|
|
|
|
|
|
|
s.Handle(s.NewHandler(&Test{new(testHandler)}))
|
|
|
|
|
|
|
|
if err := s.Start(); err != nil {
|
|
|
|
t.Fatalf("Unexpected error starting server: %v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
span, ctx := StartSpanFromContext(context.Background(), "root", tracer.ServiceName("root"), tracer.ResourceName("root"))
|
|
|
|
|
|
|
|
num := 100
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(num)
|
|
|
|
for i := 0; i < num; i++ {
|
|
|
|
func() {
|
|
|
|
go func(i int) {
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
req := c.NewRequest(serverName, "Test.Method", &TestRequest{IsError: false}, client.WithContentType("application/json"))
|
|
|
|
rsp := TestResponse{}
|
|
|
|
err := c.Call(ctx, req, &rsp)
|
|
|
|
assert.NoError(err)
|
|
|
|
}(i)
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
span.Finish()
|
|
|
|
spans := mt.FinishedSpans()
|
|
|
|
assert.Len(spans, (num*2)+1)
|
|
|
|
}
|