title: How to write unit tests to check that methods are thread-safe
body: <p>I have an object like this:</p>
  
  <pre><code>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
  }
  </code></pre>
  
  <p>How should unit-tests to look like for methods <code>Read</code> &amp; <code>Write</code> to check that they are thread safe?</p>
  
  <p>I think there are already exists patterns for cases like this, but I didn't find anything. </p>
  
  <p>I read about <code>-race</code> flag:</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>My question is how to write unit-test which emulates realistic workloads.</p>