File: export_test.go

package info (click to toggle)
golang-collectd 0.5.0%2Bgit20240104.26404de-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 468 kB
  • sloc: ansic: 324; sh: 7; makefile: 3
file content (162 lines) | stat: -rw-r--r-- 3,287 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package export // import "collectd.org/export"

import (
	"context"
	"errors"
	"expvar"
	"sync"
	"testing"
	"time"

	"collectd.org/api"
	"github.com/google/go-cmp/cmp"
	"github.com/google/go-cmp/cmp/cmpopts"
)

func TestDerive(t *testing.T) {
	// clean up shared resource after testing
	defer func() {
		vars = nil
	}()

	d := NewDeriveString("example.com/TestDerive/derive")
	for i := 0; i < 10; i++ {
		d.Add(i)
	}

	want := &api.ValueList{
		Identifier: api.Identifier{
			Host:   "example.com",
			Plugin: "TestDerive",
			Type:   "derive",
		},
		Values: []api.Value{api.Derive(45)},
	}
	got := d.ValueList()

	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("Derive.ValueList() differs (+got/-want):\n%s", diff)
	}

	s := expvar.Get("example.com/TestDerive/derive").String()
	if s != "45" {
		t.Errorf("got %q, want %q", s, "45")
	}
}

func TestGauge(t *testing.T) {
	// clean up shared resource after testing
	defer func() {
		vars = nil
	}()

	g := NewGaugeString("example.com/TestGauge/gauge")
	g.Set(42.0)

	want := &api.ValueList{
		Identifier: api.Identifier{
			Host:   "example.com",
			Plugin: "TestGauge",
			Type:   "gauge",
		},
		Values: []api.Value{api.Gauge(42)},
	}
	got := g.ValueList()

	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("Gauge.ValueList() differs (+got/-want):\n%s", diff)
	}

	s := expvar.Get("example.com/TestGauge/gauge").String()
	if s != "42" {
		t.Errorf("got %q, want %q", s, "42")
	}
}

type testWriter struct {
	got  []*api.ValueList
	done chan<- struct{}
	once *sync.Once
}

func (w *testWriter) Write(ctx context.Context, vl *api.ValueList) error {
	w.got = append(w.got, vl)
	w.once.Do(func() {
		close(w.done)
	})
	return nil
}

func TestRun(t *testing.T) {
	ctx, cancel := context.WithCancel(context.Background())
	defer cancel()
	// clean up shared resource after testing
	defer func() {
		vars = nil
	}()

	d := NewDeriveString("example.com/TestRun/derive")
	d.Add(23)

	g := NewGaugeString("example.com/TestRun/gauge")
	g.Set(42)

	var (
		done = make(chan struct{})
		once sync.Once
	)

	w := testWriter{
		done: done,
		once: &once,
	}

	go func() {
		// when one metric has been written, cancel the context
		<-done
		cancel()
	}()

	err := Run(ctx, &w, Options{Interval: 100 * time.Millisecond})
	if !errors.Is(err, context.Canceled) {
		t.Errorf("Run() = %v, want %v", err, context.Canceled)
	}

	want := []*api.ValueList{
		{
			Identifier: api.Identifier{
				Host:   "example.com",
				Plugin: "TestRun",
				Type:   "gauge",
			},
			Time:     time.Now(),
			Interval: 100 * time.Millisecond,
			Values:   []api.Value{api.Gauge(42)},
		},
		{
			Identifier: api.Identifier{
				Host:   "example.com",
				Plugin: "TestRun",
				Type:   "derive",
			},
			Time:     time.Now(),
			Interval: 100 * time.Millisecond,
			Values:   []api.Value{api.Derive(23)},
		},
	}

	ignoreOrder := cmpopts.SortSlices(func(a, b *api.ValueList) bool {
		return a.Identifier.String() < b.Identifier.String()
	})
	approximateTime := cmp.Comparer(func(t0, t1 time.Time) bool {
		diff := t0.Sub(t1)
		if t1.After(t0) {
			diff = t1.Sub(t0)
		}

		return diff < 2*time.Second
	})
	if diff := cmp.Diff(want, w.got, ignoreOrder, approximateTime); diff != "" {
		t.Errorf("received value lists differ (+got/-want):\n%s", diff)
	}
}