Source file src/runtime/pprof/label_test.go

     1  package pprof
     2  
     3  import (
     4  	"context"
     5  	"reflect"
     6  	"sort"
     7  	"testing"
     8  )
     9  
    10  func labelsSorted(ctx context.Context) []label {
    11  	ls := []label{}
    12  	ForLabels(ctx, func(key, value string) bool {
    13  		ls = append(ls, label{key, value})
    14  		return true
    15  	})
    16  	sort.Sort(labelSorter(ls))
    17  	return ls
    18  }
    19  
    20  type labelSorter []label
    21  
    22  func (s labelSorter) Len() int           { return len(s) }
    23  func (s labelSorter) Swap(i, j int)      { s[i], s[j] = s[j], s[i] }
    24  func (s labelSorter) Less(i, j int) bool { return s[i].key < s[j].key }
    25  
    26  func TestContextLabels(t *testing.T) {
    27  	// Background context starts with no labels.
    28  	ctx := context.Background()
    29  	labels := labelsSorted(ctx)
    30  	if len(labels) != 0 {
    31  		t.Errorf("labels on background context: want [], got %v ", labels)
    32  	}
    33  
    34  	// Add a single label.
    35  	ctx = WithLabels(ctx, Labels("key", "value"))
    36  	// Retrieve it with Label.
    37  	v, ok := Label(ctx, "key")
    38  	if !ok || v != "value" {
    39  		t.Errorf(`Label(ctx, "key"): got %v, %v; want "value", ok`, v, ok)
    40  	}
    41  	gotLabels := labelsSorted(ctx)
    42  	wantLabels := []label{{"key", "value"}}
    43  	if !reflect.DeepEqual(gotLabels, wantLabels) {
    44  		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
    45  	}
    46  
    47  	// Add a label with a different key.
    48  	ctx = WithLabels(ctx, Labels("key2", "value2"))
    49  	v, ok = Label(ctx, "key2")
    50  	if !ok || v != "value2" {
    51  		t.Errorf(`Label(ctx, "key2"): got %v, %v; want "value2", ok`, v, ok)
    52  	}
    53  	gotLabels = labelsSorted(ctx)
    54  	wantLabels = []label{{"key", "value"}, {"key2", "value2"}}
    55  	if !reflect.DeepEqual(gotLabels, wantLabels) {
    56  		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
    57  	}
    58  
    59  	// Add label with first key to test label replacement.
    60  	ctx = WithLabels(ctx, Labels("key", "value3"))
    61  	v, ok = Label(ctx, "key")
    62  	if !ok || v != "value3" {
    63  		t.Errorf(`Label(ctx, "key3"): got %v, %v; want "value3", ok`, v, ok)
    64  	}
    65  	gotLabels = labelsSorted(ctx)
    66  	wantLabels = []label{{"key", "value3"}, {"key2", "value2"}}
    67  	if !reflect.DeepEqual(gotLabels, wantLabels) {
    68  		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
    69  	}
    70  
    71  	// Labels called with two labels with the same key should pick the second.
    72  	ctx = WithLabels(ctx, Labels("key4", "value4a", "key4", "value4b"))
    73  	v, ok = Label(ctx, "key4")
    74  	if !ok || v != "value4b" {
    75  		t.Errorf(`Label(ctx, "key4"): got %v, %v; want "value4b", ok`, v, ok)
    76  	}
    77  	gotLabels = labelsSorted(ctx)
    78  	wantLabels = []label{{"key", "value3"}, {"key2", "value2"}, {"key4", "value4b"}}
    79  	if !reflect.DeepEqual(gotLabels, wantLabels) {
    80  		t.Errorf("(sorted) labels on context: got %v, want %v", gotLabels, wantLabels)
    81  	}
    82  }
    83  
    84  func TestLabelMapStringer(t *testing.T) {
    85  	for _, tbl := range []struct {
    86  		m        labelMap
    87  		expected string
    88  	}{
    89  		{
    90  			m: labelMap{
    91  				// empty map
    92  			},
    93  			expected: "{}",
    94  		}, {
    95  			m: labelMap{
    96  				"foo": "bar",
    97  			},
    98  			expected: `{"foo":"bar"}`,
    99  		}, {
   100  			m: labelMap{
   101  				"foo":             "bar",
   102  				"key1":            "value1",
   103  				"key2":            "value2",
   104  				"key3":            "value3",
   105  				"key4WithNewline": "\nvalue4",
   106  			},
   107  			expected: `{"foo":"bar", "key1":"value1", "key2":"value2", "key3":"value3", "key4WithNewline":"\nvalue4"}`,
   108  		},
   109  	} {
   110  		if got := tbl.m.String(); tbl.expected != got {
   111  			t.Errorf("%#v.String() = %q; want %q", tbl.m, got, tbl.expected)
   112  		}
   113  	}
   114  }
   115  

View as plain text