Skip to content

Commit

Permalink
export: don't call IsValid separately for emitting signals
Browse files Browse the repository at this point in the history
  • Loading branch information
guelfey committed Apr 8, 2023
1 parent 17fbd01 commit 239a2e0
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
12 changes: 8 additions & 4 deletions conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -563,15 +563,17 @@ func (conn *Conn) send(ctx context.Context, msg *Message, ch chan *Call) *Call {
<-ctx.Done()
conn.calls.handleSendError(msg, ctx.Err())
}()
conn.sendMessageAndIfClosed(msg, func() {
// error is handled in handleSendError
_ = conn.sendMessageAndIfClosed(msg, func() {
conn.calls.handleSendError(msg, ErrClosed)
canceler()
})
} else {
canceler()
call = &Call{Err: nil, Done: ch}
ch <- call
conn.sendMessageAndIfClosed(msg, func() {
// error is handled in handleSendError
_ = conn.sendMessageAndIfClosed(msg, func() {
call = &Call{Err: ErrClosed}
})
}
Expand Down Expand Up @@ -605,7 +607,8 @@ func (conn *Conn) sendError(err error, dest string, serial uint32) {
if len(e.Body) > 0 {
msg.Headers[FieldSignature] = MakeVariant(SignatureOf(e.Body...))
}
conn.sendMessageAndIfClosed(msg, nil)
// not much we can do to handle a possible error here
_ = conn.sendMessageAndIfClosed(msg, nil)
}

// sendReply creates a method reply message corresponding to the parameters and
Expand All @@ -622,7 +625,8 @@ func (conn *Conn) sendReply(dest string, serial uint32, values ...interface{}) {
if len(values) > 0 {
msg.Headers[FieldSignature] = MakeVariant(SignatureOf(values...))
}
conn.sendMessageAndIfClosed(msg, nil)
// not much we can do to handle a possible error here
_ = conn.sendMessageAndIfClosed(msg, nil)
}

// AddMatchSignal registers the given match rule to receive broadcast
Expand Down
2 changes: 1 addition & 1 deletion conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ process:
reply.Body = make([]interface{}, 1)
reply.Body[0] = state
reply.Headers[FieldSignature] = MakeVariant(SignatureOf(reply.Body...))
srv.sendMessageAndIfClosed(reply, nil)
_ = srv.sendMessageAndIfClosed(reply, nil)
}
case <-ctx.Done():
t.Logf("Context cancelled, server emitted %v signals", state)
Expand Down
7 changes: 2 additions & 5 deletions export.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,18 +235,15 @@ func (conn *Conn) Emit(path ObjectPath, name string, values ...interface{}) erro
if len(values) > 0 {
msg.Headers[FieldSignature] = MakeVariant(SignatureOf(values...))
}
if err := msg.IsValid(); err != nil {
return err
}

var closed bool
conn.sendMessageAndIfClosed(msg, func() {
err := conn.sendMessageAndIfClosed(msg, func() {
closed = true
})
if closed {
return ErrClosed
}
return nil
return err
}

// Export registers the given value to be exported as an object on the
Expand Down
14 changes: 14 additions & 0 deletions export_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,20 @@ func (export *noErrorExport) Run(message Message, param string) string {
return "cool"
}

// Test that trying to emit an invalid message leads to an error.
func TestEmit_invalidMessage(t *testing.T) {
connection, err := ConnectSessionBus()
if err != nil {
t.Fatalf("Unexpected error connecting to session bus: %s", err)
}
defer connection.Close()

err = connection.Emit("/org/guelfey/DBus/Test", "org.guelfey.DBusTest", "\x00")
if _, ok := err.(FormatError); !ok {
t.Fatal("expected FormatError when emitting invalid message")
}
}

// Test typical Export usage.
func TestExport(t *testing.T) {
connection, err := ConnectSessionBus()
Expand Down

0 comments on commit 239a2e0

Please sign in to comment.