chore: simplify tests with logtest (#3184)

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

@ -6,3 +6,4 @@ ignore:
- "tools" - "tools"
- "**/mock" - "**/mock"
- "**/*_mock.go" - "**/*_mock.go"
- "**/*test"

@ -1,7 +1,6 @@
package logc package logc
import ( import (
"bytes"
"context" "context"
"encoding/json" "encoding/json"
"fmt" "fmt"
@ -11,14 +10,11 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/logx/logtest"
) )
func TestAddGlobalFields(t *testing.T) { func TestAddGlobalFields(t *testing.T) {
var buf bytes.Buffer buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
Info(context.Background(), "hello") Info(context.Background(), "hello")
buf.Reset() buf.Reset()
@ -34,155 +30,90 @@ func TestAddGlobalFields(t *testing.T) {
} }
func TestAlert(t *testing.T) { func TestAlert(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
Alert(context.Background(), "foo") Alert(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), "foo"), buf.String()) assert.True(t, strings.Contains(buf.String(), "foo"), buf.String())
} }
func TestError(t *testing.T) { func TestError(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Error(context.Background(), "foo") Error(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestErrorf(t *testing.T) { func TestErrorf(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Errorf(context.Background(), "foo %s", "bar") Errorf(context.Background(), "foo %s", "bar")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestErrorv(t *testing.T) { func TestErrorv(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Errorv(context.Background(), "foo") Errorv(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestErrorw(t *testing.T) { func TestErrorw(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Errorw(context.Background(), "foo", Field("a", "b")) Errorw(context.Background(), "foo", Field("a", "b"))
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestInfo(t *testing.T) { func TestInfo(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Info(context.Background(), "foo") Info(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestInfof(t *testing.T) { func TestInfof(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Infof(context.Background(), "foo %s", "bar") Infof(context.Background(), "foo %s", "bar")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestInfov(t *testing.T) { func TestInfov(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Infov(context.Background(), "foo") Infov(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestInfow(t *testing.T) { func TestInfow(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Infow(context.Background(), "foo", Field("a", "b")) Infow(context.Background(), "foo", Field("a", "b"))
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestDebug(t *testing.T) { func TestDebug(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Debug(context.Background(), "foo") Debug(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestDebugf(t *testing.T) { func TestDebugf(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Debugf(context.Background(), "foo %s", "bar") Debugf(context.Background(), "foo %s", "bar")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestDebugv(t *testing.T) { func TestDebugv(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Debugv(context.Background(), "foo") Debugv(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
} }
func TestDebugw(t *testing.T) { func TestDebugw(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Debugw(context.Background(), "foo", Field("a", "b")) Debugw(context.Background(), "foo", Field("a", "b"))
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1))) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)))
@ -204,48 +135,28 @@ func TestMisc(t *testing.T) {
} }
func TestSlow(t *testing.T) { func TestSlow(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Slow(context.Background(), "foo") Slow(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String()) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
} }
func TestSlowf(t *testing.T) { func TestSlowf(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Slowf(context.Background(), "foo %s", "bar") Slowf(context.Background(), "foo %s", "bar")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String()) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
} }
func TestSlowv(t *testing.T) { func TestSlowv(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Slowv(context.Background(), "foo") Slowv(context.Background(), "foo")
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String()) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())
} }
func TestSloww(t *testing.T) { func TestSloww(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
file, line := getFileLine() file, line := getFileLine()
Sloww(context.Background(), "foo", Field("a", "b")) Sloww(context.Background(), "foo", Field("a", "b"))
assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String()) assert.True(t, strings.Contains(buf.String(), fmt.Sprintf("%s:%d", file, line+1)), buf.String())

@ -0,0 +1,78 @@
package logtest
import (
"bytes"
"encoding/json"
"io"
"testing"
"github.com/zeromicro/go-zero/core/logx"
)
type Buffer struct {
buf *bytes.Buffer
t *testing.T
}
func Discard(t *testing.T) {
prev := logx.Reset()
logx.SetWriter(logx.NewWriter(io.Discard))
t.Cleanup(func() {
logx.SetWriter(prev)
})
}
func NewCollector(t *testing.T) *Buffer {
var buf bytes.Buffer
writer := logx.NewWriter(&buf)
prev := logx.Reset()
logx.SetWriter(writer)
t.Cleanup(func() {
logx.SetWriter(prev)
})
return &Buffer{
buf: &buf,
t: t,
}
}
func (b *Buffer) Bytes() []byte {
return b.buf.Bytes()
}
func (b *Buffer) Content() string {
var m map[string]interface{}
if err := json.Unmarshal(b.buf.Bytes(), &m); err != nil {
b.t.Error(err)
return ""
}
content, ok := m["content"]
if !ok {
return ""
}
switch val := content.(type) {
case string:
return val
default:
bs, err := json.Marshal(content)
if err != nil {
b.t.Error(err)
return ""
}
return string(bs)
}
}
func (b *Buffer) Reset() {
b.buf.Reset()
}
func (b *Buffer) String() string {
return b.buf.String()
}

@ -0,0 +1,22 @@
package logtest
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx"
)
func TestCollector(t *testing.T) {
const input = "hello"
c := NewCollector(t)
logx.Info(input)
assert.Equal(t, input, c.Content())
assert.Contains(t, c.String(), input)
}
func TestDiscard(t *testing.T) {
const input = "hello"
Discard(t)
logx.Info(input)
}

@ -5,19 +5,11 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
) )
func TestDumpGoroutines(t *testing.T) { func TestDumpGoroutines(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
w := logx.NewWriter(&buf)
o := logx.Reset()
logx.SetWriter(w)
defer func() {
logx.Reset()
logx.SetWriter(o)
}()
dumpGoroutines() dumpGoroutines()
assert.True(t, strings.Contains(buf.String(), ".dump")) assert.True(t, strings.Contains(buf.String(), ".dump"))
} }

@ -5,25 +5,16 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
) )
func TestProfile(t *testing.T) { func TestProfile(t *testing.T) {
var buf strings.Builder c := logtest.NewCollector(t)
w := logx.NewWriter(&buf)
o := logx.Reset()
logx.SetWriter(w)
defer func() {
logx.Reset()
logx.SetWriter(o)
}()
profiler := StartProfile() profiler := StartProfile()
// start again should not work // start again should not work
assert.NotNil(t, StartProfile()) assert.NotNil(t, StartProfile())
profiler.Stop() profiler.Stop()
// stop twice // stop twice
profiler.Stop() profiler.Stop()
assert.True(t, strings.Contains(buf.String(), ".pprof")) assert.True(t, strings.Contains(c.String(), ".pprof"))
} }

@ -1,12 +1,11 @@
package stat package stat
import ( import (
"bytes"
"strings" "strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
) )
func TestBToMb(t *testing.T) { func TestBToMb(t *testing.T) {
@ -41,15 +40,11 @@ func TestBToMb(t *testing.T) {
} }
func TestPrintUsage(t *testing.T) { func TestPrintUsage(t *testing.T) {
var buf bytes.Buffer c := logtest.NewCollector(t)
writer := logx.NewWriter(&buf)
old := logx.Reset()
logx.SetWriter(writer)
defer logx.SetWriter(old)
printUsage() printUsage()
output := buf.String() output := c.String()
assert.Contains(t, output, "CPU:") assert.Contains(t, output, "CPU:")
assert.Contains(t, output, "MEMORY:") assert.Contains(t, output, "MEMORY:")
assert.Contains(t, output, "Alloc=") assert.Contains(t, output, "Alloc=")

@ -3,12 +3,11 @@ package mon
import ( import (
"context" "context"
"errors" "errors"
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/breaker" "github.com/zeromicro/go-zero/core/breaker"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
"github.com/zeromicro/go-zero/core/stringx" "github.com/zeromicro/go-zero/core/stringx"
"github.com/zeromicro/go-zero/core/timex" "github.com/zeromicro/go-zero/core/timex"
"go.mongodb.org/mongo-driver/bson" "go.mongodb.org/mongo-driver/bson"
@ -573,15 +572,7 @@ func TestDecoratedCollection_LogDuration(t *testing.T) {
brk: breaker.NewBreaker(), brk: breaker.NewBreaker(),
} }
var buf strings.Builder buf := logtest.NewCollector(t)
w := logx.NewWriter(&buf)
o := logx.Reset()
logx.SetWriter(w)
defer func() {
logx.Reset()
logx.SetWriter(o)
}()
buf.Reset() buf.Reset()
c.logDuration(context.Background(), "foo", timex.Now(), nil, "bar") c.logDuration(context.Background(), "foo", timex.Now(), nil, "bar")

@ -3,12 +3,11 @@ package mon
import ( import (
"context" "context"
"errors" "errors"
"strings"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
) )
func TestFormatAddrs(t *testing.T) { func TestFormatAddrs(t *testing.T) {
@ -40,15 +39,7 @@ func TestFormatAddrs(t *testing.T) {
} }
func Test_logDuration(t *testing.T) { func Test_logDuration(t *testing.T) {
var buf strings.Builder buf := logtest.NewCollector(t)
w := logx.NewWriter(&buf)
o := logx.Reset()
logx.SetWriter(w)
defer func() {
logx.Reset()
logx.SetWriter(o)
}()
buf.Reset() buf.Reset()
logDuration(context.Background(), "foo", "bar", time.Millisecond, nil) logDuration(context.Background(), "foo", "bar", time.Millisecond, nil)

@ -9,7 +9,7 @@ import (
red "github.com/go-redis/redis/v8" red "github.com/go-redis/redis/v8"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
ztrace "github.com/zeromicro/go-zero/core/trace" ztrace "github.com/zeromicro/go-zero/core/trace"
tracesdk "go.opentelemetry.io/otel/trace" tracesdk "go.opentelemetry.io/otel/trace"
) )
@ -47,8 +47,7 @@ func TestHookProcessCase2(t *testing.T) {
}) })
defer ztrace.StopAgent() defer ztrace.StopAgent()
w, restore := injectLog() w := logtest.NewCollector(t)
defer restore()
ctx, err := durationHook.BeforeProcess(context.Background(), red.NewCmd(context.Background())) ctx, err := durationHook.BeforeProcess(context.Background(), red.NewCmd(context.Background()))
if err != nil { if err != nil {
@ -115,8 +114,7 @@ func TestHookProcessPipelineCase2(t *testing.T) {
}) })
defer ztrace.StopAgent() defer ztrace.StopAgent()
w, restore := injectLog() w := logtest.NewCollector(t)
defer restore()
ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{ ctx, err := durationHook.BeforeProcessPipeline(context.Background(), []red.Cmder{
red.NewCmd(context.Background()), red.NewCmd(context.Background()),
@ -135,8 +133,7 @@ func TestHookProcessPipelineCase2(t *testing.T) {
} }
func TestHookProcessPipelineCase3(t *testing.T) { func TestHookProcessPipelineCase3(t *testing.T) {
w, restore := injectLog() w := logtest.NewCollector(t)
defer restore()
assert.Nil(t, durationHook.AfterProcessPipeline(context.Background(), []red.Cmder{ assert.Nil(t, durationHook.AfterProcessPipeline(context.Background(), []red.Cmder{
red.NewCmd(context.Background()), red.NewCmd(context.Background()),
@ -145,8 +142,7 @@ func TestHookProcessPipelineCase3(t *testing.T) {
} }
func TestHookProcessPipelineCase4(t *testing.T) { func TestHookProcessPipelineCase4(t *testing.T) {
w, restore := injectLog() w := logtest.NewCollector(t)
defer restore()
ctx := context.WithValue(context.Background(), startTimeKey, "foo") ctx := context.WithValue(context.Background(), startTimeKey, "foo")
assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{ assert.Nil(t, durationHook.AfterProcessPipeline(ctx, []red.Cmder{
@ -169,8 +165,7 @@ func TestHookProcessPipelineCase5(t *testing.T) {
} }
func TestLogDuration(t *testing.T) { func TestLogDuration(t *testing.T) {
w, restore := injectLog() w := logtest.NewCollector(t)
defer restore()
logDuration(context.Background(), []red.Cmder{ logDuration(context.Background(), []red.Cmder{
red.NewCmd(context.Background(), "get", "foo"), red.NewCmd(context.Background(), "get", "foo"),
@ -183,15 +178,3 @@ func TestLogDuration(t *testing.T) {
}, 1*time.Second) }, 1*time.Second)
assert.True(t, strings.Contains(w.String(), `get foo\nset bar 0`)) assert.True(t, strings.Contains(w.String(), `get foo\nset bar 0`))
} }
func injectLog() (r *strings.Builder, restore func()) {
var buf strings.Builder
w := logx.NewWriter(&buf)
o := logx.Reset()
logx.SetWriter(w)
return &buf, func() {
logx.Reset()
logx.SetWriter(o)
}
}

@ -16,5 +16,6 @@ func NewInMemoryExporter(t *testing.T) *tracetest.InMemoryExporter {
me.Reset() me.Reset()
}) })
otel.SetTracerProvider(trace.NewTracerProvider(trace.WithSyncer(me))) otel.SetTracerProvider(trace.NewTracerProvider(trace.WithSyncer(me)))
return me return me
} }

@ -7,12 +7,10 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx"
"github.com/zeromicro/go-zero/core/stat" "github.com/zeromicro/go-zero/core/stat"
) )
func init() { func init() {
logx.Disable()
stat.SetReporter(nil) stat.SetReporter(nil)
} }

@ -62,10 +62,6 @@ type requestSettings struct {
signature string signature string
} }
func init() {
log.SetOutput(io.Discard)
}
func TestContentSecurityHandler(t *testing.T) { func TestContentSecurityHandler(t *testing.T) {
tests := []struct { tests := []struct {
method string method string

@ -4,7 +4,6 @@ import (
"bytes" "bytes"
"encoding/base64" "encoding/base64"
"io" "io"
"log"
"math/rand" "math/rand"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
@ -21,10 +20,6 @@ const (
var aesKey = []byte(`PdSgVkYp3s6v9y$B&E)H+MbQeThWmZq4`) var aesKey = []byte(`PdSgVkYp3s6v9y$B&E)H+MbQeThWmZq4`)
func init() {
log.SetOutput(io.Discard)
}
func TestCryptionHandlerGet(t *testing.T) { func TestCryptionHandlerGet(t *testing.T) {
req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody) req := httptest.NewRequest(http.MethodGet, "/any", http.NoBody)
handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := CryptionHandler(aesKey)(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

@ -3,7 +3,6 @@ package handler
import ( import (
"bytes" "bytes"
"io" "io"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -14,10 +13,6 @@ import (
"github.com/zeromicro/go-zero/rest/internal/response" "github.com/zeromicro/go-zero/rest/internal/response"
) )
func init() {
log.SetOutput(io.Discard)
}
func TestLogHandler(t *testing.T) { func TestLogHandler(t *testing.T) {
handlers := []func(handler http.Handler) http.Handler{ handlers := []func(handler http.Handler) http.Handler{
LogHandler, LogHandler,

@ -1,8 +1,6 @@
package handler package handler
import ( import (
"io"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"sync" "sync"
@ -14,10 +12,6 @@ import (
const conns = 4 const conns = 4
func init() {
log.SetOutput(io.Discard)
}
func TestMaxConnsHandler(t *testing.T) { func TestMaxConnsHandler(t *testing.T) {
var waitGroup sync.WaitGroup var waitGroup sync.WaitGroup
waitGroup.Add(conns) waitGroup.Add(conns)

@ -1,8 +1,6 @@
package handler package handler
import ( import (
"io"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -10,10 +8,6 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
) )
func init() {
log.SetOutput(io.Discard)
}
func TestWithPanic(t *testing.T) { func TestWithPanic(t *testing.T) {
handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := RecoverHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
panic("whatever") panic("whatever")

@ -1,8 +1,6 @@
package handler package handler
import ( import (
"io"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
@ -12,10 +10,6 @@ import (
"github.com/zeromicro/go-zero/core/stat" "github.com/zeromicro/go-zero/core/stat"
) )
func init() {
log.SetOutput(io.Discard)
}
func TestSheddingHandlerAccept(t *testing.T) { func TestSheddingHandlerAccept(t *testing.T) {
metrics := stat.NewMetrics("unit-test") metrics := stat.NewMetrics("unit-test")
shedder := mockShedder{ shedder := mockShedder{

@ -31,15 +31,15 @@ const (
// Notice: even if canceled in server side, 499 will be logged as well. // Notice: even if canceled in server side, 499 will be logged as well.
func TimeoutHandler(duration time.Duration) func(http.Handler) http.Handler { func TimeoutHandler(duration time.Duration) func(http.Handler) http.Handler {
return func(next http.Handler) http.Handler { return func(next http.Handler) http.Handler {
if duration > 0 { if duration <= 0 {
return next
}
return &timeoutHandler{ return &timeoutHandler{
handler: next, handler: next,
dt: duration, dt: duration,
} }
} }
return next
}
} }
// timeoutHandler is the handler that controls the request timeout. // timeoutHandler is the handler that controls the request timeout.
@ -207,9 +207,11 @@ func relevantCaller() runtime.Frame {
if !strings.HasPrefix(frame.Function, "net/http.") { if !strings.HasPrefix(frame.Function, "net/http.") {
return frame return frame
} }
if !more { if !more {
break break
} }
} }
return frame return frame
} }

@ -2,21 +2,16 @@ package handler
import ( import (
"context" "context"
"io"
"log"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx/logtest"
"github.com/zeromicro/go-zero/rest/internal/response" "github.com/zeromicro/go-zero/rest/internal/response"
) )
func init() {
log.SetOutput(io.Discard)
}
func TestTimeout(t *testing.T) { func TestTimeout(t *testing.T) {
timeoutHandler := TimeoutHandler(time.Millisecond) timeoutHandler := TimeoutHandler(time.Millisecond)
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
@ -45,7 +40,12 @@ func TestWithTimeoutTimedout(t *testing.T) {
timeoutHandler := TimeoutHandler(time.Millisecond) timeoutHandler := TimeoutHandler(time.Millisecond)
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
time.Sleep(time.Millisecond * 10) time.Sleep(time.Millisecond * 10)
w.Write([]byte(`foo`)) _, err := w.Write([]byte(`foo`))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
@ -96,7 +96,12 @@ func TestTimeoutWebsocket(t *testing.T) {
func TestTimeoutWroteHeaderTwice(t *testing.T) { func TestTimeoutWroteHeaderTwice(t *testing.T) {
timeoutHandler := TimeoutHandler(time.Minute) timeoutHandler := TimeoutHandler(time.Minute)
handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { handler := timeoutHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(`hello`)) _, err := w.Write([]byte(`hello`))
if err != nil {
w.WriteHeader(http.StatusInternalServerError)
return
}
w.Header().Set("foo", "bar") w.Header().Set("foo", "bar")
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
})) }))
@ -145,7 +150,7 @@ func TestTimeoutHijack(t *testing.T) {
} }
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
writer.Hijack() _, _, _ = writer.Hijack()
}) })
writer = &timeoutWriter{ writer = &timeoutWriter{
@ -155,7 +160,7 @@ func TestTimeoutHijack(t *testing.T) {
} }
assert.NotPanics(t, func() { assert.NotPanics(t, func() {
writer.Hijack() _, _, _ = writer.Hijack()
}) })
} }
@ -165,7 +170,7 @@ func TestTimeoutPusher(t *testing.T) {
} }
assert.Panics(t, func() { assert.Panics(t, func() {
handler.Push("any", nil) _ = handler.Push("any", nil)
}) })
handler = &timeoutWriter{ handler = &timeoutWriter{
@ -174,20 +179,44 @@ func TestTimeoutPusher(t *testing.T) {
assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil)) assert.Equal(t, http.ErrNotSupported, handler.Push("any", nil))
} }
func TestTimeoutWriter_Hijack(t *testing.T) {
writer := &timeoutWriter{
w: httptest.NewRecorder(),
h: make(http.Header),
req: httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody),
}
_, _, err := writer.Hijack()
assert.Error(t, err)
}
func TestTimeoutWroteTwice(t *testing.T) {
c := logtest.NewCollector(t)
writer := &timeoutWriter{
w: &response.WithCodeResponseWriter{
Writer: httptest.NewRecorder(),
},
h: make(http.Header),
req: httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody),
}
writer.writeHeaderLocked(http.StatusOK)
writer.writeHeaderLocked(http.StatusOK)
assert.Contains(t, c.String(), "superfluous response.WriteHeader call")
}
type mockedPusher struct{} type mockedPusher struct{}
func (m mockedPusher) Header() http.Header { func (m mockedPusher) Header() http.Header {
panic("implement me") panic("implement me")
} }
func (m mockedPusher) Write(bytes []byte) (int, error) { func (m mockedPusher) Write(_ []byte) (int, error) {
panic("implement me") panic("implement me")
} }
func (m mockedPusher) WriteHeader(statusCode int) { func (m mockedPusher) WriteHeader(_ int) {
panic("implement me") panic("implement me")
} }
func (m mockedPusher) Push(target string, opts *http.PushOptions) error { func (m mockedPusher) Push(_ string, _ *http.PushOptions) error {
panic("implement me") panic("implement me")
} }

@ -8,7 +8,7 @@ import (
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
) )
func TestInfo(t *testing.T) { func TestInfo(t *testing.T) {
@ -25,20 +25,11 @@ func TestInfo(t *testing.T) {
} }
func TestError(t *testing.T) { func TestError(t *testing.T) {
var buf strings.Builder c := logtest.NewCollector(t)
w := logx.NewWriter(&buf)
o := logx.Reset()
logx.SetWriter(w)
defer func() {
logx.Reset()
logx.SetWriter(o)
}()
req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody) req := httptest.NewRequest(http.MethodGet, "http://localhost", http.NoBody)
Error(req, "first") Error(req, "first")
Errorf(req, "second %s", "third") Errorf(req, "second %s", "third")
val := buf.String() val := c.String()
assert.True(t, strings.Contains(val, "first")) assert.True(t, strings.Contains(val, "first"))
assert.True(t, strings.Contains(val, "second")) assert.True(t, strings.Contains(val, "second"))
assert.True(t, strings.Contains(val, "third")) assert.True(t, strings.Contains(val, "third"))

@ -14,7 +14,7 @@ import (
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/conf" "github.com/zeromicro/go-zero/core/conf"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
"github.com/zeromicro/go-zero/rest/chain" "github.com/zeromicro/go-zero/rest/chain"
"github.com/zeromicro/go-zero/rest/httpx" "github.com/zeromicro/go-zero/rest/httpx"
"github.com/zeromicro/go-zero/rest/internal/cors" "github.com/zeromicro/go-zero/rest/internal/cors"
@ -22,9 +22,7 @@ import (
) )
func TestNewServer(t *testing.T) { func TestNewServer(t *testing.T) {
writer := logx.Reset() logtest.Discard(t)
defer logx.SetWriter(writer)
logx.SetWriter(logx.NewWriter(io.Discard))
const configYaml = ` const configYaml = `
Name: foo Name: foo

@ -1,121 +1,96 @@
package internal package internal
import ( import (
"strings"
"testing" "testing"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/core/logx/logtest"
) )
const content = "foo" const content = "foo"
func TestLoggerError(t *testing.T) { func TestLoggerError(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Error(content) logger.Error(content)
assert.Contains(t, w.String(), content) assert.Contains(t, c.String(), content)
} }
func TestLoggerErrorf(t *testing.T) { func TestLoggerErrorf(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Errorf(content) logger.Errorf(content)
assert.Contains(t, w.String(), content) assert.Contains(t, c.String(), content)
} }
func TestLoggerErrorln(t *testing.T) { func TestLoggerErrorln(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Errorln(content) logger.Errorln(content)
assert.Contains(t, w.String(), content) assert.Contains(t, c.String(), content)
} }
func TestLoggerFatal(t *testing.T) { func TestLoggerFatal(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Fatal(content) logger.Fatal(content)
assert.Contains(t, w.String(), content) assert.Contains(t, c.String(), content)
} }
func TestLoggerFatalf(t *testing.T) { func TestLoggerFatalf(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Fatalf(content) logger.Fatalf(content)
assert.Contains(t, w.String(), content) assert.Contains(t, c.String(), content)
} }
func TestLoggerFatalln(t *testing.T) { func TestLoggerFatalln(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Fatalln(content) logger.Fatalln(content)
assert.Contains(t, w.String(), content) assert.Contains(t, c.String(), content)
} }
func TestLoggerInfo(t *testing.T) { func TestLoggerInfo(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Info(content) logger.Info(content)
assert.Empty(t, w.String()) assert.Empty(t, c.String())
} }
func TestLoggerInfof(t *testing.T) { func TestLoggerInfof(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Infof(content) logger.Infof(content)
assert.Empty(t, w.String()) assert.Empty(t, c.String())
} }
func TestLoggerWarning(t *testing.T) { func TestLoggerWarning(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Warning(content) logger.Warning(content)
assert.Empty(t, w.String()) assert.Empty(t, c.String())
} }
func TestLoggerInfoln(t *testing.T) { func TestLoggerInfoln(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Infoln(content) logger.Infoln(content)
assert.Empty(t, w.String()) assert.Empty(t, c.String())
} }
func TestLoggerWarningf(t *testing.T) { func TestLoggerWarningf(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Warningf(content) logger.Warningf(content)
assert.Empty(t, w.String()) assert.Empty(t, c.String())
} }
func TestLoggerWarningln(t *testing.T) { func TestLoggerWarningln(t *testing.T) {
w, restore := injectLog() c := logtest.NewCollector(t)
defer restore()
logger := new(Logger) logger := new(Logger)
logger.Warningln(content) logger.Warningln(content)
assert.Empty(t, w.String()) assert.Empty(t, c.String())
} }
func TestLogger_V(t *testing.T) { func TestLogger_V(t *testing.T) {
@ -125,15 +100,3 @@ func TestLogger_V(t *testing.T) {
// grpclog.infoLog // grpclog.infoLog
assert.False(t, logger.V(0)) assert.False(t, logger.V(0))
} }
func injectLog() (r *strings.Builder, restore func()) {
var buf strings.Builder
w := logx.NewWriter(&buf)
o := logx.Reset()
logx.SetWriter(w)
return &buf, func() {
logx.Reset()
logx.SetWriter(o)
}
}

@ -4,6 +4,7 @@ import (
"context" "context"
"sync" "sync"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/zeromicro/go-zero/core/proc" "github.com/zeromicro/go-zero/core/proc"
@ -40,6 +41,8 @@ func TestRpcServer(t *testing.T) {
}() }()
wg.Wait() wg.Wait()
time.Sleep(100 * time.Millisecond)
lock.Lock() lock.Lock()
grpcServer.GracefulStop() grpcServer.GracefulStop()
lock.Unlock() lock.Unlock()

Loading…
Cancel
Save