New race detector finds more bugs in my crappy code :-)

This commit is contained in:
Alex Bramley 2014-09-05 15:39:56 +01:00
parent 7c53f41c56
commit 58eaab3f1f
2 changed files with 10 additions and 4 deletions

View File

@ -236,10 +236,12 @@ func (conn *Conn) postConnect(start bool) {
bufio.NewReader(conn.sock), bufio.NewReader(conn.sock),
bufio.NewWriter(conn.sock)) bufio.NewWriter(conn.sock))
if start { if start {
conn.wg.Add(3)
go conn.send() go conn.send()
go conn.recv() go conn.recv()
go conn.runLoop() go conn.runLoop()
if conn.cfg.PingFreq > 0 { if conn.cfg.PingFreq > 0 {
conn.wg.Add(1)
go conn.ping() go conn.ping()
} }
} }
@ -252,7 +254,6 @@ func hasPort(s string) bool {
// goroutine to pass data from output channel to write() // goroutine to pass data from output channel to write()
func (conn *Conn) send() { func (conn *Conn) send() {
conn.wg.Add(1)
defer conn.wg.Done() defer conn.wg.Done()
for { for {
select { select {
@ -267,7 +268,6 @@ func (conn *Conn) send() {
// receive one \r\n terminated line from peer, parse and dispatch it // receive one \r\n terminated line from peer, parse and dispatch it
func (conn *Conn) recv() { func (conn *Conn) recv() {
conn.wg.Add(1)
for { for {
s, err := conn.io.ReadString('\n') s, err := conn.io.ReadString('\n')
if err != nil { if err != nil {
@ -293,7 +293,6 @@ func (conn *Conn) recv() {
// Repeatedly pings the server every PingFreq seconds (no matter what) // Repeatedly pings the server every PingFreq seconds (no matter what)
func (conn *Conn) ping() { func (conn *Conn) ping() {
conn.wg.Add(1)
defer conn.wg.Done() defer conn.wg.Done()
tick := time.NewTicker(conn.cfg.PingFreq) tick := time.NewTicker(conn.cfg.PingFreq)
for { for {
@ -310,7 +309,6 @@ func (conn *Conn) ping() {
// goroutine to dispatch events for lines received on input channel // goroutine to dispatch events for lines received on input channel
func (conn *Conn) runLoop() { func (conn *Conn) runLoop() {
conn.wg.Add(1)
defer conn.wg.Done() defer conn.wg.Done()
for { for {
select { select {

View File

@ -170,6 +170,8 @@ func TestSend(t *testing.T) {
// We want to test that the a goroutine calling send will exit correctly. // We want to test that the a goroutine calling send will exit correctly.
exited := callCheck(t) exited := callCheck(t)
// send() will decrement the WaitGroup, so we must increment it.
c.wg.Add(1)
go func() { go func() {
c.send() c.send()
exited.call() exited.call()
@ -224,6 +226,8 @@ func TestRecv(t *testing.T) {
// We want to test that the a goroutine calling recv will exit correctly. // We want to test that the a goroutine calling recv will exit correctly.
exited := callCheck(t) exited := callCheck(t)
// recv() will decrement the WaitGroup, so we must increment it.
c.wg.Add(1)
go func() { go func() {
c.recv() c.recv()
exited.call() exited.call()
@ -282,6 +286,8 @@ func TestPing(t *testing.T) {
// Start ping loop. // Start ping loop.
exited := callCheck(t) exited := callCheck(t)
// ping() will decrement the WaitGroup, so we must increment it.
c.wg.Add(1)
go func() { go func() {
c.ping() c.ping()
exited.call() exited.call()
@ -349,6 +355,8 @@ func TestRunLoop(t *testing.T) {
// We want to test that the a goroutine calling runLoop will exit correctly. // We want to test that the a goroutine calling runLoop will exit correctly.
// Now, we can expect the call to Dispatch to take place as runLoop starts. // Now, we can expect the call to Dispatch to take place as runLoop starts.
exited := callCheck(t) exited := callCheck(t)
// runLoop() will decrement the WaitGroup, so we must increment it.
c.wg.Add(1)
go func() { go func() {
c.runLoop() c.runLoop()
exited.call() exited.call()