Skip to content
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
3 changes: 3 additions & 0 deletions mock/mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,10 @@ func (m *Mock) String() string {

// TestData holds any data that might be useful for testing. Testify ignores
// this data completely allowing you to do whatever you like with it.
// Access to the returned map must be synchronized by the caller.
func (m *Mock) TestData() objx.Map {
m.mutex.Lock()
defer m.mutex.Unlock()
if m.testData == nil {
m.testData = make(objx.Map)
}
Expand Down
44 changes: 44 additions & 0 deletions mock/testdata_race_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package mock

import (
"sync"
"testing"

"github.com/stretchr/objx"
)

func TestMockTestDataConcurrentInitialization(t *testing.T) {
const callers = 32
for attempt := 0; attempt < 20; attempt++ {
var mocked Mock
maps := make([]objx.Map, callers)
start := make(chan struct{})
var ready sync.WaitGroup
var done sync.WaitGroup
ready.Add(callers)
done.Add(callers)
for i := 0; i < callers; i++ {
go func(index int) {
defer done.Done()
ready.Done()
<-start
// Map writes remain the caller's responsibility. Only retrieve
// the shared map concurrently in this regression test.
maps[index] = mocked.TestData()
if maps[index] == nil {
t.Error("TestData returned nil")
}
}(i)
}
ready.Wait()
close(start)
done.Wait()
// Writes and reads here are sequential, after every accessor returns.
maps[0]["shared"] = attempt
for i, values := range maps {
if values["shared"] != attempt {
t.Errorf("caller %d did not receive the shared map", i)
}
}
}
}