mirror of
https://github.com/zeromicro/go-zero.git
synced 2025-02-03 00:38:40 +08:00
test: add more tests (#1147)
* test: add more tests * test: add more tests
This commit is contained in:
parent
d41163f5c1
commit
eab77e21dd
@ -34,7 +34,7 @@ var (
|
|||||||
RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED")
|
RPCMessageTypeReceived = RPCMessageTypeKey.String("RECEIVED")
|
||||||
)
|
)
|
||||||
|
|
||||||
// StatusCodeAttr returns a attribute.KeyValue that represents the give c.
|
// StatusCodeAttr returns an attribute.KeyValue that represents the give c.
|
||||||
func StatusCodeAttr(c gcodes.Code) attribute.KeyValue {
|
func StatusCodeAttr(c gcodes.Code) attribute.KeyValue {
|
||||||
return GRPCStatusCodeKey.Int64(int64(c))
|
return GRPCStatusCodeKey.Int64(int64(c))
|
||||||
}
|
}
|
||||||
|
12
core/trace/attributes_test.go
Normal file
12
core/trace/attributes_test.go
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package trace
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
gcodes "google.golang.org/grpc/codes"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestStatusCodeAttr(t *testing.T) {
|
||||||
|
assert.Equal(t, GRPCStatusCodeKey.Int(int(gcodes.DataLoss)), StatusCodeAttr(gcodes.DataLoss))
|
||||||
|
}
|
@ -157,7 +157,8 @@ func TestExtractValidTraceContext(t *testing.T) {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||||
|
propagation.TraceContext{}, propagation.Baggage{}))
|
||||||
propagator := otel.GetTextMapPropagator()
|
propagator := otel.GetTextMapPropagator()
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -242,7 +243,8 @@ func TestExtractInvalidTraceContext(t *testing.T) {
|
|||||||
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-",
|
header: "00-4bf92f3577b34da6a3ce929d0e0e4736-00f067aa0ba902b7-",
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||||
|
propagation.TraceContext{}, propagation.Baggage{}))
|
||||||
propagator := otel.GetTextMapPropagator()
|
propagator := otel.GetTextMapPropagator()
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -308,7 +310,8 @@ func TestInjectValidTraceContext(t *testing.T) {
|
|||||||
}),
|
}),
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||||
|
propagation.TraceContext{}, propagation.Baggage{}))
|
||||||
propagator := otel.GetTextMapPropagator()
|
propagator := otel.GetTextMapPropagator()
|
||||||
|
|
||||||
for _, tt := range tests {
|
for _, tt := range tests {
|
||||||
@ -325,6 +328,11 @@ func TestInjectValidTraceContext(t *testing.T) {
|
|||||||
md := metadata.MD{}
|
md := metadata.MD{}
|
||||||
Inject(ctx, propagator, &md)
|
Inject(ctx, propagator, &md)
|
||||||
assert.Equal(t, want, md)
|
assert.Equal(t, want, md)
|
||||||
|
|
||||||
|
mm := &metadataSupplier{
|
||||||
|
metadata: &md,
|
||||||
|
}
|
||||||
|
assert.NotEmpty(t, mm.Keys())
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -334,7 +342,8 @@ func TestInvalidSpanContextDropped(t *testing.T) {
|
|||||||
require.False(t, invalidSC.IsValid())
|
require.False(t, invalidSC.IsValid())
|
||||||
ctx := trace.ContextWithRemoteSpanContext(context.Background(), invalidSC)
|
ctx := trace.ContextWithRemoteSpanContext(context.Background(), invalidSC)
|
||||||
|
|
||||||
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(propagation.TraceContext{}, propagation.Baggage{}))
|
otel.SetTextMapPropagator(propagation.NewCompositeTextMapPropagator(
|
||||||
|
propagation.TraceContext{}, propagation.Baggage{}))
|
||||||
propagator := otel.GetTextMapPropagator()
|
propagator := otel.GetTextMapPropagator()
|
||||||
|
|
||||||
md := metadata.MD{}
|
md := metadata.MD{}
|
||||||
@ -342,5 +351,6 @@ func TestInvalidSpanContextDropped(t *testing.T) {
|
|||||||
mm := &metadataSupplier{
|
mm := &metadataSupplier{
|
||||||
metadata: &md,
|
metadata: &md,
|
||||||
}
|
}
|
||||||
|
assert.Empty(t, mm.Keys())
|
||||||
assert.Equal(t, "", mm.Get("traceparent"), "injected invalid SpanContext")
|
assert.Equal(t, "", mm.Get("traceparent"), "injected invalid SpanContext")
|
||||||
}
|
}
|
||||||
|
@ -15,7 +15,7 @@ const localhost = "127.0.0.1"
|
|||||||
// PeerFromCtx returns the peer from ctx.
|
// PeerFromCtx returns the peer from ctx.
|
||||||
func PeerFromCtx(ctx context.Context) string {
|
func PeerFromCtx(ctx context.Context) string {
|
||||||
p, ok := peer.FromContext(ctx)
|
p, ok := peer.FromContext(ctx)
|
||||||
if !ok {
|
if !ok || p == nil {
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +55,7 @@ func ParseFullMethod(fullMethod string) (string, []attribute.KeyValue) {
|
|||||||
func PeerAttr(addr string) []attribute.KeyValue {
|
func PeerAttr(addr string) []attribute.KeyValue {
|
||||||
host, port, err := net.SplitHostPort(addr)
|
host, port, err := net.SplitHostPort(addr)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return []attribute.KeyValue(nil)
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(host) == 0 {
|
if len(host) == 0 {
|
||||||
|
@ -1,13 +1,53 @@
|
|||||||
package trace
|
package trace
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"context"
|
||||||
|
"net"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"go.opentelemetry.io/otel/attribute"
|
"go.opentelemetry.io/otel/attribute"
|
||||||
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
||||||
|
"google.golang.org/grpc/peer"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
func TestPeerFromContext(t *testing.T) {
|
||||||
|
addrs, err := net.InterfaceAddrs()
|
||||||
|
assert.Nil(t, err)
|
||||||
|
assert.NotEmpty(t, addrs)
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
ctx context.Context
|
||||||
|
empty bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
ctx: context.Background(),
|
||||||
|
empty: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "nil",
|
||||||
|
ctx: peer.NewContext(context.Background(), nil),
|
||||||
|
empty: true,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "with value",
|
||||||
|
ctx: peer.NewContext(context.Background(), &peer.Peer{
|
||||||
|
Addr: addrs[0],
|
||||||
|
}),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
addr := PeerFromCtx(test.ctx)
|
||||||
|
assert.Equal(t, test.empty, len(addr) == 0)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestParseFullMethod(t *testing.T) {
|
func TestParseFullMethod(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
fullMethod string
|
fullMethod string
|
||||||
@ -68,3 +108,46 @@ func TestParseFullMethod(t *testing.T) {
|
|||||||
assert.Equal(t, test.attr, a)
|
assert.Equal(t, test.attr, a)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSpanInfo(t *testing.T) {
|
||||||
|
val, kvs := SpanInfo("/fullMethod", "remote")
|
||||||
|
assert.Equal(t, "fullMethod", val)
|
||||||
|
assert.NotEmpty(t, kvs)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestPeerAttr(t *testing.T) {
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
addr string
|
||||||
|
expect []attribute.KeyValue
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
name: "empty",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "port only",
|
||||||
|
addr: ":8080",
|
||||||
|
expect: []attribute.KeyValue{
|
||||||
|
semconv.NetPeerIPKey.String(localhost),
|
||||||
|
semconv.NetPeerPortKey.String("8080"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
name: "port only",
|
||||||
|
addr: "192.168.0.2:8080",
|
||||||
|
expect: []attribute.KeyValue{
|
||||||
|
semconv.NetPeerIPKey.String("192.168.0.2"),
|
||||||
|
semconv.NetPeerPortKey.String("8080"),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
test := test
|
||||||
|
t.Run(test.name, func(t *testing.T) {
|
||||||
|
t.Parallel()
|
||||||
|
kvs := PeerAttr(test.addr)
|
||||||
|
assert.EqualValues(t, test.expect, kvs)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user