Exemplo n.º 1
0
func newTestMessage() *pb.MyMessage {
	msg := &pb.MyMessage{
		Count: proto.Int32(42),
		Name:  proto.String("Dave"),
		Quote: proto.String(`"I didn't want to go."`),
		Pet:   []string{"bunny", "kitty", "horsey"},
		Inner: &pb.InnerMessage{
			Host:      proto.String("footrest.syd"),
			Port:      proto.Int32(7001),
			Connected: proto.Bool(true),
		},
		Others: []*pb.OtherMessage{
			{
				Key:   proto.Int64(0xdeadbeef),
				Value: []byte{1, 65, 7, 12},
			},
			{
				Weight: proto.Float32(6.022),
				Inner: &pb.InnerMessage{
					Host: proto.String("lesha.mtv"),
					Port: proto.Int32(8002),
				},
			},
		},
		Bikeshed: pb.MyMessage_BLUE.Enum(),
		Somegroup: &pb.MyMessage_SomeGroup{
			GroupField: proto.Int32(8),
		},
		// One normally wouldn't do this.
		// This is an undeclared tag 13, as a varint (wire type 0) with value 4.
		XXX_unrecognized: []byte{13<<3 | 0, 4},
	}
	ext := &pb.Ext{
		Data: proto.String("Big gobs for big rats"),
	}
	if err := proto.SetExtension(msg, pb.E_Ext_More, ext); err != nil {
		panic(err)
	}
	greetings := []string{"adg", "easy", "cow"}
	if err := proto.SetExtension(msg, pb.E_Greeting, greetings); err != nil {
		panic(err)
	}

	// Add an unknown extension. We marshal a pb.Ext, and fake the ID.
	b, err := proto.Marshal(&pb.Ext{Data: proto.String("3G skiing")})
	if err != nil {
		panic(err)
	}
	b = append(proto.EncodeVarint(201<<3|proto.WireBytes), b...)
	proto.SetRawExtension(msg, 201, b)

	// Extensions can be plain fields, too, so let's test that.
	b = append(proto.EncodeVarint(202<<3|proto.WireVarint), 19)
	proto.SetRawExtension(msg, 202, b)

	return msg
}
Exemplo n.º 2
0
func TestArith(t *testing.T) {
	onceArith.Do(func() {
		go ListenAndServeArithService("tcp", ":2010", new(Arith))
	})
	client, stub, err := DialArithService("tcp", "127.0.0.1:2010")
	if err != nil {
		t.Fatalf(`DialArithService("tcp", "127.0.0.1:2010"): %v`, err)
	}
	defer client.Close()

	var args ArithRequest
	var reply ArithResponse

	// Add
	args.A = proto.Int32(1)
	args.B = proto.Int32(2)
	if err = stub.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 = stub.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 = stub.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 = stub.Div(&args, &reply); err.Error() != "divide by zero" {
		t.Fatalf(`arith.Div: expected = "%s", got = "%s"`, "divide by zero", err.Error())
	}

	// Error
	args.A = proto.Int32(1)
	args.B = proto.Int32(2)
	if err = stub.Error(&args, &reply); err.Error() != "ArithError" {
		t.Fatalf(`arith.Error: expected = "%s", got = "%s"`, "ArithError", err.Error())
	}
}
Exemplo n.º 3
0
func testArithClient(t *testing.T, client *rpc.Client) {
	var args ArithRequest
	var reply 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.Div: 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())
	}
}
Exemplo n.º 4
0
func testArithStub(t *testing.T, stub ArithService) {
	var args ArithRequest
	var reply ArithResponse
	var err error

	// Add
	args.A = proto.Int32(1)
	args.B = proto.Int32(2)
	if err = stub.Add(&args, &reply); err != nil {
		t.Fatalf(`stub.Add: %v`, err)
	}
	if reply.GetC() != 3 {
		t.Fatalf(`stub.Add: expected = %d, got = %d`, 3, reply.GetC())
	}

	// Mul
	args.A = proto.Int32(2)
	args.B = proto.Int32(3)
	if err = stub.Mul(&args, &reply); err != nil {
		t.Fatalf(`stub.Mul: %v`, err)
	}
	if reply.GetC() != 6 {
		t.Fatalf(`stub.Mul: expected = %d, got = %d`, 6, reply.GetC())
	}

	// Div
	args.A = proto.Int32(13)
	args.B = proto.Int32(5)
	if err = stub.Div(&args, &reply); err != nil {
		t.Fatalf(`stub.Div: %v`, err)
	}
	if reply.GetC() != 2 {
		t.Fatalf(`stub.Div: expected = %d, got = %d`, 2, reply.GetC())
	}

	// Div zero
	args.A = proto.Int32(1)
	args.B = proto.Int32(0)
	if err = stub.Div(&args, &reply); err.Error() != "divide by zero" {
		t.Fatalf(`stub.Div: expected = "%s", got = "%s"`, "divide by zero", err.Error())
	}

	// Error
	args.A = proto.Int32(1)
	args.B = proto.Int32(2)
	if err = stub.Error(&args, &reply); err.Error() != "ArithError" {
		t.Fatalf(`stub.Error: expected = "%s", got = "%s"`, "ArithError", err.Error())
	}
}
Exemplo n.º 5
0
func (t *Arith) Div(args *ArithRequest, reply *ArithResponse) error {
	if args.GetB() == 0 {
		return errors.New("divide by zero")
	}
	reply.C = proto.Int32(args.GetA() / args.GetB())
	return nil
}
Exemplo n.º 6
0
func (t *Arith) Div(args *service.ArithRequest, reply *service.ArithResponse) (err error) {
	if args.GetB() == 0 {
		err = errors.New("divide by zero")
		log.Printf("Arith.Div: args = %v, reply = %v, err = %v\n", args, reply, err)
		return
	}
	reply.C = proto.Int32(args.GetA() / args.GetB())
	log.Printf("Arith.Div: args = %v, reply = %v, err = %v\n", args, reply, err)
	return
}
Exemplo n.º 7
0
func (t *Arith) Mul(args *ArithRequest, reply *ArithResponse) error {
	reply.C = proto.Int32(args.GetA() * args.GetB())
	return nil
}
Exemplo n.º 8
0
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

package proto_test

import (
	"testing"

	"encoding/protobuf/proto"

	pb "./testdata"
)

var cloneTestMessage = &pb.MyMessage{
	Count: proto.Int32(42),
	Name:  proto.String("Dave"),
	Pet:   []string{"bunny", "kitty", "horsey"},
	Inner: &pb.InnerMessage{
		Host:      proto.String("niles"),
		Port:      proto.Int32(9099),
		Connected: proto.Bool(true),
	},
	Others: []*pb.OtherMessage{
		{
			Value: []byte("some bytes"),
		},
	},
	Somegroup: &pb.MyMessage_SomeGroup{
		GroupField: proto.Int32(6),
	},
Exemplo n.º 9
0
func (t *Arith) Mul(args *service.ArithRequest, reply *service.ArithResponse) (err error) {
	reply.C = proto.Int32(args.GetA() * args.GetB())
	log.Printf("Arith.Mul: args = %v, reply = %v, err = %v\n", args, reply, err)
	return
}
Exemplo n.º 10
0
func main() {
	var client *rpc.Client
	var err error

	client, err = protorpc.Dial("tcp", "127.0.0.1:1234")
	if err != nil {
		log.Fatal(err)
	}
	defer client.Close()

	arithStub := service.NewArithServiceStub(client)
	echoStub := service.NewEchoServiceStub(client)

	var arithArgs service.ArithRequest
	var arithReply service.ArithResponse
	var echoArgs service.EchoRequest
	var echoReply service.EchoResponse

	// Add
	arithArgs.A = proto.Int32(1)
	arithArgs.B = proto.Int32(2)
	if err = arithStub.Add(&arithArgs, &arithReply); err != nil {
		log.Fatalf(`arith.Add: %v`, err)
	}
	if arithReply.GetC() != 3 {
		log.Fatalf(`arith.Add: expected = %d, got = %d`, 3, arithReply.GetC())
	}

	// Mul
	arithArgs.A = proto.Int32(2)
	arithArgs.B = proto.Int32(3)
	if err = arithStub.Mul(&arithArgs, &arithReply); err != nil {
		log.Fatalf(`arith.Mul: %v`, err)
	}
	if arithReply.GetC() != 6 {
		log.Fatalf(`arith.Mul: expected = %d, got = %d`, 6, arithReply.GetC())
	}

	// Div
	arithArgs.A = proto.Int32(13)
	arithArgs.B = proto.Int32(5)
	if err = arithStub.Div(&arithArgs, &arithReply); err != nil {
		log.Fatalf(`arith.Div: %v`, err)
	}
	if arithReply.GetC() != 2 {
		log.Fatalf(`arith.Div: expected = %d, got = %d`, 2, arithReply.GetC())
	}

	// Div zero
	arithArgs.A = proto.Int32(1)
	arithArgs.B = proto.Int32(0)
	if err = arithStub.Div(&arithArgs, &arithReply); err.Error() != "divide by zero" {
		log.Fatalf(`arith.Div: expected = "%s", got = "%s"`, "divide by zero", err.Error())
	}

	// Error
	arithArgs.A = proto.Int32(1)
	arithArgs.B = proto.Int32(2)
	if err = arithStub.Error(&arithArgs, &arithReply); err.Error() != "ArithError" {
		log.Fatalf(`arith.Error: expected = "%s", got = "%s"`, "ArithError", err.Error())
	}

	// EchoService.Echo
	echoArgs.Msg = proto.String("Hello, Protobuf-RPC")
	if err = echoStub.Echo(&echoArgs, &echoReply); err != nil {
		log.Fatalf(`echoStub.Echo: %v`, err)
	}
	if echoArgs.GetMsg() != echoReply.GetMsg() {
		log.Fatalf(`echoStub.Echo: expected = "%s", got = "%s"`, echoArgs.GetMsg(), echoReply.GetMsg())
	}

	fmt.Printf("Done\n")
}
Exemplo n.º 11
0
func (t *Arith) Add(args *msg.ArithRequest, reply *msg.ArithResponse) error {
	reply.C = proto.Int32(args.GetA() + args.GetB())
	return nil
}