agent/grpc: remove misleading warnings from test output

Handle shutdown properly in tests so that the tests don't warn about using a closed connection.
This commit is contained in:
Daniel Nephin 2020-10-14 13:21:51 -04:00
parent 64105079d9
commit 9b89fb492d
2 changed files with 21 additions and 10 deletions

View File

@ -8,12 +8,13 @@ import (
"testing"
"time"
"github.com/hashicorp/consul/agent/grpc/internal/testservice"
"github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/consul/agent/pool"
"github.com/stretchr/testify/require"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
"github.com/hashicorp/consul/agent/grpc/internal/testservice"
"github.com/hashicorp/consul/agent/metadata"
"github.com/hashicorp/consul/agent/pool"
)
type testServer struct {
@ -40,16 +41,23 @@ func newTestServer(t *testing.T, name string, dc string) testServer {
g := errgroup.Group{}
g.Go(func() error {
return rpc.listen(lis)
if err := rpc.listen(lis); err != nil {
return fmt.Errorf("fake rpc listen error: %w", err)
}
return nil
})
g.Go(func() error {
return handler.Run()
if err := handler.Run(); err != nil {
return fmt.Errorf("grpc server error: %w", err)
}
return nil
})
return testServer{
addr: lis.Addr(),
name: name,
dc: dc,
shutdown: func() {
rpc.shutdown = true
if err := lis.Close(); err != nil {
t.Logf("listener closed with error: %v", err)
}
@ -57,7 +65,7 @@ func newTestServer(t *testing.T, name string, dc string) testServer {
t.Logf("grpc server shutdown: %v", err)
}
if err := g.Wait(); err != nil {
t.Logf("grpc server error: %v", err)
t.Log(err)
}
},
}
@ -91,12 +99,16 @@ func (s *simple) Something(_ context.Context, _ *testservice.Req) (*testservice.
type fakeRPCListener struct {
t *testing.T
handler *Handler
shutdown bool
}
func (f *fakeRPCListener) listen(listener net.Listener) error {
for {
conn, err := listener.Accept()
if err != nil {
if f.shutdown {
return nil
}
return err
}

View File

@ -30,7 +30,6 @@ func TestHandler_EmitsStats(t *testing.T) {
lis, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
t.Cleanup(logError(t, lis.Close))
ctx, cancel := context.WithCancel(context.Background())
t.Cleanup(cancel)
@ -50,7 +49,7 @@ func TestHandler_EmitsStats(t *testing.T) {
conn, err := grpc.DialContext(ctx, lis.Addr().String(), grpc.WithInsecure())
require.NoError(t, err)
t.Cleanup(logError(t, conn.Close))
t.Cleanup(func() { conn.Close() })
client := testservice.NewSimpleClient(conn)
fClient, err := client.Flow(ctx, &testservice.Req{Datacenter: "mine"})