Reduce the number of data races to 11.

master
Robert Gerus 2015-12-09 01:58:11 +01:00
parent 643c307d07
commit 7fb66c985c
1 changed files with 9 additions and 7 deletions

View File

@ -42,12 +42,6 @@ var input = []Message{
var actualOutput []Message var actualOutput []Message
var actualInput []Message var actualInput []Message
func connWriter(c net.Conn) {
}
func connReader(c net.Conn) {
}
func fakeServer(t *testing.T) { func fakeServer(t *testing.T) {
ln, err := net.Listen("tcp", ":36667") ln, err := net.Listen("tcp", ":36667")
if err != nil { if err != nil {
@ -73,10 +67,10 @@ func fakeServer(t *testing.T) {
} }
}(conn) }(conn)
wg.Add(len(expectedOutput))
// reader // reader
go func(c net.Conn) { go func(c net.Conn) {
reader := bufio.NewReader(c) reader := bufio.NewReader(c)
wg.Add(len(expectedOutput))
for range expectedOutput { for range expectedOutput {
raw, err := reader.ReadString(delim) raw, err := reader.ReadString(delim)
if err != nil { if err != nil {
@ -103,6 +97,8 @@ func fakeServer(t *testing.T) {
} }
} }
var setupMutex sync.Mutex
func TestSetup(t *testing.T) { func TestSetup(t *testing.T) {
go fakeServer(t) go fakeServer(t)
@ -115,9 +111,13 @@ func TestSetup(t *testing.T) {
// since we tested a reconnect, we should expect actual results to be // since we tested a reconnect, we should expect actual results to be
// multipled // multipled
setupMutex.Lock()
actualExpectedOutput := append(expectedOutput, expectedOutput...) actualExpectedOutput := append(expectedOutput, expectedOutput...)
actualExpectedInput := append(input, input...) actualExpectedInput := append(input, input...)
setupMutex.Unlock()
setupMutex.Lock()
defer setupMutex.Unlock()
if fmt.Sprintf("%+v", actualExpectedOutput) != fmt.Sprintf("%+v", actualOutput) { if fmt.Sprintf("%+v", actualExpectedOutput) != fmt.Sprintf("%+v", actualOutput) {
t.Log("Expected output does not match actual output") t.Log("Expected output does not match actual output")
t.Logf("expected: %+v\n", actualExpectedOutput) t.Logf("expected: %+v\n", actualExpectedOutput)
@ -135,6 +135,8 @@ func TestSetup(t *testing.T) {
func fakeDispatcher(output func(Message), input Message) { func fakeDispatcher(output func(Message), input Message) {
// nullify Context as it isn't transmitted over the wire // nullify Context as it isn't transmitted over the wire
setupMutex.Lock()
defer setupMutex.Unlock()
input.Context = make(map[string]string) input.Context = make(map[string]string)
actualInput = append(actualInput, input) actualInput = append(actualInput, input)
} }