ref: 5646a8896664ecc31a858a514925b94c50848a76
server/grpc/grpc_test.go
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
package grpc import ( "context" "google.golang.org/grpc" "log" "testing" "time" "yats-server/proto" ) func TestGrpcCreateMetric(t *testing.T) { conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := proto.NewYatsGrpcServiceClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() item := &proto.Metric{IdClient: "DefaultIdClient", Name: "testMetric1", Mtime: time.Now().UnixMilli(), Value: "This is the value"} res, err := c.CreateMetric(ctx, &proto.CreateMetricRequest{Metric: item}) if err != nil { log.Fatalf("could not create metric: %v", err) } log.Printf("Metric created: %v", res.GetMetric()) } func TestGrpcCreateEvent(t *testing.T) { conn, err := grpc.Dial("localhost:50051", grpc.WithInsecure(), grpc.WithBlock()) if err != nil { log.Fatalf("did not connect: %v", err) } defer conn.Close() c := proto.NewYatsGrpcServiceClient(conn) ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() item := &proto.Event{IdClient: "DefaultIdClient", Name: "testEvent1", Etime: time.Now().UnixMilli()} res, err := c.CreateEvent(ctx, &proto.CreateEventRequest{Event: item}) if err != nil { log.Fatalf("could not create event: %v", err) } log.Printf("Event created: %v", res.GetEvent()) } |