1// Copyright 2015 The Go Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style
3// license that can be found in the LICENSE file.
4
5package profile
6
7import (
8	"bytes"
9	"testing"
10)
11
12func TestEmptyProfile(t *testing.T) {
13	var buf bytes.Buffer
14	p, err := Parse(&buf)
15	if err != nil {
16		t.Error("Want no error, got", err)
17	}
18	if p == nil {
19		t.Fatal("Want a valid profile, got <nil>")
20	}
21	if !p.Empty() {
22		t.Errorf("Profile should be empty, got %#v", p)
23	}
24}
25
26func TestParseContention(t *testing.T) {
27	tests := []struct {
28		name    string
29		in      string
30		wantErr bool
31	}{
32		{
33			name: "valid",
34			in: `--- mutex:
35cycles/second=3491920901
36sampling period=1
3743227965305 1659640 @ 0x45e851 0x45f764 0x4a2be1 0x44ea31
3834035731690 15760 @ 0x45e851 0x45f764 0x4a2b17 0x44ea31
39`,
40		},
41		{
42			name: "valid with comment",
43			in: `--- mutex:
44cycles/second=3491920901
45sampling period=1
4643227965305 1659640 @ 0x45e851 0x45f764 0x4a2be1 0x44ea31
47#	0x45e850	sync.(*Mutex).Unlock+0x80	/go/src/sync/mutex.go:126
48#	0x45f763	sync.(*RWMutex).Unlock+0x83	/go/src/sync/rwmutex.go:125
49#	0x4a2be0	main.main.func3+0x70		/go/src/internal/pprof/profile/a_binary.go:58
50
5134035731690 15760 @ 0x45e851 0x45f764 0x4a2b17 0x44ea31
52#	0x45e850	sync.(*Mutex).Unlock+0x80	/go/src/sync/mutex.go:126
53#	0x45f763	sync.(*RWMutex).Unlock+0x83	/go/src/sync/rwmutex.go:125
54#	0x4a2b16	main.main.func2+0xd6		/go/src/internal/pprof/profile/a_binary.go:48
55`,
56		},
57		{
58			name:    "empty",
59			in:      `--- mutex:`,
60			wantErr: true,
61		},
62		{
63			name: "invalid header",
64			in: `--- channel:
6543227965305 1659640 @ 0x45e851 0x45f764 0x4a2be1 0x44ea31`,
66			wantErr: true,
67		},
68	}
69	for _, tc := range tests {
70		_, err := parseContention([]byte(tc.in))
71		if tc.wantErr && err == nil {
72			t.Errorf("parseContention(%q) succeeded unexpectedly", tc.name)
73		}
74		if !tc.wantErr && err != nil {
75			t.Errorf("parseContention(%q) failed unexpectedly: %v", tc.name, err)
76		}
77	}
78
79}
80