Commit 67d0445c authored by Rémy Oudompheng's avatar Rémy Oudompheng

log/syslog: fix race in test between channel close and accept.

Fixes #4769.

R=golang-dev, dave, adg, bradfitz
CC=fullung, golang-dev
https://golang.org/cl/7322089
parent e2f9e816
...@@ -51,7 +51,6 @@ func runStreamSyslog(l net.Listener, done chan<- string, wg *sync.WaitGroup) { ...@@ -51,7 +51,6 @@ func runStreamSyslog(l net.Listener, done chan<- string, wg *sync.WaitGroup) {
var c net.Conn var c net.Conn
var err error var err error
if c, err = l.Accept(); err != nil { if c, err = l.Accept(); err != nil {
fmt.Print(err)
return return
} }
wg.Add(1) wg.Add(1)
...@@ -71,7 +70,7 @@ func runStreamSyslog(l net.Listener, done chan<- string, wg *sync.WaitGroup) { ...@@ -71,7 +70,7 @@ func runStreamSyslog(l net.Listener, done chan<- string, wg *sync.WaitGroup) {
} }
} }
func startServer(n, la string, done chan<- string) (addr string, wg *sync.WaitGroup) { func startServer(n, la string, done chan<- string) (addr string, sock io.Closer, wg *sync.WaitGroup) {
if n == "udp" || n == "tcp" { if n == "udp" || n == "tcp" {
la = "127.0.0.1:0" la = "127.0.0.1:0"
} else { } else {
...@@ -95,6 +94,7 @@ func startServer(n, la string, done chan<- string) (addr string, wg *sync.WaitGr ...@@ -95,6 +94,7 @@ func startServer(n, la string, done chan<- string) (addr string, wg *sync.WaitGr
log.Fatalf("startServer failed: %v", e) log.Fatalf("startServer failed: %v", e)
} }
addr = l.LocalAddr().String() addr = l.LocalAddr().String()
sock = l
wg.Add(1) wg.Add(1)
go func() { go func() {
defer wg.Done() defer wg.Done()
...@@ -106,7 +106,12 @@ func startServer(n, la string, done chan<- string) (addr string, wg *sync.WaitGr ...@@ -106,7 +106,12 @@ func startServer(n, la string, done chan<- string) (addr string, wg *sync.WaitGr
log.Fatalf("startServer failed: %v", e) log.Fatalf("startServer failed: %v", e)
} }
addr = l.Addr().String() addr = l.Addr().String()
go runStreamSyslog(l, done, wg) sock = l
wg.Add(1)
go func() {
defer wg.Done()
runStreamSyslog(l, done, wg)
}()
} }
return return
} }
...@@ -117,7 +122,7 @@ func TestWithSimulated(t *testing.T) { ...@@ -117,7 +122,7 @@ func TestWithSimulated(t *testing.T) {
for _, tr := range transport { for _, tr := range transport {
done := make(chan string) done := make(chan string)
addr, _ := startServer(tr, "", done) addr, _, _ := startServer(tr, "", done)
if tr == "unix" || tr == "unixgram" { if tr == "unix" || tr == "unixgram" {
defer os.Remove(addr) defer os.Remove(addr)
} }
...@@ -137,8 +142,9 @@ func TestWithSimulated(t *testing.T) { ...@@ -137,8 +142,9 @@ func TestWithSimulated(t *testing.T) {
func TestFlap(t *testing.T) { func TestFlap(t *testing.T) {
net := "unix" net := "unix"
done := make(chan string) done := make(chan string)
addr, _ := startServer(net, "", done) addr, sock, _ := startServer(net, "", done)
defer os.Remove(addr) defer os.Remove(addr)
defer sock.Close()
s, err := Dial(net, addr, LOG_INFO|LOG_USER, "syslog_test") s, err := Dial(net, addr, LOG_INFO|LOG_USER, "syslog_test")
if err != nil { if err != nil {
...@@ -152,7 +158,8 @@ func TestFlap(t *testing.T) { ...@@ -152,7 +158,8 @@ func TestFlap(t *testing.T) {
check(t, msg, <-done) check(t, msg, <-done)
// restart the server // restart the server
startServer(net, addr, done) _, sock2, _ := startServer(net, addr, done)
defer sock2.Close()
// and try retransmitting // and try retransmitting
msg = "Moo 3" msg = "Moo 3"
...@@ -242,7 +249,8 @@ func TestWrite(t *testing.T) { ...@@ -242,7 +249,8 @@ func TestWrite(t *testing.T) {
} else { } else {
for _, test := range tests { for _, test := range tests {
done := make(chan string) done := make(chan string)
addr, _ := startServer("udp", "", done) addr, sock, _ := startServer("udp", "", done)
defer sock.Close()
l, err := Dial("udp", addr, test.pri, test.pre) l, err := Dial("udp", addr, test.pri, test.pre)
if err != nil { if err != nil {
t.Fatalf("syslog.Dial() failed: %v", err) t.Fatalf("syslog.Dial() failed: %v", err)
...@@ -263,7 +271,8 @@ func TestWrite(t *testing.T) { ...@@ -263,7 +271,8 @@ func TestWrite(t *testing.T) {
} }
func TestConcurrentWrite(t *testing.T) { func TestConcurrentWrite(t *testing.T) {
addr, _ := startServer("udp", "", make(chan string)) addr, sock, _ := startServer("udp", "", make(chan string))
defer sock.Close()
w, err := Dial("udp", addr, LOG_USER|LOG_ERR, "how's it going?") w, err := Dial("udp", addr, LOG_USER|LOG_ERR, "how's it going?")
if err != nil { if err != nil {
t.Fatalf("syslog.Dial() failed: %v", err) t.Fatalf("syslog.Dial() failed: %v", err)
...@@ -289,7 +298,7 @@ func TestConcurrentReconnect(t *testing.T) { ...@@ -289,7 +298,7 @@ func TestConcurrentReconnect(t *testing.T) {
net := "unix" net := "unix"
done := make(chan string) done := make(chan string)
addr, srvWG := startServer(net, "", done) addr, sock, srvWG := startServer(net, "", done)
defer os.Remove(addr) defer os.Remove(addr)
// count all the messages arriving // count all the messages arriving
...@@ -327,6 +336,7 @@ func TestConcurrentReconnect(t *testing.T) { ...@@ -327,6 +336,7 @@ func TestConcurrentReconnect(t *testing.T) {
}() }()
} }
wg.Wait() wg.Wait()
sock.Close()
srvWG.Wait() srvWG.Wait()
close(done) close(done)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment