示例#1
0
func encodeMessage(header bindings.MessageHeader, payload bindings.Payload) *bindings.Message {
	message, err := bindings.EncodeMessage(header, payload)
	if err != nil {
		panic(err)
	}
	return message
}
示例#2
0
func TestStubClosesPipe(t *testing.T) {
	request, pointer := echo.CreateMessagePipeForEcho()
	// Create a message with valid header and invalid payload and send it.
	header := bindings.MessageHeader{0, bindings.MessageIsResponseFlag, 1}
	message, err := bindings.EncodeMessage(header, &header)
	if err != nil {
		t.Fatalf("Error encoding message: %v", err)
	}
	pointerPipe := pointer.PassMessagePipe()
	if r := pointerPipe.WriteMessage(message.Bytes, nil, system.MOJO_WRITE_MESSAGE_FLAG_NONE); r != system.MOJO_RESULT_OK {
		t.Fatalf("Can't send a message: %v", r)
	}

	// Make echo request with response (that is already sent).
	echoStub := echo.NewEchoStub(request, &echoImpl{}, waiter)
	if err := echoStub.ServeRequest(); err == nil {
		t.Fatalf("Message is invalid, error expected")
	}

	// Wait for the pipe to be closed.
	waitResponseChan := make(chan bindings.WaitResponse)
	waiter.AsyncWait(pointerPipe, system.MOJO_HANDLE_SIGNAL_PEER_CLOSED, waitResponseChan)
	waitResponse := <-waitResponseChan
	if waitResponse.Result != system.MOJO_RESULT_OK {
		t.Fatalf("Error waiting on pipe to be closed: %v", waitResponse.Result)
	}
	pointerPipe.Close()
}
示例#3
0
func check(t *testing.T, value, zeroValue bindings.Payload) {
	message, err := bindings.EncodeMessage(bindings.MessageHeader{}, value)
	if err != nil {
		t.Fatalf("error encoding value %v: %v", value, err)
	}
	decodedMessage, err := bindings.ParseMessage(message.Bytes, message.Handles)
	if err != nil {
		t.Fatalf("error decoding message header from bytes %v for tested value %v: %v", message.Bytes, value, err)
	}
	if err = decodedMessage.DecodePayload(zeroValue); err != nil {
		t.Fatalf("error decoding message payload from bytes %v for tested value %v: %v", message.Payload, value, err)
	}
	if !reflect.DeepEqual(value, zeroValue) {
		t.Fatalf("unexpected value after decoding: expected %v, got %v", value, zeroValue)
	}
}
示例#4
0
func checkMessageEncoding(t *testing.T, header, payload bindings.MessageHeader) {
	var encodedMessage, decodedMessage *bindings.Message
	var err error
	var decodedHeader, decodedPayload bindings.MessageHeader

	if encodedMessage, err = bindings.EncodeMessage(header, &payload); err != nil {
		t.Fatalf("Failed encoding message: %v", err)
	}

	if decodedMessage, err = bindings.ParseMessage(encodedMessage.Bytes, nil); err != nil {
		t.Fatalf("Failed decoding message header: %v", err)
	}
	if decodedHeader = decodedMessage.Header; decodedHeader != header {
		t.Fatalf("Unexpected header decoded: got %v, want %v", decodedHeader, header)
	}
	if err = decodedMessage.DecodePayload(&decodedPayload); err != nil {
		t.Fatalf("Failed decoding message payload: %v", err)
	}
	if decodedPayload != payload {
		t.Fatalf("Unexpected header with request id decoded: got %v, want %v", decodedPayload, payload)
	}
}