-
Notifications
You must be signed in to change notification settings - Fork 4.5k
grpctest: new package #2186
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
grpctest: new package #2186
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/* | ||
Package grpctest provides utilities for RPC testing. | ||
|
||
Call grpctest.NewServer to create a Server for a test; | ||
use its Addr field as the address to dial, or invoke its Dial method. | ||
*/ | ||
package grpctest | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"google.golang.org/grpc" | ||
) | ||
|
||
// A Server is an RPC server listening on a system-chosen port on the local loopback interface. | ||
type Server struct { | ||
GRPC *grpc.Server | ||
Addr string // address of test server, suitable for passing to grpc.Dial | ||
|
||
t *testing.T | ||
l net.Listener | ||
closing bool | ||
closed chan struct{} | ||
} | ||
|
||
// NewServer returns a new unstarted Server. | ||
// Attach RPC services to s.GRPC, then invoke s.Start to start the server. | ||
// The caller should call the Server's Close method when finished. | ||
func NewServer(t *testing.T) *Server { | ||
t.Helper() | ||
l, err := net.Listen("tcp", "127.0.0.1:0") | ||
if err != nil { | ||
// IPv4 might be unavailable. Try IPv6. | ||
t.Logf("net.Listen on 127.0.0.1:0: %v", err) | ||
l, err = net.Listen("tcp", "[::1]:0") | ||
if err != nil { | ||
t.Fatalf("net.Listen on [::1]:0: %v", err) | ||
} | ||
} | ||
s := &Server{ | ||
GRPC: grpc.NewServer(), | ||
Addr: l.Addr().String(), | ||
t: t, | ||
l: l, | ||
closed: make(chan struct{}), | ||
} | ||
return s | ||
} | ||
|
||
// Start starts the server. RPC services must be registered before this is called. | ||
func (s *Server) Start() { | ||
go func() { | ||
err := s.GRPC.Serve(s.l) | ||
if !s.closing && err != nil { | ||
s.t.Errorf("gRPC serve: %v", err) | ||
} | ||
close(s.closed) | ||
}() | ||
} | ||
|
||
// Close shuts down the server and refuses any new connections. | ||
// Existing RPC connections will stay open. | ||
func (s *Server) Close() { | ||
s.closing = true | ||
s.l.Close() | ||
<-s.closed | ||
} | ||
|
||
// Dial is like grpc.Dial(s.Addr) but with error handling abstracted. | ||
func (s *Server) Dial(opts ...grpc.DialOption) *grpc.ClientConn { | ||
s.t.Helper() | ||
opts = append([]grpc.DialOption{grpc.WithInsecure()}, opts...) | ||
|
||
conn, err := grpc.Dial(s.Addr, opts...) | ||
if err != nil { | ||
s.t.Fatalf("grpc.Dial(%q): %v", s.Addr, err) | ||
} | ||
return conn | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This uses and encourages an anti-pattern for Go testing, which is passing
testing.T
s around (which is particularly discouraged when crossing package boundaries and storing in structs AIUI).Further, I'm not convinced this package is worth the extra API surface it introduces. It is easy to start and stop a server/client with grpc. The main complexity is the
Listen
-on-localhost problem (which really should be solved in thenet
package itself, but that's irrelevant here). In the not-too-distant future (~1Q or so), we should be introducing an in-process transport that will be even easier to serve/dial.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's fine to pass around
testing.T
in limited circumstances. That's whyt.Helper
exists, after all. It solves the problems that passing around atesting.T
produces.It may be easy to start and stop a server for people like us who know what's going on, but lots of people aren't so comfortable. I see a lot of people getting it wrong in various ways (e.g. binding to
:0
instead of a loopback address, or starting the server before registering service handlers). It's not a lot of code, but it cuts down the error rate a lot.