fix go vet warnings: call to (*T).Fatalf from a non-test goroutine

The goroutine started from test must not call t.Fatal, but t.Error. Adds
a sync.WaitGroup to make sure all goroutines have a chance to report an
error before test stops.
pull/167/head
Michal Vyskocil 2022-12-12 22:27:50 +01:00 committed by mattn
parent ae970802cf
commit 29bb16009b
2 changed files with 29 additions and 6 deletions

View File

@ -7,6 +7,7 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
) )
@ -29,11 +30,14 @@ event: delete
data: 1234567 data: 1234567
:thump :thump
`, largeContent)) `, largeContent))
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer wg.Done()
defer close(q) defer close(q)
err := handleReader(q, r) err := handleReader(q, r)
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Errorf("should not be fail: %v", err)
} }
}() }()
var passUpdate, passUpdateLarge, passNotification, passDelete, passError bool var passUpdate, passUpdateLarge, passNotification, passDelete, passError bool
@ -69,6 +73,7 @@ data: 1234567
"update: %t, update (large): %t, notification: %t, delete: %t, error: %t", "update: %t, update (large): %t, notification: %t, delete: %t, error: %t",
passUpdate, passUpdateLarge, passNotification, passDelete, passError) passUpdate, passUpdateLarge, passNotification, passDelete, passError)
} }
wg.Wait()
} }
func TestStreaming(t *testing.T) { func TestStreaming(t *testing.T) {
@ -148,11 +153,14 @@ func TestDoStreaming(t *testing.T) {
req = req.WithContext(ctx) req = req.WithContext(ctx)
q := make(chan Event) q := make(chan Event)
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer wg.Done()
defer close(q) defer close(q)
c.doStreaming(req, q) c.doStreaming(req, q)
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Errorf("should not be fail: %v", err)
} }
}() }()
var passError bool var passError bool
@ -167,6 +175,7 @@ func TestDoStreaming(t *testing.T) {
if !passError { if !passError {
t.Fatalf("have not passed through: error %t", passError) t.Fatalf("have not passed through: error %t", passError)
} }
wg.Wait()
} }
func TestStreamingUser(t *testing.T) { func TestStreamingUser(t *testing.T) {

View File

@ -4,6 +4,7 @@ import (
"context" "context"
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"sync"
"testing" "testing"
"time" "time"
@ -151,12 +152,16 @@ func TestStreamingWS(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("should not be fail: %v", err) t.Fatalf("should not be fail: %v", err)
} }
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer wg.Done()
e := <-q e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok { if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Fatalf("should be fail: %v", errorEvent.err) t.Errorf("should be fail: %v", errorEvent.err)
} }
}() }()
wg.Wait()
} }
func TestHandleWS(t *testing.T) { func TestHandleWS(t *testing.T) {
@ -183,10 +188,13 @@ func TestHandleWS(t *testing.T) {
q := make(chan Event) q := make(chan Event)
client := NewClient(&Config{}).NewWSClient() client := NewClient(&Config{}).NewWSClient()
var wg sync.WaitGroup
wg.Add(1)
go func() { go func() {
defer wg.Done()
e := <-q e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok { if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Fatalf("should be fail: %v", errorEvent.err) t.Errorf("should be fail: %v", errorEvent.err)
} }
}() }()
err := client.handleWS(context.Background(), ":", q) err := client.handleWS(context.Background(), ":", q)
@ -196,10 +204,12 @@ func TestHandleWS(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
cancel() cancel()
wg.Add(1)
go func() { go func() {
defer wg.Done()
e := <-q e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok { if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Fatalf("should be fail: %v", errorEvent.err) t.Errorf("should be fail: %v", errorEvent.err)
} }
}() }()
err = client.handleWS(ctx, "ws://"+ts.Listener.Addr().String(), q) err = client.handleWS(ctx, "ws://"+ts.Listener.Addr().String(), q)
@ -207,13 +217,17 @@ func TestHandleWS(t *testing.T) {
t.Fatalf("should be fail: %v", err) t.Fatalf("should be fail: %v", err)
} }
wg.Add(1)
go func() { go func() {
defer wg.Done()
e := <-q e := <-q
if errorEvent, ok := e.(*ErrorEvent); !ok { if errorEvent, ok := e.(*ErrorEvent); !ok {
t.Fatalf("should be fail: %v", errorEvent.err) t.Errorf("should be fail: %v", errorEvent.err)
} }
}() }()
client.handleWS(context.Background(), "ws://"+ts.Listener.Addr().String(), q) client.handleWS(context.Background(), "ws://"+ts.Listener.Addr().String(), q)
wg.Wait()
} }
func TestDialRedirect(t *testing.T) { func TestDialRedirect(t *testing.T) {