I have an object like this:
type Store struct {
mutex sync.RWMutex
data map[string]int
}
func (s * Store) Read(key string) int, error {
// ...
defer s.mutex.RUnlock()
s.mutex.RLock()
// ...
return val, nil
}
func (s * Store) Write(key string, value int) error {
// ...
defer s.mutex.Unlock()
s.mutex.Lock()
// ...
return nil
}
How should unit-tests to look like for methods Read
& Write
to check that they are thread safe?
I think there are already exists patterns for cases like this, but I didn't find anything.
I read about -race
flag:
the race detector can detect race conditions only when they are actually triggered by running code, which means it's important to run race-enabled binaries under realistic workloads
My question is how to write unit-test which emulates realistic workloads.