Skip to content
Closed
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
4 changes: 2 additions & 2 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -886,15 +886,15 @@ func (args Arguments) Diff(objects []interface{}) (string, int) {
actualFmt = "(Missing)"
} else {
actual = objects[i]
actualFmt = fmt.Sprintf("(%[1]T=%[1]v)", actual)
actualFmt = fmt.Sprintf("(%[1]T=%[1]p)", actual)
}

if len(args) <= i {
expected = "(Missing)"
expectedFmt = "(Missing)"
} else {
expected = args[i]
expectedFmt = fmt.Sprintf("(%[1]T=%[1]v)", expected)
expectedFmt = fmt.Sprintf("(%[1]T=%[1]p)", expected)
}

if matcher, ok := expected.(argumentMatcher); ok {
Expand Down
35 changes: 35 additions & 0 deletions mock/mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,41 @@ func Test_MockReturnAndCalledConcurrent(t *testing.T) {
wg.Wait()
}

// Test to validate fix for racy concurrent value access in MethodCalled()
func Test_MockRunAndCalledConcurrent(t *testing.T) {
type argType struct {
Value int
}

arg := argType{}

iterations := 1000
m := &Mock{}
call := m.On("ConcurrencyTestMethod", Anything)

wg := sync.WaitGroup{}
wg.Add(2)

go func() {
for i := 0; i < iterations; i++ {
call.Run(func(args Arguments) {
u := args.Get(2).(*argType)
// u.mu.Lock()
u.Value = 2
// u.mu.Unlock()
})
}
wg.Done()
}()
go func() {
for i := 0; i < iterations; i++ {
m.Called(arg)
}
wg.Done()
}()
wg.Wait()
}

type timer struct{ Mock }

func (s *timer) GetTime(i int) string {
Expand Down