chore: add more tests (#3299)

master
Kevin Wan 2 years ago committed by GitHub
parent e751736516
commit 8a4cc4f98d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -61,5 +61,5 @@ jobs:
run: | run: |
go mod verify go mod verify
go mod download go mod download
go test -v -race ./... go test ./...
cd tools/goctl && go build -v goctl.go cd tools/goctl && go build -v goctl.go

@ -85,10 +85,7 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) {
if len(c.OtlpHeaders) > 0 { if len(c.OtlpHeaders) > 0 {
opts = append(opts, otlptracegrpc.WithHeaders(c.OtlpHeaders)) opts = append(opts, otlptracegrpc.WithHeaders(c.OtlpHeaders))
} }
return otlptracegrpc.New( return otlptracegrpc.New(context.Background(), opts...)
context.Background(),
opts...,
)
case kindOtlpHttp: case kindOtlpHttp:
// Not support flexible configuration now. // Not support flexible configuration now.
opts := []otlptracehttp.Option{ opts := []otlptracehttp.Option{
@ -101,10 +98,7 @@ func createExporter(c Config) (sdktrace.SpanExporter, error) {
if len(c.OtlpHttpPath) > 0 { if len(c.OtlpHttpPath) > 0 {
opts = append(opts, otlptracehttp.WithURLPath(c.OtlpHttpPath)) opts = append(opts, otlptracehttp.WithURLPath(c.OtlpHttpPath))
} }
return otlptracehttp.New( return otlptracehttp.New(context.Background(), opts...)
context.Background(),
opts...,
)
case kindFile: case kindFile:
f, err := os.OpenFile(c.Endpoint, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666) f, err := os.OpenFile(c.Endpoint, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0666)
if err != nil { if err != nil {

@ -17,7 +17,8 @@ func TestStartAgent(t *testing.T) {
endpoint4 = "localhost:1236" endpoint4 = "localhost:1236"
endpoint5 = "udp://localhost:6831" endpoint5 = "udp://localhost:6831"
endpoint6 = "localhost:1237" endpoint6 = "localhost:1237"
endpoint7 = "/tmp/trace.log" endpoint71 = "/tmp/trace.log"
endpoint72 = "/not-exist-fs/trace.log"
) )
c1 := Config{ c1 := Config{
Name: "foo", Name: "foo",
@ -66,7 +67,12 @@ func TestStartAgent(t *testing.T) {
} }
c9 := Config{ c9 := Config{
Name: "file", Name: "file",
Endpoint: endpoint7, Endpoint: endpoint71,
Batcher: kindFile,
}
c10 := Config{
Name: "file",
Endpoint: endpoint72,
Batcher: kindFile, Batcher: kindFile,
} }
@ -80,6 +86,7 @@ func TestStartAgent(t *testing.T) {
StartAgent(c7) StartAgent(c7)
StartAgent(c8) StartAgent(c8)
StartAgent(c9) StartAgent(c9)
StartAgent(c10)
defer StopAgent() defer StopAgent()
lock.Lock() lock.Lock()
@ -97,6 +104,8 @@ func TestStartAgent(t *testing.T) {
assert.True(t, ok) assert.True(t, ok)
_, ok = agents[endpoint6] _, ok = agents[endpoint6]
assert.False(t, ok) assert.False(t, ok)
_, ok = agents[endpoint7] _, ok = agents[endpoint71]
assert.True(t, ok) assert.True(t, ok)
_, ok = agents[endpoint72]
assert.False(t, ok)
} }

@ -2,6 +2,7 @@ package rest
import ( import (
"context" "context"
"crypto/tls"
"errors" "errors"
"fmt" "fmt"
"net/http" "net/http"
@ -15,6 +16,7 @@ import (
"github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/fs" "github.com/zeromicro/go-zero/core/fs"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/rest/router"
) )
const ( const (
@ -200,6 +202,7 @@ Verbose: true
}, },
} }
var index int32
for _, yaml := range yamls { for _, yaml := range yamls {
yaml := yaml yaml := yaml
for _, route := range routes { for _, route := range routes {
@ -208,6 +211,11 @@ Verbose: true
var cnf RestConf var cnf RestConf
assert.Nil(t, conf.LoadFromYamlBytes([]byte(yaml), &cnf)) assert.Nil(t, conf.LoadFromYamlBytes([]byte(yaml), &cnf))
ng := newEngine(cnf) ng := newEngine(cnf)
if atomic.AddInt32(&index, 1)%2 == 0 {
ng.setUnsignedCallback(func(w http.ResponseWriter, r *http.Request,
next http.Handler, strict bool, code int) {
})
}
ng.addRoutes(route) ng.addRoutes(route)
ng.use(func(next http.HandlerFunc) http.HandlerFunc { ng.use(func(next http.HandlerFunc) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
@ -398,6 +406,29 @@ func TestEngine_withTimeout(t *testing.T) {
} }
} }
func TestEngine_start(t *testing.T) {
logx.Disable()
t.Run("http", func(t *testing.T) {
ng := newEngine(RestConf{
Host: "localhost",
Port: -1,
})
assert.Error(t, ng.start(router.NewRouter()))
})
t.Run("https", func(t *testing.T) {
ng := newEngine(RestConf{
Host: "localhost",
Port: -1,
CertFile: "foo",
KeyFile: "bar",
})
ng.tlsConfig = &tls.Config{}
assert.Error(t, ng.start(router.NewRouter()))
})
}
type mockedRouter struct { type mockedRouter struct {
} }

Loading…
Cancel
Save