Giter VIP home page Giter VIP logo

Comments (2)

dfawley avatar dfawley commented on June 11, 2024 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.

coxley avatar coxley commented on June 11, 2024

Yeah, fair enough!

from grpc-go.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.