12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- package rpc
- import (
- "context"
- "log"
- "os"
- "testing"
- "time"
- "google.golang.org/grpc"
- "passport/app/rpc/pb"
- "passport/consul"
- )
- const (
- target = "consul://127.0.0.1:8500/im_passport"
- defaultName = "world"
- )
- func TestCallRpc(t *testing.T) {
- consul.Init()
- // Set up a connection to the server.
- ctx, _ := context.WithTimeout(context.Background(), 5*time.Second)
- conn, err := grpc.DialContext(ctx, target, grpc.WithBlock(), grpc.WithInsecure(), grpc.WithBalancerName("round_robin"))
- if err != nil {
- log.Fatalf("did not connect: %v", err)
- }
- defer conn.Close()
- c := passportpb.NewPassportClient(conn)
- // Contact the server and print out its response.
- name := defaultName
- if len(os.Args) > 1 {
- name = os.Args[1]
- }
- for {
- ctx, _ := context.WithTimeout(context.Background(), time.Second)
- r, err := c.SayHello(ctx, &passportpb.HelloRequest{Name: name})
- if err != nil {
- log.Fatalf("could not greet: %v", err)
- }
- log.Printf("Greeting: %s", r.Name)
- time.Sleep(time.Second * 2)
- }
- }
|