1 // Copyright 2017 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.
14 func TestSetGoroutineLabels(t
*testing
.T
) {
15 sync
:= make(chan struct{})
17 wantLabels
:= map[string]string{}
18 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
19 t
.Errorf("Expected parent goroutine's profile labels to be empty before test, got %v", gotLabels
)
22 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
23 t
.Errorf("Expected child goroutine's profile labels to be empty before test, got %v", gotLabels
)
29 wantLabels
= map[string]string{"key": "value"}
30 ctx
:= WithLabels(context
.Background(), Labels("key", "value"))
31 SetGoroutineLabels(ctx
)
32 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
33 t
.Errorf("parent goroutine's profile labels: got %v, want %v", gotLabels
, wantLabels
)
36 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
37 t
.Errorf("child goroutine's profile labels: got %v, want %v", gotLabels
, wantLabels
)
43 wantLabels
= map[string]string{}
44 ctx
= context
.Background()
45 SetGoroutineLabels(ctx
)
46 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
47 t
.Errorf("Expected parent goroutine's profile labels to be empty, got %v", gotLabels
)
50 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
51 t
.Errorf("Expected child goroutine's profile labels to be empty, got %v", gotLabels
)
58 func TestDo(t
*testing
.T
) {
59 wantLabels
:= map[string]string{}
60 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
61 t
.Errorf("Expected parent goroutine's profile labels to be empty before Do, got %v", gotLabels
)
64 Do(context
.Background(), Labels("key1", "value1", "key2", "value2"), func(ctx context
.Context
) {
65 wantLabels
:= map[string]string{"key1": "value1", "key2": "value2"}
66 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
67 t
.Errorf("parent goroutine's profile labels: got %v, want %v", gotLabels
, wantLabels
)
70 sync
:= make(chan struct{})
72 wantLabels
:= map[string]string{"key1": "value1", "key2": "value2"}
73 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
74 t
.Errorf("child goroutine's profile labels: got %v, want %v", gotLabels
, wantLabels
)
82 wantLabels
= map[string]string{}
83 if gotLabels
:= getProfLabel(); !reflect
.DeepEqual(gotLabels
, wantLabels
) {
84 fmt
.Printf("%#v", gotLabels
)
85 fmt
.Printf("%#v", wantLabels
)
86 t
.Errorf("Expected parent goroutine's profile labels to be empty after Do, got %v", gotLabels
)
90 func getProfLabel() map[string]string {
91 l
:= (*labelMap
)(runtime_getProfLabel())
93 return map[string]string{}