func (t *Arith) Div(args *msg.ArithRequest, reply *msg.ArithResponse) error { if args.GetB() == 0 { return errors.New("divide by zero") } reply.C = proto.Int32(args.GetA() / args.GetB()) return nil }
func testArithClient(t *testing.T, client *rpc.Client) { var args msg.ArithRequest var reply msg.ArithResponse var err error // Add args.A = proto.Int32(1) args.B = proto.Int32(2) if err = client.Call("ArithService.Add", &args, &reply); err != nil { t.Fatalf(`arith.Add: %v`, err) } if reply.GetC() != 3 { t.Fatalf(`arith.Add: expected = %d, got = %d`, 3, reply.GetC()) } // Mul args.A = proto.Int32(2) args.B = proto.Int32(3) if err = client.Call("ArithService.Mul", &args, &reply); err != nil { t.Fatalf(`arith.Mul: %v`, err) } if reply.GetC() != 6 { t.Fatalf(`arith.Mul: expected = %d, got = %d`, 6, reply.GetC()) } // Div args.A = proto.Int32(13) args.B = proto.Int32(5) if err = client.Call("ArithService.Div", &args, &reply); err != nil { t.Fatalf(`arith.Div: %v`, err) } if reply.GetC() != 2 { t.Fatalf(`arith.Div: expected = %d, got = %d`, 2, reply.GetC()) } // Div zero args.A = proto.Int32(1) args.B = proto.Int32(0) if err = client.Call("ArithService.Div", &args, &reply); err.Error() != "divide by zero" { t.Fatalf(`arith.Error: expected = "%s", got = "%s"`, "divide by zero", err.Error()) } // Error args.A = proto.Int32(1) args.B = proto.Int32(2) if err = client.Call("ArithService.Error", &args, &reply); err.Error() != "ArithError" { t.Fatalf(`arith.Error: expected = "%s", got = "%s"`, "ArithError", err.Error()) } }
func (t *Arith) Mul(args *msg.ArithRequest, reply *msg.ArithResponse) error { reply.C = proto.Int32(args.GetA() * args.GetB()) return nil }
func (t *Arith) Add(args *msg.ArithRequest, reply *msg.ArithResponse) error { reply.C = proto.Int32(args.GetA() + args.GetB()) log.Printf("Arith.Add(%v, %v): %v", args.GetA(), args.GetB(), reply.GetC()) return nil }