Ejemplo n.º 1
0
// WriteMessage writes a message to a message pipe.
func WriteMessage(handle system.MessagePipeHandle, message *Message) error {
	result := handle.WriteMessage(message.Bytes, message.Handles, system.MOJO_WRITE_MESSAGE_FLAG_NONE)
	if result != system.MOJO_RESULT_OK {
		return &ConnectionError{result}
	}
	return nil
}
Ejemplo n.º 2
0
// Mojo ServiceProvider implementation.
func (sp *serviceProviderImpl) ConnectToService(name string, messagePipe system.MessagePipeHandle) error {
	factory, ok := sp.factories[name]
	if !ok {
		messagePipe.Close()
		return nil
	}
	factory.Create(messagePipe)
	return nil
}
Ejemplo n.º 3
0
func TestMessagePipe(t *testing.T) {
	var h0, h1 system.MessagePipeHandle
	var r system.MojoResult
	var state system.MojoHandleSignalsState

	if r, h0, h1 = core.CreateMessagePipe(nil); r != system.MOJO_RESULT_OK {
		t.Fatalf("CreateMessagePipe failed:%v", r)
	}
	if !h0.IsValid() || !h1.IsValid() {
		t.Fatalf("CreateMessagePipe returned invalid handles h0:%v h1:%v", h0, h1)
	}

	r, state = h0.Wait(system.MOJO_HANDLE_SIGNAL_READABLE, 0)
	if r != system.MOJO_RESULT_DEADLINE_EXCEEDED {
		t.Fatalf("h0 should not be readable:%v", r)
	}
	if state.SatisfiedSignals != system.MOJO_HANDLE_SIGNAL_WRITABLE {
		t.Fatalf("state should be not be signaled readable after CreateMessagePipe:%v", state.SatisfiedSignals)
	}
	if state.SatisfiableSignals != MOJO_HANDLE_SIGNAL_ALL {
		t.Fatalf("state should allow all signals after CreateMessagePipe:%v", state.SatisfiableSignals)
	}

	r, state = h0.Wait(system.MOJO_HANDLE_SIGNAL_WRITABLE, system.MOJO_DEADLINE_INDEFINITE)
	if r != system.MOJO_RESULT_OK {
		t.Fatalf("h0 should be writable:%v", r)
	}
	if state.SatisfiedSignals != system.MOJO_HANDLE_SIGNAL_WRITABLE {
		t.Fatalf("state should be signaled writable after core.Wait:%v", state.SatisfiedSignals)
	}
	if state.SatisfiableSignals != MOJO_HANDLE_SIGNAL_ALL {
		t.Fatalf("state should allow all signals after core.Wait:%v", state.SatisfiableSignals)
	}

	if r, _, _ = h0.ReadMessage(system.MOJO_READ_MESSAGE_FLAG_NONE); r != system.MOJO_RESULT_SHOULD_WAIT {
		t.Fatalf("Read on h0 did not return wait:%v", r)
	}
	kHello := []byte("hello")
	if r = h1.WriteMessage(kHello, nil, system.MOJO_WRITE_MESSAGE_FLAG_NONE); r != system.MOJO_RESULT_OK {
		t.Fatalf("Failed WriteMessage on h1:%v", r)
	}

	r, state = h0.Wait(system.MOJO_HANDLE_SIGNAL_READABLE, system.MOJO_DEADLINE_INDEFINITE)
	if r != system.MOJO_RESULT_OK {
		t.Fatalf("h0 should be readable after WriteMessage to h1:%v", r)
	}
	if state.SatisfiedSignals != MOJO_HANDLE_SIGNAL_READWRITABLE {
		t.Fatalf("h0 should be signaled readable after WriteMessage to h1:%v", state.SatisfiedSignals)
	}
	if state.SatisfiableSignals != MOJO_HANDLE_SIGNAL_ALL {
		t.Fatalf("h0 should be readable/writable after WriteMessage to h1:%v", state.SatisfiableSignals)
	}
	if !state.SatisfiableSignals.IsReadable() || !state.SatisfiableSignals.IsWritable() || !state.SatisfiableSignals.IsClosed() {
		t.Fatalf("Helper functions are misbehaving")
	}

	r, msg, _ := h0.ReadMessage(system.MOJO_READ_MESSAGE_FLAG_NONE)
	if r != system.MOJO_RESULT_OK {
		t.Fatalf("Failed ReadMessage on h0:%v", r)
	}
	if !bytes.Equal(msg, kHello) {
		t.Fatalf("Invalid message expected:%s, got:%s", kHello, msg)
	}

	r, index, states := core.WaitMany([]system.Handle{h0}, []system.MojoHandleSignals{system.MOJO_HANDLE_SIGNAL_READABLE}, 10)
	if r != system.MOJO_RESULT_DEADLINE_EXCEEDED {
		t.Fatalf("h0 should not be readable after reading message:%v", r)
	}
	if index != -1 {
		t.Fatalf("should be no index after MOJO_RESULT_DEADLINE_EXCEEDED:%v", index)
	}
	if len(states) != 1 {
		t.Fatalf("states should be set after WaitMany:%v", states)
	}
	if states[0].SatisfiedSignals != system.MOJO_HANDLE_SIGNAL_WRITABLE {
		t.Fatalf("h0 should be signaled readable WaitMany:%v", states[0].SatisfiedSignals)
	}
	if states[0].SatisfiableSignals != MOJO_HANDLE_SIGNAL_ALL {
		t.Fatalf("h0 should be readable/writable after WaitMany:%v", states[0].SatisfiableSignals)
	}

	if r = h0.Close(); r != system.MOJO_RESULT_OK {
		t.Fatalf("Close on h0 failed:%v", r)
	}

	r, state = h1.Wait(MOJO_HANDLE_SIGNAL_READWRITABLE, system.MOJO_DEADLINE_INDEFINITE)
	if r != system.MOJO_RESULT_FAILED_PRECONDITION {
		t.Fatalf("h1 should not be readable/writable after Close(h0):%v", r)
	}
	if state.SatisfiedSignals != system.MOJO_HANDLE_SIGNAL_PEER_CLOSED {
		t.Fatalf("state should be signaled closed after Close(h0):%v", state.SatisfiedSignals)
	}
	if state.SatisfiableSignals != system.MOJO_HANDLE_SIGNAL_PEER_CLOSED {
		t.Fatalf("state should only be closable after Close(h0):%v", state.SatisfiableSignals)
	}

	if r = h1.Close(); r != system.MOJO_RESULT_OK {
		t.Fatalf("Close on h1 failed:%v", r)
	}
}