@@ -3,6 +3,10 @@ package main
33import (
44 "reflect"
55 "testing"
6+ "time"
7+
8+ "github.com/prometheus/client_golang/prometheus"
9+ dto "github.com/prometheus/client_model/go"
610)
711
812func TestParseInfo (t * testing.T ) {
@@ -35,3 +39,59 @@ func TestParseInfo(t *testing.T) {
3539 }
3640 }
3741}
42+
43+ func TestInfoCollect (t * testing.T ) {
44+ type cas struct {
45+ payload string
46+ field string
47+ metric cmetric
48+ want string
49+ }
50+ for n , c := range []cas {
51+ {
52+ payload : "counter-1=3.14:gauge-1=6.12:flag=false:counter-2=6.66" ,
53+ field : "gauge-1" ,
54+ metric : cmetric {
55+ typ : prometheus .GaugeValue ,
56+ desc : prometheus .NewDesc (
57+ "g1" ,
58+ "My first gauge" ,
59+ nil ,
60+ nil ,
61+ ),
62+ },
63+ want : `gauge:<value:6.12 > ` ,
64+ },
65+ {
66+ payload : "counter-1=3.14:gauge-1=6.12:flag=false:counter-2=6.66" ,
67+ field : "counter-1" ,
68+ metric : cmetric {
69+ typ : prometheus .CounterValue ,
70+ desc : prometheus .NewDesc (
71+ "c1" ,
72+ "My first counter" ,
73+ nil ,
74+ nil ,
75+ ),
76+ },
77+ want : `counter:<value:3.14 > ` ,
78+ },
79+ } {
80+ ch := make (chan prometheus.Metric )
81+ metrics := cmetrics {c .field : c .metric }
82+ go infoCollect (ch , metrics , c .payload )
83+
84+ var metric prometheus.Metric
85+ select {
86+ case metric = <- ch :
87+ case <- time .After (time .Second ):
88+ t .Fatalf ("timeout for case %d (%q)" , n , c .want )
89+ }
90+
91+ m := & dto.Metric {}
92+ metric .Write (m )
93+ if have , want := m .String (), c .want ; have != want {
94+ t .Errorf ("case %d: have %q, want %q" , n , have , want )
95+ }
96+ }
97+ }
0 commit comments