mirror of https://github.com/hashicorp/consul
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.pull/8961/head
parent
e101aa8a74
commit
df405ac978
|
@ -8,12 +8,13 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"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"
|
"github.com/stretchr/testify/require"
|
||||||
"golang.org/x/sync/errgroup"
|
"golang.org/x/sync/errgroup"
|
||||||
"google.golang.org/grpc"
|
"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 {
|
type testServer struct {
|
||||||
|
@ -40,16 +41,23 @@ func newTestServer(t *testing.T, name string, dc string) testServer {
|
||||||
|
|
||||||
g := errgroup.Group{}
|
g := errgroup.Group{}
|
||||||
g.Go(func() error {
|
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 {
|
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{
|
return testServer{
|
||||||
addr: lis.Addr(),
|
addr: lis.Addr(),
|
||||||
name: name,
|
name: name,
|
||||||
dc: dc,
|
dc: dc,
|
||||||
shutdown: func() {
|
shutdown: func() {
|
||||||
|
rpc.shutdown = true
|
||||||
if err := lis.Close(); err != nil {
|
if err := lis.Close(); err != nil {
|
||||||
t.Logf("listener closed with error: %v", err)
|
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)
|
t.Logf("grpc server shutdown: %v", err)
|
||||||
}
|
}
|
||||||
if err := g.Wait(); err != nil {
|
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 {
|
type fakeRPCListener struct {
|
||||||
t *testing.T
|
t *testing.T
|
||||||
handler *Handler
|
handler *Handler
|
||||||
|
shutdown bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *fakeRPCListener) listen(listener net.Listener) error {
|
func (f *fakeRPCListener) listen(listener net.Listener) error {
|
||||||
for {
|
for {
|
||||||
conn, err := listener.Accept()
|
conn, err := listener.Accept()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
if f.shutdown {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -30,7 +30,6 @@ func TestHandler_EmitsStats(t *testing.T) {
|
||||||
|
|
||||||
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
lis, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(logError(t, lis.Close))
|
|
||||||
|
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
t.Cleanup(cancel)
|
t.Cleanup(cancel)
|
||||||
|
@ -50,7 +49,7 @@ func TestHandler_EmitsStats(t *testing.T) {
|
||||||
|
|
||||||
conn, err := grpc.DialContext(ctx, lis.Addr().String(), grpc.WithInsecure())
|
conn, err := grpc.DialContext(ctx, lis.Addr().String(), grpc.WithInsecure())
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
t.Cleanup(logError(t, conn.Close))
|
t.Cleanup(func() { conn.Close() })
|
||||||
|
|
||||||
client := testservice.NewSimpleClient(conn)
|
client := testservice.NewSimpleClient(conn)
|
||||||
fClient, err := client.Flow(ctx, &testservice.Req{Datacenter: "mine"})
|
fClient, err := client.Flow(ctx, &testservice.Req{Datacenter: "mine"})
|
||||||
|
|
Loading…
Reference in New Issue