process_race_test.go 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. package xray
  2. import (
  3. "errors"
  4. "os/exec"
  5. "sync"
  6. "testing"
  7. "time"
  8. )
  9. // TestProcessLifecycleFieldsRaceSafe drives the lifecycle fields (cmd, done,
  10. // exitErr) the way Start/startCommand and the waitForCommand goroutine do, while
  11. // the status getters read them concurrently. Run with -race: any unsynchronized
  12. // access to those fields is reported as a data race.
  13. func TestProcessLifecycleFieldsRaceSafe(t *testing.T) {
  14. p := &process{logWriter: NewLogWriter()}
  15. var wg sync.WaitGroup
  16. stop := make(chan struct{})
  17. // Writer: churn cmd/done/exitErr like Start + waitForCommand.
  18. wg.Go(func() {
  19. for {
  20. select {
  21. case <-stop:
  22. return
  23. default:
  24. }
  25. p.mu.Lock()
  26. p.cmd = &exec.Cmd{}
  27. p.done = make(chan struct{})
  28. p.mu.Unlock()
  29. p.setExitErr(errors.New("boom"))
  30. }
  31. })
  32. // Readers: the concurrent status getters.
  33. for range 4 {
  34. wg.Go(func() {
  35. for {
  36. select {
  37. case <-stop:
  38. return
  39. default:
  40. }
  41. _ = p.IsRunning()
  42. _ = p.GetErr()
  43. _ = p.GetResult()
  44. }
  45. })
  46. }
  47. time.Sleep(50 * time.Millisecond)
  48. close(stop)
  49. wg.Wait()
  50. }