You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
mock: fix data race in Arguments.Diff for pointer-like arguments
Arguments.Diff uses fmt.Sprintf("%v") to format arguments for diff output.
When arguments contain pointers, maps, slices, or other reference types that
are concurrently modified by other goroutines, this causes data races:
- For maps: a fatal "concurrent map iteration and map write" error that
crashes the entire test process (non-recoverable)
- For pointers/slices: data races detectable by -race, and potential panics
This commit introduces safeFormatArg() which handles each case:
- Maps: uses %p (address-only) since map iteration cannot be made safe
without external synchronization
- Pointers/slices: attempts %v with panic recovery, falls back to %p
- All other types: unchanged %v behavior (value types, no race possible)
This preserves the full %v diff output for the common case (non-concurrent
arguments, value types, pointers/slices without races) while preventing
testify itself from being the source of data races in user tests.
Fixes#1597
Related: #1598 (alternative approach using %p for all pointers)
`\s+mock: Unexpected Method Call\s+-*\s+GetTimes\(\[\]int\)\s+0: \[\]int\{1\}\s+The closest call I have is:\s+GetTimes\(mock.argumentMatcher\)\s+0: mock.argumentMatcher\{.*?\}\s+Diff:.*\(\[\]int=\[1\]\) not matched by func\(\[\]int\) bool\nat: \[[^\]]+mock\/mock_test.go`)
2349
+
`\s+mock: Unexpected Method Call\s+-*\s+GetTimes\(\[\]int\)\s+0: \[\]int\{1\}\s+The closest call I have is:\s+GetTimes\(mock.argumentMatcher\)\s+0: mock.argumentMatcher\{.*?\}\s+Diff:.*\(\[\]int=0x[0-9a-f]+\) not matched by func\(\[\]int\) bool\nat: \[[^\]]+mock\/mock_test.go`)
0 commit comments