How to use Run method of grpc Package

Best Venom code snippet using grpc.Run

tracinginterceptor_test.go

Source:tracinginterceptor_test.go Github

copy

Full Screen

...112 },113 }114 for _, test := range tests {115 test := test116 t.Run(test.name, func(t *testing.T) {117 t.Parallel()118 var wg sync.WaitGroup119 wg.Add(1)120 cc := new(grpc.ClientConn)121 stream, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",122 func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,123 opts ...grpc.CallOption) (grpc.ClientStream, error) {124 defer wg.Done()125 return &mockedClientStream{126 err: errors.New("dummy"),127 }, nil128 })129 wg.Wait()130 assert.Nil(t, err)131 cs := stream.(*clientStream)132 cs.sendStreamEvent(test.event, status.Error(codes.DataLoss, "dummy"))133 <-cs.eventsDone134 cs.sendStreamEvent(test.event, test.err)135 assert.NotNil(t, cs.CloseSend())136 })137 }138}139func TestStreamTracingInterceptor_WithError(t *testing.T) {140 tests := []struct {141 name string142 err error143 }{144 {145 name: "normal error",146 err: errors.New("dummy"),147 },148 {149 name: "grpc error",150 err: status.Error(codes.DataLoss, "dummy"),151 },152 }153 for _, test := range tests {154 test := test155 t.Run(test.name, func(t *testing.T) {156 t.Parallel()157 var run int32158 var wg sync.WaitGroup159 wg.Add(1)160 cc := new(grpc.ClientConn)161 _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",162 func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,163 opts ...grpc.CallOption) (grpc.ClientStream, error) {164 defer wg.Done()165 atomic.AddInt32(&run, 1)166 return new(mockedClientStream), test.err167 })168 wg.Wait()169 assert.NotNil(t, err)170 assert.Equal(t, int32(1), atomic.LoadInt32(&run))171 })172 }173}174func TestUnaryTracingInterceptor_GrpcFormat(t *testing.T) {175 var run int32176 var wg sync.WaitGroup177 wg.Add(1)178 cc := new(grpc.ClientConn)179 err := UnaryTracingInterceptor(context.Background(), "/foo", nil, nil, cc,180 func(ctx context.Context, method string, req, reply interface{}, cc *grpc.ClientConn,181 opts ...grpc.CallOption) error {182 defer wg.Done()183 atomic.AddInt32(&run, 1)184 return nil185 })186 wg.Wait()187 assert.Nil(t, err)188 assert.Equal(t, int32(1), atomic.LoadInt32(&run))189}190func TestStreamTracingInterceptor_GrpcFormat(t *testing.T) {191 var run int32192 var wg sync.WaitGroup193 wg.Add(1)194 cc := new(grpc.ClientConn)195 _, err := StreamTracingInterceptor(context.Background(), nil, cc, "/foo",196 func(ctx context.Context, desc *grpc.StreamDesc, cc *grpc.ClientConn, method string,197 opts ...grpc.CallOption) (grpc.ClientStream, error) {198 defer wg.Done()199 atomic.AddInt32(&run, 1)200 return nil, nil201 })202 wg.Wait()203 assert.Nil(t, err)204 assert.Equal(t, int32(1), atomic.LoadInt32(&run))205}206func TestClientStream_RecvMsg(t *testing.T) {207 tests := []struct {208 name string209 serverStreams bool210 err error211 }{212 {213 name: "nil error",214 },215 {216 name: "EOF",217 err: io.EOF,218 },219 {220 name: "dummy error",221 err: errors.New("dummy"),222 },223 {224 name: "server streams",225 serverStreams: true,226 },227 }228 for _, test := range tests {229 test := test230 t.Run(test.name, func(t *testing.T) {231 t.Parallel()232 desc := new(grpc.StreamDesc)233 desc.ServerStreams = test.serverStreams234 stream := wrapClientStream(context.Background(), &mockedClientStream{235 md: nil,236 err: test.err,237 }, desc)238 assert.Equal(t, test.err, stream.RecvMsg(nil))239 })240 }241}242func TestClientStream_Header(t *testing.T) {243 tests := []struct {244 name string245 err error246 }{247 {248 name: "nil error",249 },250 {251 name: "with error",252 err: errors.New("dummy"),253 },254 }255 for _, test := range tests {256 test := test257 t.Run(test.name, func(t *testing.T) {258 t.Parallel()259 desc := new(grpc.StreamDesc)260 stream := wrapClientStream(context.Background(), &mockedClientStream{261 md: metadata.MD{},262 err: test.err,263 }, desc)264 _, err := stream.Header()265 assert.Equal(t, test.err, err)266 })267 }268}269func TestClientStream_SendMsg(t *testing.T) {270 tests := []struct {271 name string272 err error273 }{274 {275 name: "nil error",276 },277 {278 name: "with error",279 err: errors.New("dummy"),280 },281 }282 for _, test := range tests {283 test := test284 t.Run(test.name, func(t *testing.T) {285 t.Parallel()286 desc := new(grpc.StreamDesc)287 stream := wrapClientStream(context.Background(), &mockedClientStream{288 md: metadata.MD{},289 err: test.err,290 }, desc)291 assert.Equal(t, test.err, stream.SendMsg(nil))292 })293 }294}295type mockedClientStream struct {296 md metadata.MD297 err error298}...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

1package main2import (3 "api-skeleton/app/Global"4 "api-skeleton/bootstrap"5 "api-skeleton/grpc/GrpcRoutes"6 "context"7 "fmt"8 "github.com/grpc-ecosystem/grpc-gateway/runtime"9 "golang.org/x/net/http2"10 "golang.org/x/net/http2/h2c"11 "google.golang.org/grpc"12 "google.golang.org/grpc/reflection"13 "net/http"14 "strings"15)16/**17Grpc 服务端18*/19func main() {20 bootstrap.InitConfig()21 bootstrap.InitDB()22 httpMux := runHttpServer()23 grpcS := runGrpcServer()24 gatewayMux := runGrpcGatewayServer()25 //dir, _ := os.Getwd()26 //severPemPath := path.Dir(dir+"/grpc/Keys/") + "/server.pem"27 //severKeyPath := path.Dir(dir+"/grpc/Keys/") + "/server.key"28 httpMux.Handle("/", gatewayMux)29 fmt.Println("启动grpc和http同端口双流量服务")30 err := http.ListenAndServe(":"+Global.Configs.Grpc.Port, grpcHandlerFunc(grpcS, httpMux))31 //监听tls32 //err := http.ListenAndServeTLS(":"+Global.Configs.Grpc.Port, severPemPath, severKeyPath, grpcHandlerFunc(grpcS, httpMux))33 if err != nil {34 panic(fmt.Sprintf("启动grpc http同端口双流量服务异常:%s", err))35 }36}37//runGrpcServer 启动注册grpc服务38func runGrpcServer() *grpc.Server {39 //Grpc 新增Tls配置40 //tls与ssl 一样需要配置读取证书路径,本地签发无效当前方式可以链接41 //dir, _ := os.Getwd()42 //severPemPath := path.Dir(dir+"/grpc/Keys/") + "/server.pem"43 //severKeyPath := path.Dir(dir+"/grpc/Keys/") + "/server.key"44 //cred, err := credentials.45 // NewServerTLSFromFile(severPemPath, severKeyPath)46 //if err != nil {47 // panic(fmt.Sprintf("配置Tls启动rpc 服务端异常:%s", err))48 //}49 //grpcServer := grpc.NewServer(grpc.Creds(cred))50 grpcServer := grpc.NewServer()51 GrpcRoutes.RegisterGrpcServer(grpcServer)52 reflection.Register(grpcServer)53 return grpcServer54}55//runHttpServer 注册http1.0客户端服务56func runHttpServer() *http.ServeMux {57 serveMux := http.NewServeMux()58 serveMux.HandleFunc("/ping", func(w http.ResponseWriter, r *http.Request) {59 _, _ = w.Write([]byte(`pong`))60 })61 return serveMux62}63//runGrpcGatewayServer 注册grpc基于http1.0网关服务64func runGrpcGatewayServer() *runtime.ServeMux {65 ctx := context.Background()66 endpoint := fmt.Sprintf("%s:%s", Global.Configs.Grpc.Host, Global.Configs.Grpc.Port)67 gwmux := runtime.NewServeMux()68 //dir, _ := os.Getwd()69 //severPemPath := path.Dir(dir+"/grpc/Keys/") + "/server.pem"70 ////serverName 生产pem文件时定义的CommonName71 //cred, err := credentials.72 // NewClientTLSFromFile(severPemPath, "ApiSkeleton")73 //if err != nil {74 // panic(fmt.Sprintf("配置Tls启动rpc 客户端异常:%s", err))75 //}76 dopts := []grpc.DialOption{77 grpc.WithInsecure(), //忽略tls认证78 //grpc.WithTimeout(time.Second * 3),79 //grpc.WithTransportCredentials(cred),//开启tls认证80 }81 //绑定grpc http路由82 GrpcRoutes.RegisterGrpcClient(ctx, gwmux, endpoint, dopts)83 return gwmux84}85func grpcHandlerFunc(grpcServer *grpc.Server, otherHandler http.Handler) http.Handler {86 return h2c.NewHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {87 if r.ProtoMajor == 2 && strings.Contains(r.Header.Get("Content-Type"), "application/grpc") {88 grpcServer.ServeHTTP(w, r)89 } else {90 otherHandler.ServeHTTP(w, r)91 }92 }), &http2.Server{})93}...

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1func main() {2 conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())3 if err != nil {4 log.Fatalf("did not connect: %v", err)5 }6 defer conn.Close()7 c := pb.NewGreeterClient(conn)8 if len(os.Args) > 1 {9 }10 ctx, cancel := context.WithTimeout(context.Background(), time.Second)11 defer cancel()12 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})13 if err != nil {14 log.Fatalf("could not greet: %v", err)15 }16 log.Printf("Greeting: %s", r.Message)17}18func main() {19 conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())20 if err != nil {21 log.Fatalf("did not connect: %v", err)22 }23 defer conn.Close()24 c := pb.NewGreeterClient(conn)25 if len(os.Args) > 1 {26 }27 ctx, cancel := context.WithTimeout(context.Background(), time.Second)28 defer cancel()29 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})30 if err != nil {31 log.Fatalf("could not greet: %v", err)32 }33 log.Printf("Greeting: %s", r.Message)34}35func main() {36 conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())37 if err != nil {38 log.Fatalf("did not connect: %v", err)39 }40 defer conn.Close()

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1func main() {2 conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())3 if err != nil {4 log.Fatalf("did not connect: %v", err)5 }6 defer conn.Close()7 c := pb.NewGreeterClient(conn)8 if len(os.Args) > 1 {9 }10 ctx, cancel := context.WithTimeout(context.Background(), time.Second)11 defer cancel()12 r, err := c.SayHello(ctx, &pb.HelloRequest{Name: name})13 if err != nil {14 log.Fatalf("could not greet: %v", err)15 }16 log.Printf("Greeting: %s", r.Message)17}18func main() {19 conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())20 if err != nil {21 log.Fatalf("did not connect: %v", err)22 }23 defer conn.Close()24 c := pb.NewGreeterClient(conn)25 if len(os.Args) > 1 {26 }27 ctx, cancel := context.WithTimeout(context.Background(), time.Second)28 defer cancel()29 r, err := c.SayHelloAgain(ctx, &pb.HelloRequest{Name: name})30 if err != nil {31 log.Fatalf("could not greet: %v", err)32 }33 log.Printf("Greeting: %s", r.Message)34}35func main() {36 conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure())37 if err != nil {38 log.Fatalf("did not connect: %v", err)39 }40 defer conn.Close()41 c := pb.NewGreeterClient(conn)42 if len(os.Args) > 1 {43 }

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 creds, err := credentials.NewClientTLSFromFile("certs/server.crt", "localhost")4 if err != nil {5 log.Fatalf("Failed to create TLS credentials %v", err)6 }7 conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds))8 if err != nil {9 log.Fatalf("could not connect: %v", err)10 }11 defer conn.Close()12 client := pb.NewGreetServiceClient(conn)13 doUnary(client)14}15import (16func main() {17 creds, err := credentials.NewClientTLSFromFile("certs/server.crt", "localhost")18 if err != nil {19 log.Fatalf("Failed to create TLS credentials %v", err)20 }21 conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds))22 if err != nil {23 log.Fatalf("could not connect: %v", err)24 }25 defer conn.Close()26 client := pb.NewGreetServiceClient(conn)27 doUnary(client)28}29import (30func main() {31 creds, err := credentials.NewClientTLSFromFile("certs/server.crt", "localhost")32 if err != nil {33 log.Fatalf("Failed to create TLS credentials %v", err)34 }35 conn, err := grpc.Dial("localhost:50051", grpc.WithTransportCredentials(creds))36 if err != nil {37 log.Fatalf("could not connect: %v", err)38 }39 defer conn.Close()40 client := pb.NewGreetServiceClient(conn)41 doUnary(client)42}43import (44func main() {45 creds, err := credentials.NewClientTLSFromFile("certs/server.crt", "localhost")46 if err != nil {47 log.Fatalf("Failed to create TLS credentials %v", err)48 }49 conn, err := grpc.Dial("

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2const (3func main() {4 conn, err := grpc.Dial(address, grpc.WithInsecure())5 if err != nil {6 log.Fatalf("did not connect: %v", err)7 }8 defer conn.Close()9 c := pb.NewGreeterClient(conn)10 r, err := c.SayHello(context.Background(), &pb.HelloRequest{Name: name})11 if err != nil {12 log.Fatalf("could not greet: %v", err)13 }14 log.Printf("Greeting: %s", r.Message)15}16import (17const (18type server struct {19}20func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {21 return &pb.HelloReply{Message: "Hello " + in.Name}, nil22}23func main() {24 lis, err := net.Listen("tcp", port)25 if err != nil {26 log.Fatalf("failed to listen: %v", err)27 }28 s := grpc.NewServer()29 pb.RegisterGreeterServer(s, &server{})30 if err := s.Serve(lis); err != nil {31 log.Fatalf("failed to serve: %v", err)32 }33}34syntax = "proto3";35package helloworld;36service Greeter {37 rpc SayHello (HelloRequest) returns (HelloReply) {}38}39message HelloRequest {40 string name = 1;41}42message HelloReply {

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1func main() {2}3func main() {4}5func main() {6}7func main() {8}9func main() {10}11func main() {12}13func main() {14}15func main() {16}17func main() {18}19func main() {20}21func main() {22}23func main()

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2const (3func main() {4 conn, err := grpc.Dial(address, grpc.WithInsecure(), grpc.WithBlock())5 if err != nil {6 log.Fatalf("did not connect: %v", err)7 }8 defer conn.Close()9 c := pb.NewHelloServiceClient(conn)10 ctx, cancel := context.WithTimeout(context.Background(), time.Second)11 defer cancel()12 r, err := c.Run(ctx, &pb.HelloRequest{Name: name})13 if err != nil {14 log.Fatalf("could not greet: %v", err)15 }16 fmt.Println(r.GetMessage())17}18import (19const (20type server struct {21}22func (s *server) Run(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {23 return &pb.HelloReply{Message: "Hello " + in.Name}, nil24}25func main() {26 lis, err := net.Listen("tcp", port)27 if err != nil {28 log.Fatalf("failed to listen: %v", err)29 }30 s := grpc.NewServer()31 pb.RegisterHelloServiceServer(s, &server{})32 if err := s.Serve(lis); err != nil {33 log.Fatalf("failed to serve: %v", err)34 }35}

Full Screen

Full Screen

Run

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 lis, err := net.Listen("tcp", ":50051")4 if err != nil {5 log.Fatalf("failed to listen: %v", err)6 }7 s := grpc.NewServer()8 pb.RegisterGreeterServer(s, &server{})9 if err := s.Serve(lis); err != nil {10 log.Fatalf("failed to serve: %v", err)11 }12}13import (14type server struct{}15func (s *server) SayHello(ctx context.Context, in *pb.HelloRequest) (*pb.HelloReply, error) {16 return &pb.HelloReply{Message: "Hello " + in.Name}, nil17}18syntax = "proto3";19package helloworld;20option go_package = "github.com/krishna-aj/GoLang/grpc/protos";21message HelloRequest {22 string name = 1;23}24message HelloReply {25 string message = 1;26}27service Greeter {28 rpc SayHello (HelloRequest) returns (HelloReply) {}29}30import (31const (32func main() {33 conn, err := grpc.Dial(address, grpc.WithInsecure())34 if err != nil {35 log.Fatalf("did not connect: %v", err)36 }37 defer conn.Close()38 c := pb.NewGreeterClient(conn)39 if len(os.Args) > 1 {40 }41 r, err := c.SayHello(context.Background(), &pb.H

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Venom automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful