log_writer_race_test.go 859 B

123456789101112131415161718192021222324252627282930313233343536
  1. package xray
  2. import (
  3. "sync"
  4. "testing"
  5. )
  6. // TestLogWriterLastLineConcurrent exercises the LogWriter from multiple
  7. // goroutines: Xray drives Write while another goroutine (Process.GetResult)
  8. // reads the last line. Run under `go test -race` this fails on an unguarded
  9. // lastLine field and passes once the access is serialized.
  10. func TestLogWriterLastLineConcurrent(t *testing.T) {
  11. lw := NewLogWriter()
  12. const writers, readers, iterations = 4, 4, 500
  13. var wg sync.WaitGroup
  14. wg.Add(writers + readers)
  15. for i := 0; i < writers; i++ {
  16. go func() {
  17. defer wg.Done()
  18. for j := 0; j < iterations; j++ {
  19. _, _ = lw.Write([]byte("2024/01/01 00:00:00.000000 [Info] connection accepted"))
  20. }
  21. }()
  22. }
  23. for i := 0; i < readers; i++ {
  24. go func() {
  25. defer wg.Done()
  26. for j := 0; j < iterations; j++ {
  27. _ = lw.LastLine()
  28. }
  29. }()
  30. }
  31. wg.Wait()
  32. }