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: avoid data races in Arguments.Diff #1598

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

petergardfjall
Copy link

@petergardfjall petergardfjall commented May 14, 2024

Fixes a concurrency issue that would lead to testify mocks producing data races detected by go test -race. These data races would occur whenever a mock pointer argument was concurrently modified. The reason being that Arguments.Diff uses the %v format specifier to get a presentable string for the argument. This also traverses the pointed-to data structure, which would lead to the data race.

It should be noted that it is heavily inspired by #1229, but since that PR hasn't seen any updates in over a year I decided to start fresh.

Summary

Avoids testify mocks causing data races for (concurrently accessed) pointer arguments when running go test -race.

Changes

Arguments.Diff now uses %p to produce a "presentable string" for pointers rather than %v, since the latter traverses the pointed to data structure.

Motivation

We need to test our code for race conditions with go test -race, and we cannot have testify be the offender of data races.

Related issues

#1597

Fixes a concurrency issue that would lead to testify mocks producing data races
detected by go test -race. These data races would occur whenever a mock pointer
argument was concurrently modified. The reason being that Arguments.Diff uses
the %v format specifier to get a presentable string for the argument. This also
traverses the pointed-to data structure, which would lead to the data race.

Signed-off-by: Peter Gardfjäll <[email protected]>
@dolmen dolmen changed the title avoid data races in Arguments.Diff mock: avoid data races in Arguments.Diff May 16, 2024
@dolmen dolmen added bug pkg-mock Any issues related to Mock labels May 16, 2024
@dolmen dolmen requested a review from brackendawson May 16, 2024 11:51
@petergardfjall
Copy link
Author

Friendly reminder. 🙂

@petergardfjall
Copy link
Author

Any chance of this being accepted?

@ndk
Copy link

ndk commented Jul 9, 2024

It would be great to see the fix in master branch

Copy link
Collaborator

@brackendawson brackendawson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This solution only works for reflect.Pointer kinds, but there are other kinds which are inherent pointers, such as maps.

Consider this test:

type mapArgMock struct{ Mock }

func (m *mapArgMock) Question(arg map[string]string) int {
	args := m.Called(arg)
	return args.Int(0)
}

func Test_CallMockWithConcurrentlyModifiedMapArg(t *testing.T) {
	m := &mapArgMock{}
	m.On("Question", Anything).Return(42)

	mapArg := map[string]string{"Question": "What's the meaning of life?"}

	// Emulates a situation where the pointer value gets concurrently updated by another thread.
	wg := sync.WaitGroup{}
	wg.Add(1)
	go func() {
		defer wg.Done()
		mapArg["Question"] = "What is 7 * 6?"
	}()

	// This is where we would get a data race since Arguments.Diff would traverse the pointed to
	// struct while being updated. Something go test -race would identify as a data race.
	value := m.Question(mapArg)
	assert.Equal(t, 42, value)
	wg.Wait()

	m.AssertExpectations(t)
}

IsPtr needs to be replaced with a more comprehensive test for pointyness.

I also fear the wrath of the users passing complex struct pointers with small errors into mocks, they won't see the diff any more. But I probably care more about fixing the data race.

Is this the only way to fix the issue? Given that we consider equivelant instances of structs to be equal, could On be made to create a deep copy of the argument?

PS. Sorry for my tardiness on this review.

@brackendawson
Copy link
Collaborator

Thinking more on the above, does IsPtr need to recurse deep objects up to the first pointer shaped object to catch pointer fields in non-pointer structs? It's turtles all the way down.

@petergardfjall
Copy link
Author

Thinking more on the above, does IsPtr need to recurse deep objects up to the first pointer shaped object to catch pointer fields in non-pointer structs? It's turtles all the way down.

Yes, I think you are right. The IsPtr only works for pointer arguments, but not for other "mutable" types such as Map, Slice, Array. So a more comprehensive check would be something like:

// isMutable indicates if the supplied value is of a mutable type.
func isMutable(v interface{}) bool {
	switch reflect.ValueOf(v).Kind() {
	case reflect.Array, reflect.Chan, reflect.Map, reflect.Ptr, reflect.Slice:
		return true
	}
	return false
}

And then it still won't catch the case where a struct value (not pointer) gets passed which contains a nested pointer field that gets updated.

Not sure how to solve this as recursing down v in search of a mutable/"pointerish" field traverses the value which is what we were trying to avoid to begin with (then we might as well just use %v). 🤷

@brackendawson
Copy link
Collaborator

brackendawson commented Oct 3, 2024

It is possible to write a test to show if any leaf of the structure is "pointerish" without reading those leaves, but I like it about as much as you do.

I considered if we could make a deep copy of the object rather than relying on the pointers but this isn't possible. Even with reflection you cannot copy unexported struct fields, but unexported struct fields' values are used when comparing structs with reflect.DeepEqual (and with ==).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug pkg-mock Any issues related to Mock
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants