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

mock: update callString to format context.Context as pointer #1689

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mock

import (
"context"
"errors"
"fmt"
"path"
Expand Down Expand Up @@ -446,6 +447,11 @@ func callString(method string, arguments Arguments, includeArgumentValues bool)
if includeArgumentValues {
var argVals []string
for argIndex, arg := range arguments {
if ctx, ok := arg.(context.Context); ok {
// avoid data race from formating context directly
argVals = append(argVals, fmt.Sprintf("%d: %p", argIndex, ctx))
continue
}
if _, ok := arg.(*FunctionalOptionsArgument); ok {
argVals = append(argVals, fmt.Sprintf("%d: %s", argIndex, arg))
continue
Expand Down
19 changes: 19 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mock

import (
"context"
"errors"
"fmt"
"regexp"
Expand Down Expand Up @@ -1234,6 +1235,24 @@ func Test_callString(t *testing.T) {
assert.Equal(t, `Method(int,bool,string)`, callString("Method", []interface{}{1, true, "something"}, false))
assert.Equal(t, `Method(<nil>)`, callString("Method", []interface{}{nil}, false))

t.Run("context", func(t *testing.T) {
// special treatment for context.Context to avoid data races
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
done := make(chan struct{})
t.Cleanup(func() {
<-done
})
go func() {
defer close(done)
for i := 0; i < 20; i++ {
cancel()
}
}()
assert.Equal(t, fmt.Sprintf(`Method(*context.cancelCtx)
0: %p`, ctx),
callString("Method", []interface{}{ctx}, true))
})
}

func Test_Mock_Called(t *testing.T) {
Expand Down
Loading