Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Increase test and benchmark coverage #394

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,7 @@ func TestCloseBeforeSignal(t *testing.T) {
reader, pipewriter := io.Pipe()
defer pipewriter.Close()
defer reader.Close()
wg := sync.WaitGroup{}

bus, err := NewConn(rwc{Reader: reader, Writer: io.Discard})
if err != nil {
Expand All @@ -178,11 +179,13 @@ func TestCloseBeforeSignal(t *testing.T) {
ch := make(chan *Signal, 1)
bus.Signal(ch)

wg.Add(1)
go func() {
_, err := pipewriter.Write([]byte("REJECTED name\r\nOK myuuid\r\n"))
if err != nil {
t.Errorf("error writing to pipe: %v", err)
}
wg.Done()
}()

err = bus.Auth([]Auth{fakeAuth{}})
Expand All @@ -207,6 +210,7 @@ func TestCloseBeforeSignal(t *testing.T) {
if err != nil {
t.Fatal(err)
}
wg.Wait()
}

func TestCloseChannelAfterRemoveSignal(t *testing.T) {
Expand Down
115 changes: 115 additions & 0 deletions decoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,3 +86,118 @@ func TestSigByteSize(t *testing.T) {
}
}
}

func BenchmarkDecodeArrayEmptyStruct(b *testing.B) {
buf := bytes.NewBuffer(nil)
msg := &Message{
Type: 0x02,
Flags: 0x00,
Headers: map[HeaderField]Variant{
0x06: {
sig: Signature{
str: "s",
},
value: ":1.391",
},
0x05: {
sig: Signature{
str: "u",
},
value: uint32(2),
},
0x08: {
sig: Signature{
str: "g",
},
value: Signature{
str: "v",
},
},
},
Body: []interface{}{
Variant{
sig: Signature{
str: "(sa(iiay)ss)",
},
value: property{
IconName: "iconname",
Pixmaps: []pixmap{},
Title: "title",
Description: "description",
},
},
},
serial: 0x00000003,
}
err := msg.EncodeTo(buf, binary.LittleEndian)
if err != nil {
b.Fatal(err)
}
data := buf.Bytes()
for i := 0; i < b.N; i++ {
buf.Reset()
buf.Write(data)
_, err = DecodeMessage(buf)
if err != nil {
b.Fatal(err)
}
}
}

func BenchmarkDecodePropertyChanged(b *testing.B) {
intVal := int32(1)
buf := bytes.NewBuffer(nil)
msg := &Message{
Type: TypeSignal,
Flags: 0x00,
Headers: map[HeaderField]Variant{
FieldSignature: {
sig: Signature{
str: "g",
},
value: Signature{
str: "sa{sv}as",
},
},
FieldInterface: {
sig: Signature{
str: "s",
},
value: "org.freedesktop.DBus.Properties",
},
FieldMember: {
sig: Signature{
str: "s",
},
value: "PropertiesChanged",
},
FieldPath: {
sig: Signature{
str: "o",
},
value: ObjectPath("/com/github/pboyd/Stress"),
},
},
Body: []interface{}{
"com.github.pboyd.Stress",
map[string]Variant{
"SomeInt": {sig: Signature{str: "i"}, value: &intVal},
},
[]string{},
},
serial: 0x000029f5,
}
err := msg.EncodeTo(buf, binary.LittleEndian)
if err != nil {
b.Fatal(err)
}
data := buf.Bytes()
for n := 0; n < b.N; n++ {
buf.Reset()
buf.Write(data)
_, err = DecodeMessage(buf)
if err != nil {
b.Fatal(err)
}
}
}
7 changes: 7 additions & 0 deletions encoder.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,13 @@ func newEncoderAtOffset(out io.Writer, offset int, order binary.ByteOrder, fds [
return enc
}

func (enc *encoder) Reset(out io.Writer, order binary.ByteOrder, fds []int) {
enc.out = out
enc.order = order
enc.pos = 0
enc.fds = fds
}

// Aligns the next output to be on a multiple of n. Panics on write errors.
func (enc *encoder) align(n int) {
pad := enc.padding(0, n)
Expand Down
Loading