Skip to content

Commit

Permalink
ci: Enabled SA2002 staticcheck check
Browse files Browse the repository at this point in the history
And handle errors in the main test goroutine
  • Loading branch information
dnephin committed Jun 5, 2020
1 parent fed7489 commit caa692d
Show file tree
Hide file tree
Showing 10 changed files with 66 additions and 53 deletions.
4 changes: 0 additions & 4 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,6 @@ issues:
- linters: [staticcheck]
text: "SA4006:"

# Temp ignore SA2002: the goroutine calls T.Fatalf, which must be called in the same goroutine as the test
- linters: [staticcheck]
text: "SA2002:"

linters-settings:
gofmt:
simplify: false
Expand Down
37 changes: 24 additions & 13 deletions agent/cache-types/connect_ca_leaf_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,6 @@ func TestConnectCALeaf_CSRRateLimiting(t *testing.T) {
func TestConnectCALeaf_watchRootsDedupingMultipleCallers(t *testing.T) {
t.Parallel()

require := require.New(t)
rpc := TestRPC(t)
defer rpc.AssertExpectations(t)

Expand Down Expand Up @@ -737,8 +736,8 @@ func TestConnectCALeaf_watchRootsDedupingMultipleCallers(t *testing.T) {
// initial cert delivered and is blocking before the root changes. It's not a
// wait group since we want to be able to timeout the main test goroutine if
// one of the clients gets stuck. Instead it's a buffered chan.
setupDoneCh := make(chan struct{}, n)
testDoneCh := make(chan struct{}, n)
setupDoneCh := make(chan error, n)
testDoneCh := make(chan error, n)
// rootsUpdate is used to coordinate clients so they know when they should
// expect to see leaf renewed after root change.
rootsUpdatedCh := make(chan struct{})
Expand All @@ -755,7 +754,8 @@ func TestConnectCALeaf_watchRootsDedupingMultipleCallers(t *testing.T) {
fetchCh := TestFetchCh(t, typ, opts, req)
select {
case <-time.After(100 * time.Millisecond):
t.Fatal("shouldn't block waiting for fetch")
setupDoneCh <- fmt.Errorf("shouldn't block waiting for fetch")
return
case result := <-fetchCh:
v := mustFetchResult(t, result)
opts.LastResult = &v
Expand All @@ -766,33 +766,38 @@ func TestConnectCALeaf_watchRootsDedupingMultipleCallers(t *testing.T) {
fetchCh = TestFetchCh(t, typ, opts, req)
select {
case result := <-fetchCh:
t.Fatalf("should not return: %#v", result)
setupDoneCh <- fmt.Errorf("should not return: %#v", result)
return
case <-time.After(100 * time.Millisecond):
}

// We're done with setup and the blocking call is still blocking in
// background.
setupDoneCh <- struct{}{}
setupDoneCh <- nil

// Wait until all others are also done and roots change incase there are
// stragglers delaying the root update.
select {
case <-rootsUpdatedCh:
case <-time.After(200 * time.Millisecond):
t.Fatalf("waited too long for root update")
testDoneCh <- fmt.Errorf("waited too long for root update")
return
}

// Now we should see root update within a short period
select {
case <-time.After(100 * time.Millisecond):
t.Fatal("shouldn't block waiting for fetch")
testDoneCh <- fmt.Errorf("shouldn't block waiting for fetch")
return
case result := <-fetchCh:
v := mustFetchResult(t, result)
// Index must be different
require.NotEqual(opts.MinIndex, v.Value.(*structs.IssuedCert).CreateIndex)
if opts.MinIndex == v.Value.(*structs.IssuedCert).CreateIndex {
testDoneCh <- fmt.Errorf("index must be different")
return
}
}

testDoneCh <- struct{}{}
testDoneCh <- nil
}

// Sanity check the roots watcher is not running yet
Expand All @@ -808,7 +813,10 @@ func TestConnectCALeaf_watchRootsDedupingMultipleCallers(t *testing.T) {
select {
case <-timeoutCh:
t.Fatal("timed out waiting for clients")
case <-setupDoneCh:
case err := <-setupDoneCh:
if err != nil {
t.Fatalf(err.Error())
}
}
}

Expand Down Expand Up @@ -837,7 +845,10 @@ func TestConnectCALeaf_watchRootsDedupingMultipleCallers(t *testing.T) {
select {
case <-timeoutCh:
t.Fatalf("timed out waiting for %d of %d clients to renew after root change", n-i, n)
case <-testDoneCh:
case err := <-testDoneCh:
if err != nil {
t.Fatalf(err.Error())
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion agent/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -315,7 +315,7 @@ func TestCatalogNodes_Blocking(t *testing.T) {
}
var out struct{}
if err := a.RPC("Catalog.Register", args, &out); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()

Expand Down
5 changes: 3 additions & 2 deletions agent/consul/catalog_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1418,10 +1418,11 @@ func TestCatalog_ListServices_Blocking(t *testing.T) {
go func() {
time.Sleep(100 * time.Millisecond)
if err := s1.fsm.State().EnsureNode(idx+1, &structs.Node{Node: "foo", Address: "127.0.0.1"}); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
return
}
if err := s1.fsm.State().EnsureService(idx+2, "foo", &structs.NodeService{ID: "db", Service: "db", Tags: []string{"primary"}, Address: "127.0.0.1", Port: 5000}); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()

Expand Down
5 changes: 3 additions & 2 deletions agent/consul/kvs_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ func TestKVSEndpoint_List_Blocking(t *testing.T) {
}
var out bool
if err := msgpackrpc.CallWithCodec(codec, "KVS.Apply", &arg, &out); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("RPC call failed: %v", err)
}
}()

Expand Down Expand Up @@ -891,7 +891,8 @@ func TestKVS_Issue_1626(t *testing.T) {
}
var dirent structs.IndexedDirEntries
if err := msgpackrpc.CallWithCodec(codec, "KVS.Get", &getR, &dirent); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("RPC call failed: %v", err)
return
}
doneCh <- &dirent
}()
Expand Down
2 changes: 1 addition & 1 deletion agent/event_endpoint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -269,7 +269,7 @@ func TestEventList_Blocking(t *testing.T) {
time.Sleep(50 * time.Millisecond)
p := &UserEvent{Name: "second"}
if err := a.UserEvent("dc1", "root", p); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()

Expand Down
24 changes: 12 additions & 12 deletions api/kv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)

func TestAPI_ClientPutGetDelete(t *testing.T) {
Expand Down Expand Up @@ -246,16 +248,14 @@ func TestAPI_ClientWatchGet(t *testing.T) {

// Put the key
value := []byte("test")
doneCh := make(chan struct{})
doneCh := make(chan error)
go func() {
kv := c.KV()

time.Sleep(100 * time.Millisecond)
p := &KVPair{Key: key, Flags: 42, Value: value}
if _, err := kv.Put(p, nil); err != nil {
t.Fatalf("err: %v", err)
}
doneCh <- struct{}{}
_, err := kv.Put(p, nil)
doneCh <- err
}()

// Get should work
Expand All @@ -278,7 +278,8 @@ func TestAPI_ClientWatchGet(t *testing.T) {
}

// Block until put finishes to avoid a race between it and deferred s.Stop()
<-doneCh
err = <-doneCh
require.NoError(t, err)
}

func TestAPI_ClientWatchList(t *testing.T) {
Expand All @@ -304,16 +305,14 @@ func TestAPI_ClientWatchList(t *testing.T) {

// Put the key
value := []byte("test")
doneCh := make(chan struct{})
doneCh := make(chan error)
go func() {
kv := c.KV()

time.Sleep(100 * time.Millisecond)
p := &KVPair{Key: key, Flags: 42, Value: value}
if _, err := kv.Put(p, nil); err != nil {
t.Fatalf("err: %v", err)
}
doneCh <- struct{}{}
_, err := kv.Put(p, nil)
doneCh <- err
}()

// Get should work
Expand All @@ -336,7 +335,8 @@ func TestAPI_ClientWatchList(t *testing.T) {
}

// Block until put finishes to avoid a race between it and deferred s.Stop()
<-doneCh
err = <-doneCh
require.NoError(t, err)
}

func TestAPI_ClientKeys_DeleteRecurse(t *testing.T) {
Expand Down
8 changes: 5 additions & 3 deletions api/lock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,10 +189,12 @@ func TestAPI_LockContend(t *testing.T) {
// Should work eventually, will contend
leaderCh, err := lock.Lock(nil)
if err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
return
}
if leaderCh == nil {
t.Fatalf("not leader")
t.Errorf("not leader")
return
}
defer lock.Unlock()
log.Printf("Contender %d acquired", idx)
Expand Down Expand Up @@ -358,7 +360,7 @@ func TestAPI_LockReclaimLock(t *testing.T) {
go func() {
l2Ch, err := l2.Lock(nil)
if err != nil {
t.Fatalf("not locked: %v", err)
t.Errorf("not locked: %v", err)
}
reclaimed <- l2Ch
}()
Expand Down
6 changes: 4 additions & 2 deletions api/semaphore_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -184,10 +184,12 @@ func TestAPI_SemaphoreContend(t *testing.T) {
// Should work eventually, will contend
lockCh, err := sema.Acquire(nil)
if err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
return
}
if lockCh == nil {
t.Fatalf("not locked")
t.Errorf("not locked")
return
}
defer sema.Release()
log.Printf("Contender %d acquired", idx)
Expand Down
26 changes: 13 additions & 13 deletions api/watch/funcs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func TestKeyWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -194,7 +194,7 @@ func TestKeyWatch_With_PrefixDelete(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -259,7 +259,7 @@ func TestKeyPrefixWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -325,7 +325,7 @@ func TestServicesWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -399,7 +399,7 @@ func TestNodesWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -475,7 +475,7 @@ func TestServiceWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -545,7 +545,7 @@ func TestServiceMultipleTagsWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -637,7 +637,7 @@ func TestChecksWatch_State(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -713,7 +713,7 @@ func TestChecksWatch_Service(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -791,7 +791,7 @@ func TestEventWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -855,7 +855,7 @@ func TestConnectRootsWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -931,7 +931,7 @@ func TestConnectLeafWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down Expand Up @@ -1013,7 +1013,7 @@ func TestAgentServiceWatch(t *testing.T) {
go func() {
defer wg.Done()
if err := plan.Run(s.HTTPAddr); err != nil {
t.Fatalf("err: %v", err)
t.Errorf("err: %v", err)
}
}()
defer plan.Stop()
Expand Down

0 comments on commit caa692d

Please sign in to comment.