Exemplo n.º 1
0
func testNewServerRPC(t *testing.T, addr string) {
	client, err := protorpc.Dial("tcp", addr)
	if err != nil {
		t.Fatal("dialing", err)
	}
	defer client.Close()

	// Synchronous calls
	args := &Args{7, 8}
	reply := new(Reply)
	err = client.Call("newServer.Arith.Add", args, reply)
	if err != nil {
		t.Errorf("Add: expected no error but got string %q", err.Error())
	}
	if reply.C != args.A+args.B {
		t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
	}
}
Exemplo n.º 2
0
func TestProtoError(t *testing.T) {
	if runtime.GOOS == "plan9" {
		t.Skip("skipping test; see http://golang.org/issue/8908")
	}
	defer func() {
		err := recover()
		if err == nil {
			t.Fatal("no error")
		}
		if !strings.Contains("reading body proto: wrong wireType = 2 for field C", err.(error).Error()) {
			t.Fatal("expected proto', got", err)
		}
	}()
	protorpc.Register(new(S))

	listen, err := net.Listen("tcp", "127.0.0.1:0")
	if err != nil {
		panic(err)
	}
	go protorpc.Accept(listen)

	client, err := protorpc.Dial("tcp", listen.Addr().String())
	if err != nil {
		panic(err)
	}

	var reply Reply
	err = client.Call("S.Recv", nil, &reply)
	if err != nil {
		panic(err)
	}

	fmt.Printf("%#v\n", reply)
	client.Close()

	listen.Close()
}
Exemplo n.º 3
0
func dialDirect() (*protorpc.Client, error) {
	return protorpc.Dial("tcp", serverAddr)
}
Exemplo n.º 4
0
func testRPC(t *testing.T, addr string) {
	client, err := protorpc.Dial("tcp", addr)
	if err != nil {
		t.Fatal("dialing", err)
	}
	defer client.Close()

	// Synchronous calls
	log.Println("Synchronous calls")
	args := &Args{7, 8}
	reply := new(Reply)
	err = client.Call("Arith.Add", args, reply)
	if err != nil {
		t.Errorf("Add: expected no error but got string %q", err.Error())
	}
	if reply.C != args.A+args.B {
		t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
	}

	// Nonexistent method
	log.Println("Nonexistent method")
	args = &Args{7, 0}
	reply = new(Reply)
	err = client.Call("Arith.BadOperation", args, reply)
	// expect an error
	if err == nil {
		t.Error("BadOperation: expected error")
	} else if !strings.HasPrefix(err.Error(), "rpc: can't find method ") {
		t.Errorf("BadOperation: expected can't find method error; got %q", err)
	}

	// Unknown service
	log.Println("Unknown service")
	args = &Args{7, 8}
	reply = new(Reply)
	err = client.Call("Arith.Unknown", args, reply)
	if err == nil {
		t.Error("expected error calling unknown service")
	} else if strings.Index(err.Error(), "method") < 0 {
		t.Error("expected error about method; got", err)
	}

	// Out of order.
	log.Println("Out of order")
	args = &Args{7, 8}
	mulReply := new(Reply)
	log.Println("Aritu.Mul")
	mulCall := client.Go("Arith.Mul", args, mulReply, nil)
	addReply := new(Reply)
	log.Println("Aritu.Add")
	addCall := client.Go("Arith.Add", args, addReply, nil)

	addCall = <-addCall.Done
	if addCall.Error != nil {
		t.Errorf("Add: expected no error but got string %q", addCall.Error.Error())
	}
	if addReply.C != args.A+args.B {
		t.Errorf("Add: expected %d got %d", addReply.C, args.A+args.B)
	}

	mulCall = <-mulCall.Done
	if mulCall.Error != nil {
		t.Errorf("Mul: expected no error but got string %q", mulCall.Error.Error())
	}
	if mulReply.C != args.A*args.B {
		t.Errorf("Mul: expected %d got %d", mulReply.C, args.A*args.B)
	}

	//	// Error test
	//	args = &Args{7, 0}
	//	reply = new(Reply)
	//	err = client.Call("Arith.Div", args, reply)
	//	// expect an error: zero divide
	//	if err == nil {
	//		t.Error("Div: expected error")
	//	} else if err.Error() != "divide by zero" {
	//		t.Error("Div: expected divide by zero error; got", err)
	//	}
	//
	//	// Bad type.
	//	reply = new(Reply)
	//	err = client.Call("Arith.Add", reply, reply) // args, reply would be the correct thing to use
	//	if err == nil {
	//		t.Error("expected error calling Arith.Add with wrong arg type")
	//	} else if strings.Index(err.Error(), "type") < 0 {
	//		t.Error("expected error about type; got", err)
	//	}
	//
	//	// Non-struct argument
	//	const Val = 12345
	//	str := new(Str)
	//	str.Val = fmt.Sprint(Val)
	//	reply = new(Reply)
	//	err = client.Call("Arith.Scan", str, reply)
	//	if err != nil {
	//		t.Errorf("Scan: expected no error but got string %q", err.Error())
	//	} else if reply.C != Val {
	//		t.Errorf("Scan: expected %d got %d", Val, reply.C)
	//	}
	//
	//	// Non-struct reply
	//	args = &Args{27, 35}
	//	str.Val = ""
	//	err = client.Call("Arith.String", args, str)
	//	if err != nil {
	//		t.Errorf("String: expected no error but got string %q", err.Error())
	//	}
	//	expect := fmt.Sprintf("%d+%d=%d", args.A, args.B, args.A+args.B)
	//	if str.Val != expect {
	//		t.Errorf("String: expected %s got %s", expect, str)
	//	}
	//
	//	args = &Args{7, 8}
	//	reply = new(Reply)
	//	err = client.Call("Arith.Mul", args, reply)
	//	if err != nil {
	//		t.Errorf("Mul: expected no error but got string %q", err.Error())
	//	}
	//	if reply.C != args.A*args.B {
	//		t.Errorf("Mul: expected %d got %d", reply.C, args.A*args.B)
	//	}
	//
	//	// ServiceName contain "." character
	//	args = &Args{7, 8}
	//	reply = new(Reply)
	//	err = client.Call("net.rpc.Arith.Add", args, reply)
	//	if err != nil {
	//		t.Errorf("Add: expected no error but got string %q", err.Error())
	//	}
	//	if reply.C != args.A+args.B {
	//		t.Errorf("Add: expected %d got %d", reply.C, args.A+args.B)
	//	}
}