-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathscheduler_test.go
95 lines (81 loc) · 2.1 KB
/
scheduler_test.go
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
package gather
import (
"context"
"net/http/httptest"
"os"
"testing"
"time"
"github.com/google/go-cmp/cmp"
influxlogger "github.com/influxdata/influxdb/logger"
"github.com/influxdata/platform"
"github.com/influxdata/platform/mock"
)
func TestScheduler(t *testing.T) {
publisher, subscriber := mock.NewNats()
totalGatherJobs := 3
// Create top level logger
logger := influxlogger.New(os.Stdout)
ts := httptest.NewServer(&mockHTTPHandler{
responseMap: map[string]string{
"/metrics": sampleRespSmall,
},
})
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
storage := &mockStorage{
Metrics: make(map[int64]Metrics),
Targets: []platform.ScraperTarget{
{
Type: platform.PrometheusScraperType,
URL: ts.URL + "/metrics",
},
},
TotalGatherJobs: make(chan struct{}, totalGatherJobs),
}
subscriber.Subscribe(MetricsSubject, "", &StorageHandler{
Logger: logger,
Storage: storage,
})
scheduler, err := NewScheduler(10, logger,
storage, publisher, subscriber, time.Millisecond, time.Microsecond)
go func() {
err = scheduler.run(ctx)
if err != nil {
t.Fatal(err)
}
}()
go func(scheduler *Scheduler) {
// let scheduler gather #{totalGatherJobs} metrics.
for i := 0; i < totalGatherJobs; i++ {
// make sure timestamp don't overwrite each other
time.Sleep(time.Millisecond * 10)
scheduler.gather <- struct{}{}
}
}(scheduler)
// make sure all jobs are done
for i := 0; i < totalGatherJobs; i++ {
<-storage.TotalGatherJobs
}
want := Metrics{
Name: "go_goroutines",
Type: MetricTypeGauge,
Tags: map[string]string{},
Fields: map[string]interface{}{
"gauge": float64(36),
},
}
if len(storage.Metrics) < totalGatherJobs {
t.Fatalf("metrics stored less than expected, got len %d", len(storage.Metrics))
}
for _, v := range storage.Metrics {
if diff := cmp.Diff(v, want, metricsCmpOption); diff != "" {
t.Fatalf("scraper parse metrics want %v, got %v", want, v)
}
}
ts.Close()
}
const sampleRespSmall = `
# HELP go_goroutines Number of goroutines that currently exist.
# TYPE go_goroutines gauge
go_goroutines 36
`