Skip to content

Commit

Permalink
Make logTestCase an anonymous struct and 'want' plaintext
Browse files Browse the repository at this point in the history
  • Loading branch information
hchen12 committed Jul 7, 2020
1 parent 2636e79 commit 2c879a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 28 deletions.
4 changes: 1 addition & 3 deletions api/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (

type logFunc func(statusCode int, err error)

const panicRecoveryPrefix = "panic: "

// logWithCheckingPanic attemps to recover from a possible panic,
// modifies statusCode and err if there was indeed a panic,
// passes the possibly updated status and err to the logFunc,
Expand All @@ -17,7 +15,7 @@ const panicRecoveryPrefix = "panic: "
func logWithCheckingPanic(f logFunc, statusCode int, err error) {
if r := recover(); r != nil {
statusCode = http.StatusInternalServerError
err = fmt.Errorf("%s%v", panicRecoveryPrefix, r)
err = fmt.Errorf("panic: %v", r)
defer panic(r)
}
f(statusCode, err)
Expand Down
47 changes: 22 additions & 25 deletions api/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,43 +7,40 @@ import (
"testing"
)

type logTestCase struct {
name string
panicInput interface{}
}

func TestLogWithCheckingPanic(t *testing.T) {
t.Parallel()
testCases := []*logTestCase{
testCases := []struct {
name string
input interface{}
want string // See logStr below for the format
}{
{
name: "panic with string",
panicInput: "string",
name: "panic with string",
input: "string",
want: "st: 500, err: panic: string",
},
{
name: "panic with error",
panicInput: errors.New("error"),
name: "panic with error",
input: errors.New("error"),
want: "st: 500, err: panic: error",
},
{
name: "no panic",
panicInput: nil,
name: "no panic",
input: nil,
want: "st: 200, err: <nil>", // See inputStatusCode below for 200
},
}
const (
logStr = "st: %d, err: %v"
inputStatusCode = http.StatusOK
)
var inputError error

for _, tc := range testCases {
// https://github.com/golang/go/wiki/CommonMistakes#using-goroutines-on-loop-iterator-variables
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
const (
logStr = "st: %d, err: %v"
inputStatusCode = http.StatusOK
)
var inputError error

want := fmt.Sprintf(logStr, http.StatusInternalServerError, panicRecoveryPrefix+fmt.Sprintf("%s", tc.panicInput))
if tc.panicInput == nil {
want = fmt.Sprintf(logStr, inputStatusCode, inputError)
}

got := ""
f := func(statusCode int, err error) {
Expand All @@ -53,12 +50,12 @@ func TestLogWithCheckingPanic(t *testing.T) {
defer func() {
// Capture the panic thrown from logWithCheckingPanic.
recover()
if got != want {
t.Errorf("got: %q, want: %q", got, want)
if got != tc.want {
t.Errorf("got: %q, want: %q", got, tc.want)
}
}()
defer logWithCheckingPanic(f, inputStatusCode, inputError)
panic(tc.panicInput)
panic(tc.input)
})
}
}

0 comments on commit 2c879a6

Please sign in to comment.