Best K6 code snippet using grpcext.Send
reflect.go
Source:reflect.go
...83// which is too much in the case the requirement is to just make a reflection's request.84// It makes the API more restricted and with a controlled surface,85// in this way the testing should be easier also.86type sendReceiver interface {87 Send(*reflectpb.ServerReflectionRequest) error88 Recv() (*reflectpb.ServerReflectionResponse, error)89}90// sendReceive sends a request to a reflection client and,91// receives a response.92func sendReceive(93 client sendReceiver,94 req *reflectpb.ServerReflectionRequest,95) (*reflectpb.ServerReflectionResponse, error) {96 if err := client.Send(req); err != nil {97 return nil, fmt.Errorf("can't send request: %w", err)98 }99 resp, err := client.Recv()100 if err != nil {101 return nil, fmt.Errorf("can't receive response: %w", err)102 }103 return resp, nil104}105type fileDescriptorLookupKey struct {106 Package string107 Name string108}...
access_test.go
Source:access_test.go
...26func (s *memorySink) Sync() error { return nil }27type tService struct {28 pingpong.UnimplementedPingPongServer29}30func (s *tService) SendPing(ctx context.Context, req *pingpong.Ping) (*pingpong.Pong, error) {31 AddToLog(ctx, bag.Field{32 Key: "token",33 Value: "abcd",34 })35 if req.Message == "return error" {36 return nil, status.Error(codes.Unknown, "pong error")37 }38 return &pingpong.Pong{Message: "pong"}, nil39}40type accessSuite struct {41 suite.Suite42 listener *bufconn.Listener43 gRPCServer *grpc.Server44 pingPongServer pingpong.PingPongServer45 sink *memorySink46}47func newAccessSuite() *accessSuite {48 return &accessSuite{49 sink: &memorySink{new(bytes.Buffer)},50 }51}52func (s *accessSuite) bufDialer(context.Context, string) (net.Conn, error) {53 return s.listener.Dial()54}55func (s *accessSuite) SetupSuite() {56 // Define a new scheme to write into memory sink.57 err := zap.RegisterSink("memory", func(*url.URL) (zap.Sink, error) {58 return s.sink, nil59 })60 require.Nil(s.T(), err)61}62func (s *accessSuite) SetupTest() {63 accessLogger := NewLogger(zlog.New(s.sink))64 s.gRPCServer = grpc.NewServer(grpc.UnaryInterceptor(UnaryServerInterceptor(accessLogger)))65 s.pingPongServer = &tService{}66 s.listener = bufconn.Listen(bufSize)67 pingpong.RegisterPingPongServer(s.gRPCServer, s.pingPongServer)68 go func() {69 err := s.gRPCServer.Serve(s.listener)70 assert.Nil(s.T(), err)71 }()72}73func (s *accessSuite) AfterTest(_, _ string) {74 s.gRPCServer.GracefulStop()75 s.sink.Reset()76}77func (s *accessSuite) TestUnaryServerInterceptor_OkReply() {78 t := s.T()79 ctx := context.Background()80 conn, err := grpc.DialContext(ctx, "bufnet", grpc.WithContextDialer(s.bufDialer), grpc.WithInsecure())81 require.Nil(t, err)82 defer conn.Close()83 client := pingpong.NewPingPongClient(conn)84 req := &pingpong.Ping{}85 _, err = client.SendPing(ctx, req)86 require.Nil(t, err)87 entry := s.sink.String()88 assert.Contains(t, entry, `"grpc.method":"SendPing"`)89 assert.Contains(t, entry, `"grpc.code":"OK"`)90 assert.Contains(t, entry, "grpc.start_time")91 assert.Contains(t, entry, "grpc.duration_ms")92 assert.Contains(t, entry, `"token":"abcd"`)93}94func (s *accessSuite) TestUnaryServerInterceptor_ErrReply() {95 t := s.T()96 conn, err := grpc.DialContext(context.Background(), "bufnet", grpc.WithContextDialer(s.bufDialer), grpc.WithInsecure())97 require.Nil(t, err)98 defer conn.Close()99 client := pingpong.NewPingPongClient(conn)100 req := &pingpong.Ping{101 Message: "return error",102 }103 _, err = client.SendPing(context.Background(), req)104 require.NotNil(t, err)105 entry := s.sink.String()106 assert.Contains(t, entry, `"grpc.method":"SendPing"`)107 assert.Contains(t, entry, `"grpc.code":"Unknown"`)108 assert.Contains(t, entry, "grpc.start_time")109 assert.Contains(t, entry, "grpc.duration_ms")110 assert.Contains(t, entry, `error":"rpc error: code = Unknown desc = pong error"`)111 assert.Contains(t, entry, `"token":"abcd"`)112}113func TestAccessLogInterceptor(t *testing.T) {114 suite.Run(t, newAccessSuite())115}...
chat.go
Source:chat.go
...39// Exports returns the exports of the module.40func (mi *ModuleInstance) Exports() modules.Exports {41 return modules.Exports{42 Named: map[string]interface{}{43 "send": mi.OpenAndSend,44 },45 }46}47func (mi *ModuleInstance) OpenAndSend(addr string, method string, payload goja.Value) (*grpcext.Response, error) {48 ctx, cancel := context.WithTimeout(mi.vu.Context(), 5*time.Second)49 defer cancel()50 notls := grpc.WithTransportCredentials(insecure.NewCredentials())51 conn, err := grpcext.Dial(ctx, addr, notls)52 if err != nil {53 return nil, fmt.Errorf("dialing failed: %w", err)54 }55 defer conn.Close()56 md, err := mi.methodDescriptor(ctx, conn, method)57 if err != nil {58 return nil, err59 }60 req := grpcext.Request{61 MethodDescriptor: md,...
Send
Using AI Code Generation
1import (2func main() {3 opts = append(opts, grpc.WithInsecure())4 opts = append(opts, grpc.WithBlock())5 ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)6 defer cancel()7 conn, err := grpc.DialContext(ctx, "localhost:8080", opts...)8 if err != nil {9 log.Fatalf("did not connect: %v", err)10 }11 defer conn.Close()12 client := examplepb.NewEchoServiceClient(conn)13 md := metadata.Pairs("username", "user1")14 ctx = metadata.NewOutgoingContext(context.Background(), md)15 r, err := client.Echo(ctx, &examplepb.EchoRequest{Message: "hello world"})16 if err != nil {17 log.Fatalf("could not greet: %v", err)18 }19 log.Printf("Greeting: %s", r.Message)20 r, err = client.Echo(ctx, &examplepb.EchoRequest{Message: "hello world"})21 if err != nil {22 log.Fatalf("could not greet: %v", err)23 }24 log.Printf("Greeting: %s", r.Message)25 r, err = client.Echo(ctx, &examplepb.EchoRequest{Message: "hello world"})26 if err != nil {27 log.Fatalf("could not greet: %v", err)28 }29 log.Printf("Greeting: %s", r.Message)30}31import (
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!