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() }
// AcceptConnection accepts incoming connection, providing and asking for echo // service. func (delegate *EchoDelegate) AcceptConnection(conn *application.Connection) { if conn.RequestorURL() != pairedURL(delegate.localURL) { panic(fmt.Sprintf("invalid requestor URL: want %v, got %v", pairedURL(delegate.localURL), conn.RequestorURL())) } if conn.ConnectionURL() != delegate.localURL { panic(fmt.Sprintf("invalid connection URL: want %v, got %v", delegate.localURL, conn.ConnectionURL())) } echoRequest, echoPointer := echo.CreateMessagePipeForEcho() conn.ProvideServices(&echo.Echo_ServiceFactory{delegate}).ConnectToService(&echoRequest) checkEcho(echoPointer) }
func (delegate *EchoClientDelegate) Initialize(ctx application.Context) { echoRequest, echoPointer := echo.CreateMessagePipeForEcho() ctx.ConnectToApplication("mojo:go_echo_server").ConnectToService(&echoRequest) echoProxy := echo.NewEchoProxy(echoPointer, bindings.GetAsyncWaiter()) response, err := echoProxy.EchoString(bindings.StringPointer("Hello, Go world!")) if response != nil { fmt.Printf("client: %s\n", *response) } else { log.Println(err) } echoProxy.Close_Proxy() ctx.Close() }
// Initialize connects to other instance, providing and asking for echo service. func (delegate *EchoDelegate) Initialize(ctx application.Context) { if ctx.URL() != delegate.localURL { panic(fmt.Sprintf("invalid URL: want %v, got %v", delegate.localURL, ctx.URL())) } if len(ctx.Args()) != 1 || ctx.Args()[0] != delegate.localURL { panic(fmt.Sprintf("invalid args: want %v, got %v", []string{delegate.localURL}, ctx.Args())) } go func() { echoRequest, echoPointer := echo.CreateMessagePipeForEcho() conn := ctx.ConnectToApplication(pairedURL(delegate.localURL), &echo.Echo_ServiceFactory{delegate}) if conn.RequestorURL() != delegate.localURL { panic(fmt.Sprintf("invalid requestor URL: want %v, got %v", delegate.localURL, conn.RequestorURL())) } if conn.ConnectionURL() != pairedURL(delegate.localURL) { panic(fmt.Sprintf("invalid connection URL: want %v, got %v", pairedURL(delegate.localURL), conn.ConnectionURL())) } conn.ConnectToService(&echoRequest) checkEcho(echoPointer) }() }