Skip to content

Commit

Permalink
Uncomment go client for order-service
Browse files Browse the repository at this point in the history
  • Loading branch information
llama90 committed Dec 22, 2020
1 parent 00480f1 commit 0f611f1
Showing 1 changed file with 90 additions and 90 deletions.
180 changes: 90 additions & 90 deletions grpc/grpc-up-and-running/ch03/order-service/go/client/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,95 +31,95 @@ func main() {
}

// // Get Order
// retrievedOrder , err := client.GetOrder(ctx, &wrapper.StringValue{Value: "106"})
// log.Print("GetOrder Response -> : ", retrievedOrder)

// // Search Order : Server streaming scenario
// searchStream, _ := client.SearchOrders(ctx, &wrapper.StringValue{Value: "Google"})
// for {
// searchOrder, err := searchStream.Recv()
// if err == io.EOF {
// log.Print("EOF")
// break
// }

// if err == nil {
// log.Print("Search Result : ", searchOrder)
// }
// }

// // =========================================
// // Update Orders : Client streaming scenario
// updOrder1 := pb.Order{Id: "102", Items:[]string{"Google Pixel 3A", "Google Pixel Book"}, Destination:"Mountain View, CA", Price:1100.00}
// updOrder2 := pb.Order{Id: "103", Items:[]string{"Apple Watch S4", "Mac Book Pro", "iPad Pro"}, Destination:"San Jose, CA", Price:2800.00}
// updOrder3 := pb.Order{Id: "104", Items:[]string{"Google Home Mini", "Google Nest Hub", "iPad Mini"}, Destination:"Mountain View, CA", Price:2200.00}

// updateStream, err := client.UpdateOrders(ctx)

// if err != nil {
// log.Fatalf("%v.UpdateOrders(_) = _, %v", client, err)
// }

// // Updating order 1
// if err := updateStream.Send(&updOrder1); err != nil {
// log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder1, err)
// }

// // Updating order 2
// if err := updateStream.Send(&updOrder2); err != nil {
// log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder2, err)
// }

// // Updating order 3
// if err := updateStream.Send(&updOrder3); err != nil {
// log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder3, err)
// }

// updateRes, err := updateStream.CloseAndRecv()
// if err != nil {
// log.Fatalf("%v.CloseAndRecv() got error %v, want %v", updateStream, err, nil)
// }
// log.Printf("Update Orders Res : %s", updateRes)

// // =========================================
// // Process Order : Bi-di streaming scenario
// streamProcOrder, err := client.ProcessOrders(ctx)
// if err != nil {
// log.Fatalf("%v.ProcessOrders(_) = _, %v", client, err)
// }

// if err := streamProcOrder.Send(&wrapper.StringValue{Value:"102"}); err != nil {
// log.Fatalf("%v.Send(%v) = %v", client, "102", err)
// }

// if err := streamProcOrder.Send(&wrapper.StringValue{Value:"103"}); err != nil {
// log.Fatalf("%v.Send(%v) = %v", client, "103", err)
// }

// if err := streamProcOrder.Send(&wrapper.StringValue{Value:"104"}); err != nil {
// log.Fatalf("%v.Send(%v) = %v", client, "104", err)
// }

// channel := make(chan struct{})
// go asncClientBidirectionalRPC(streamProcOrder, channel)
// time.Sleep(time.Millisecond * 1000)

// if err := streamProcOrder.Send(&wrapper.StringValue{Value:"101"}); err != nil {
// log.Fatalf("%v.Send(%v) = %v", client, "101", err)
// }
// if err := streamProcOrder.CloseSend(); err != nil {
// log.Fatal(err)
// }
// <- channel
retrievedOrder, err := client.GetOrder(ctx, &wrapper.StringValue{Value: "106"})
log.Print("GetOrder Response -> : ", retrievedOrder)

// Search Order : Server streaming scenario
searchStream, _ := client.SearchOrders(ctx, &wrapper.StringValue{Value: "Google"})
for {
searchOrder, err := searchStream.Recv()
if err == io.EOF {
log.Print("EOF")
break
}

if err == nil {
log.Print("Search Result : ", searchOrder)
}
}

// =========================================
// Update Orders : Client streaming scenario
updOrder1 := pb.Order{Id: "102", Items:[]string{"Google Pixel 3A", "Google Pixel Book"}, Destination:"Mountain View, CA", Price:1100.00}
updOrder2 := pb.Order{Id: "103", Items:[]string{"Apple Watch S4", "Mac Book Pro", "iPad Pro"}, Destination:"San Jose, CA", Price:2800.00}
updOrder3 := pb.Order{Id: "104", Items:[]string{"Google Home Mini", "Google Nest Hub", "iPad Mini"}, Destination:"Mountain View, CA", Price:2200.00}

updateStream, err := client.UpdateOrders(ctx)

if err != nil {
log.Fatalf("%v.UpdateOrders(_) = _, %v", client, err)
}

// Updating order 1
if err := updateStream.Send(&updOrder1); err != nil {
log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder1, err)
}

// Updating order 2
if err := updateStream.Send(&updOrder2); err != nil {
log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder2, err)
}

// Updating order 3
if err := updateStream.Send(&updOrder3); err != nil {
log.Fatalf("%v.Send(%v) = %v", updateStream, updOrder3, err)
}

updateRes, err := updateStream.CloseAndRecv()
if err != nil {
log.Fatalf("%v.CloseAndRecv() got error %v, want %v", updateStream, err, nil)
}
log.Printf("Update Orders Res : %s", updateRes)

// =========================================
// Process Order : Bi-di streaming scenario
streamProcOrder, err := client.ProcessOrders(ctx)
if err != nil {
log.Fatalf("%v.ProcessOrders(_) = _, %v", client, err)
}

if err := streamProcOrder.Send(&wrapper.StringValue{Value:"102"}); err != nil {
log.Fatalf("%v.Send(%v) = %v", client, "102", err)
}

if err := streamProcOrder.Send(&wrapper.StringValue{Value:"103"}); err != nil {
log.Fatalf("%v.Send(%v) = %v", client, "103", err)
}

if err := streamProcOrder.Send(&wrapper.StringValue{Value:"104"}); err != nil {
log.Fatalf("%v.Send(%v) = %v", client, "104", err)
}

channel := make(chan struct{})
go asncClientBidirectionalRPC(streamProcOrder, channel)
time.Sleep(time.Millisecond * 1000)

if err := streamProcOrder.Send(&wrapper.StringValue{Value:"101"}); err != nil {
log.Fatalf("%v.Send(%v) = %v", client, "101", err)
}
if err := streamProcOrder.CloseSend(); err != nil {
log.Fatal(err)
}
<- channel
}

//func asncClientBidirectionalRPC(streamProcOrder pb.OrderManagement_ProcessOrdersClient, c chan struct{}) {
// for {
// combinedShipment, errProcOrder := streamProcOrder.Recv()
// if errProcOrder == io.EOF {
// break
// }
// log.Printf("Combined shipment : ", combinedShipment.OrdersList)
// }
// <-c
//}
func asncClientBidirectionalRPC(streamProcOrder pb.OrderManagement_ProcessOrdersClient, c chan struct{}) {
for {
combinedShipment, errProcOrder := streamProcOrder.Recv()
if errProcOrder == io.EOF {
break
}
log.Printf("Combined shipment : ", combinedShipment.OrdersList)
}
<-c
}

0 comments on commit 0f611f1

Please sign in to comment.