Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: improve codecov #1878

Merged
merged 1 commit into from May 8, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/logx/writer.go
Expand Up @@ -241,7 +241,6 @@ func output(writer io.Writer, level string, val interface{}, fields ...LogField)

switch atomic.LoadUint32(&encoding) {
case plainEncodingType:
level = wrapLevelWithColor(level)
writePlainAny(writer, level, val, buildFields(fields...)...)
default:
entry := make(logEntryWithFields)
Expand Down Expand Up @@ -290,6 +289,8 @@ func writeJson(writer io.Writer, info interface{}) {
}

func writePlainAny(writer io.Writer, level string, val interface{}, fields ...string) {
level = wrapLevelWithColor(level)

switch v := val.(type) {
case string:
writePlainText(writer, level, v, fields...)
Expand Down
17 changes: 13 additions & 4 deletions core/logx/writer_test.go
Expand Up @@ -124,18 +124,27 @@ func TestWritePlainAny(t *testing.T) {
assert.Contains(t, buf.String(), "foo")

buf.Reset()
writePlainAny(nil, levelInfo, make(chan int))
writePlainAny(nil, levelError, make(chan int))
assert.Contains(t, buf.String(), "unsupported type")
writePlainAny(nil, levelInfo, 100)
writePlainAny(nil, levelSlow, 100)
assert.Contains(t, buf.String(), "100")

buf.Reset()
writePlainAny(hardToWriteWriter{}, levelInfo, 100)
writePlainAny(hardToWriteWriter{}, levelStat, 100)
assert.Contains(t, buf.String(), "write error")

buf.Reset()
writePlainAny(hardToWriteWriter{}, levelInfo, "foo")
writePlainAny(hardToWriteWriter{}, levelSevere, "foo")
assert.Contains(t, buf.String(), "write error")

buf.Reset()
writePlainAny(hardToWriteWriter{}, levelAlert, "foo")
assert.Contains(t, buf.String(), "write error")

buf.Reset()
writePlainAny(hardToWriteWriter{}, levelFatal, "foo")
assert.Contains(t, buf.String(), "write error")

}

type mockedEntry struct {
Expand Down
51 changes: 51 additions & 0 deletions rest/handler/loghandler_test.go
@@ -1,6 +1,8 @@
package handler

import (
"bytes"
"io"
"io/ioutil"
"log"
"net/http"
Expand Down Expand Up @@ -44,6 +46,33 @@ func TestLogHandler(t *testing.T) {
}
}

func TestLogHandlerVeryLong(t *testing.T) {
var buf bytes.Buffer
for i := 0; i < limitBodyBytes<<1; i++ {
buf.WriteByte('a')
}

req := httptest.NewRequest(http.MethodPost, "http://localhost", &buf)
handler := LogHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
r.Context().Value(internal.LogContext).(*internal.LogCollector).Append("anything")
io.Copy(ioutil.Discard, r.Body)
w.Header().Set("X-Test", "test")
w.WriteHeader(http.StatusServiceUnavailable)
_, err := w.Write([]byte("content"))
assert.Nil(t, err)

flusher, ok := w.(http.Flusher)
assert.True(t, ok)
flusher.Flush()
}))

resp := httptest.NewRecorder()
handler.ServeHTTP(resp, req)
assert.Equal(t, http.StatusServiceUnavailable, resp.Code)
assert.Equal(t, "test", resp.Header().Get("X-Test"))
assert.Equal(t, "content", resp.Body.String())
}

func TestLogHandlerSlow(t *testing.T) {
handlers := []func(handler http.Handler) http.Handler{
LogHandler,
Expand Down Expand Up @@ -106,6 +135,28 @@ func TestSetSlowThreshold(t *testing.T) {
assert.Equal(t, time.Second, slowThreshold.Load())
}

func TestWrapMethodWithColor(t *testing.T) {
// no tty
assert.Equal(t, http.MethodGet, wrapMethod(http.MethodGet))
assert.Equal(t, http.MethodPost, wrapMethod(http.MethodPost))
assert.Equal(t, http.MethodPut, wrapMethod(http.MethodPut))
assert.Equal(t, http.MethodDelete, wrapMethod(http.MethodDelete))
assert.Equal(t, http.MethodPatch, wrapMethod(http.MethodPatch))
assert.Equal(t, http.MethodHead, wrapMethod(http.MethodHead))
assert.Equal(t, http.MethodOptions, wrapMethod(http.MethodOptions))
assert.Equal(t, http.MethodConnect, wrapMethod(http.MethodConnect))
assert.Equal(t, http.MethodTrace, wrapMethod(http.MethodTrace))
}

func TestWrapStatusCodeWithColor(t *testing.T) {
// no tty
assert.Equal(t, "200", wrapStatusCode(http.StatusOK))
assert.Equal(t, "302", wrapStatusCode(http.StatusFound))
assert.Equal(t, "404", wrapStatusCode(http.StatusNotFound))
assert.Equal(t, "500", wrapStatusCode(http.StatusInternalServerError))
assert.Equal(t, "503", wrapStatusCode(http.StatusServiceUnavailable))
}

func BenchmarkLogHandler(b *testing.B) {
b.ReportAllocs()

Expand Down