MCPcopy Create free account
hub / github.com/prometheus/common / TestEncode

Function TestEncode

expfmt/encode_test.go:106–176  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

104}
105
106func TestEncode(t *testing.T) {
107 var buff bytes.Buffer
108 delimEncoder := NewEncoder(&buff, FmtProtoDelim)
109 metric := &dto.MetricFamily{
110 Name: proto.String("foo_metric"),
111 Type: dto.MetricType_UNTYPED.Enum(),
112 Metric: []*dto.Metric{
113 {
114 Untyped: &dto.Untyped{
115 Value: proto.Float64(1.234),
116 },
117 },
118 },
119 }
120
121 err := delimEncoder.Encode(metric)
122 if err != nil {
123 t.Errorf("unexpected error during encode: %s", err.Error())
124 }
125
126 out := buff.Bytes()
127 if len(out) == 0 {
128 t.Errorf("expected the output bytes buffer to be non-empty")
129 }
130
131 buff.Reset()
132
133 compactEncoder := NewEncoder(&buff, FmtProtoCompact)
134 err = compactEncoder.Encode(metric)
135 if err != nil {
136 t.Errorf("unexpected error during encode: %s", err.Error())
137 }
138
139 out = buff.Bytes()
140 if len(out) == 0 {
141 t.Errorf("expected the output bytes buffer to be non-empty")
142 }
143
144 buff.Reset()
145
146 protoTextEncoder := NewEncoder(&buff, FmtProtoText)
147 err = protoTextEncoder.Encode(metric)
148 if err != nil {
149 t.Errorf("unexpected error during encode: %s", err.Error())
150 }
151
152 out = buff.Bytes()
153 if len(out) == 0 {
154 t.Errorf("expected the output bytes buffer to be non-empty")
155 }
156
157 buff.Reset()
158
159 textEncoder := NewEncoder(&buff, FmtText)
160 err = textEncoder.Encode(metric)
161 if err != nil {
162 t.Errorf("unexpected error during encode: %s", err.Error())
163 }

Callers

nothing calls this directly

Calls 4

EncodeMethod · 0.95
NewEncoderFunction · 0.85
ErrorMethod · 0.80
StringMethod · 0.65

Tested by

no test coverage detected