Comments (2)
Thanks for the suggestion.
I think you can do this with a more general function instead. The function would accept a *grpc.Server
that has been created by the test with the right pb.Regster*
functions already called on it. It would create the bufconn
listener and return a ClientConn
connected to it. Then the test would do client := pb.FooClient(cc)
.
I.e.
package bufconntesting
func StartBufConnService(s *grpc.Server) (*grpc.ClientConn, error) {
// (mostly just your code above:)
t.Helper()
lis := bufconn.Listen(1 << 10)
go func() {
if err := srv.Serve(lis); err != nil {
t.Logf("service exited with error: %v", err)
}
}()
t.Cleanup(func() {
srv.GracefulStop()
lis.Close()
})
return grpc.NewClient(
"bufnet",
grpc.WithTransportCredentials(insecure.NewCredentials()),
grpc.WithContextDialer(func(ctx context.Context, _ string) (net.Conn, error) {
return lis.DialContext(ctx)
}),
)
require.NoError(t, err)
}
---
package some_test
func TestFoo(t *testing.T) {
s := grpc.NewServer(<whatever opts this test case likes>)
mypb.RegisterFooService(s)
cc, err := StartBufConnService(s)
if err != nil { t.Fatal() }
client := pb.FooClient(cc)
// use client
}
One problem you'll run into is that if a test case wants custom dial options, now you'll need to pass those in, too. (I avoided that problem by making the test create the server.) This is why I'm generally not a fan of testing helpers like this, although we do use them to some extent in our own tests. This and other opinions that you have in the code (e.g. to simply log the error if Serve
exits with one, vs. calling t.Error
) are reasons why we can't really be in the business of standardizing this kind of thing.
from grpc-go.
Yeah, fair enough!
from grpc-go.
Related Issues (20)
- Flaky test: TestClientSendsAGoAway HOT 8
- Catching grpc connection configuration errors at startup HOT 2
- CloseSend immediately after Send
- Minor 'invalid code' error output suggestion
- advancedtls package does not set VerifiedChains in TLSInfo.State HOT 5
- question: Bidirectional Streaming get error: rpc error: code = `Canceled` desc = `context canceled` HOT 6
- Delayed dns resolve after upgrade to v1.60.0 HOT 5
- channelz: SubChannel do not report their Target properly since 1.63 HOT 1
- xds: support LRS for all cluster types HOT 4
- Updating credentials of a running server HOT 5
- clusterimpl: update picker synchronously upon receipt of configuration update HOT 1
- cds: update picker synchronously upon receipt of configuration update
- rls: update picker synchronously upon receipt of configuration update
- Improve grpc-go/Documentation to clearly specify usage of `grpc.SetTrailer` function for adding metadata to send to client
- New ServerOption to set cancel insensitive "values" context. HOT 1
- Return the actual error from unary RPCs when `SendMsg()` returns `io.EOF` HOT 10
- protoc-gen-go-grpc: copy service comment to interfaces HOT 2
- Deprecate Dial and DialContext through a mechanism that doesn't trigger linters HOT 1
- Flaky test: 6/100K: Test/LRSClient HOT 2
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from grpc-go.