Skip to content

Commit

Permalink
fix race condition in pkg/prom/cluster/node_test.go (#463)
Browse files Browse the repository at this point in the history
gRPC service can't be registered after calling Serve on the gRPC server.
Since Serve was being called in a separate goroutine, it was a race
condition for whether it would fail.
  • Loading branch information
rfratto authored Mar 11, 2021
1 parent 12e1372 commit f86bda8
Showing 1 changed file with 3 additions and 11 deletions.
14 changes: 3 additions & 11 deletions pkg/prom/cluster/node_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,29 +150,21 @@ func Test_node_ApplyConfig(t *testing.T) {
waitAll(t, localReshard)
}

func testGRPCServer(t *testing.T) (*grpc.Server, net.Listener) {
// startNode launches srv as a gRPC server and registers it to the ring.
func startNode(t *testing.T, srv agentproto.ScrapingServiceServer) {
t.Helper()

l, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)

grpcServer := grpc.NewServer()
agentproto.RegisterScrapingServiceServer(grpcServer, srv)

go func() {
_ = grpcServer.Serve(l)
}()
t.Cleanup(func() { grpcServer.Stop() })

return grpcServer, l
}

// startNode launches srv as a gRPC server and registers it to the ring.
func startNode(t *testing.T, srv agentproto.ScrapingServiceServer) {
t.Helper()

grpcServer, l := testGRPCServer(t)
agentproto.RegisterScrapingServiceServer(grpcServer, srv)

lcConfig := testLifecyclerConfig(t)
lcConfig.Addr = l.Addr().(*net.TCPAddr).IP.String()
lcConfig.Port = l.Addr().(*net.TCPAddr).Port
Expand Down

0 comments on commit f86bda8

Please sign in to comment.