Files
3x-ui/internal/xray/log_writer_race_test.go
T
MHSanaei 1c0b76c27a Use efficient APIs and simplify loops
Minor refactors across the codebase to improve readability and use more efficient APIs: replace fmt.Sprintf+base64 encoding with fmt.Appendf when building Shadowsocks userInfo; compute elapsed using max(now-prev.at, window) to simplify logic; use strings.SplitSeq for splitting in two places; simplify test and goroutine loops to range-based iterations and use errgroup's Go helper; and align/clean up struct field formatting and test map literals. Mostly stylistic/efficiency changes with no intended behavior changes.
2026-06-23 14:12:28 +02:00

37 lines
815 B
Go

package xray
import (
"sync"
"testing"
)
// TestLogWriterLastLineConcurrent exercises the LogWriter from multiple
// goroutines: Xray drives Write while another goroutine (Process.GetResult)
// reads the last line. Run under `go test -race` this fails on an unguarded
// lastLine field and passes once the access is serialized.
func TestLogWriterLastLineConcurrent(t *testing.T) {
lw := NewLogWriter()
const writers, readers, iterations = 4, 4, 500
var wg sync.WaitGroup
wg.Add(writers + readers)
for range writers {
go func() {
defer wg.Done()
for range iterations {
_, _ = lw.Write([]byte("2024/01/01 00:00:00.000000 [Info] connection accepted"))
}
}()
}
for range readers {
go func() {
defer wg.Done()
for range iterations {
_ = lw.LastLine()
}
}()
}
wg.Wait()
}