MCPcopy
hub / github.com/grafana/tempo / Test_ReportLoop

Function Test_ReportLoop

pkg/usagestats/reporter_test.go:129–210  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

127}
128
129func Test_ReportLoop(t *testing.T) {
130 origReportCheckInterval := reportCheckInterval
131 origReportInterval := reportInterval
132 origStabilityCheckInterval := stabilityCheckInterval
133 origStabilityMinimumRequired := stabilityMinimumRequired
134 origUsageStatsURL := usageStatsURL
135 defer func() {
136 reportCheckInterval = origReportCheckInterval
137 reportInterval = origReportInterval
138 stabilityCheckInterval = origStabilityCheckInterval
139 stabilityMinimumRequired = origStabilityMinimumRequired
140 usageStatsURL = origUsageStatsURL
141 }()
142
143 reportCheckInterval = 10 * time.Millisecond
144 reportInterval = 50 * time.Millisecond
145 stabilityCheckInterval = 10 * time.Millisecond
146 stabilityMinimumRequired = 1
147
148 const targetReports = 5
149 var (
150 mtx sync.Mutex
151 totalReport int
152 clusterIDs []string
153 )
154 reportsDone := make(chan struct{})
155 server := httptest.NewServer(http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
156 var received Report
157 require.NoError(t, json.NewDecoder(r.Body).Decode(&received))
158
159 mtx.Lock()
160 totalReport++
161 clusterIDs = append(clusterIDs, received.ClusterID)
162 shouldClose := totalReport >= targetReports
163 mtx.Unlock()
164
165 if shouldClose {
166 select {
167 case <-reportsDone:
168 default:
169 close(reportsDone)
170 }
171 }
172
173 rw.WriteHeader(http.StatusOK)
174 }))
175 defer server.Close()
176 usageStatsURL = server.URL
177
178 objectClient, err := local.NewBackend(&local.Config{
179 Path: t.TempDir(),
180 })
181 require.NoError(t, err)
182
183 r, err := NewReporter(Config{Leader: true, Enabled: true}, kv.Config{
184 Store: "inmemory",
185 }, objectClient, objectClient, log.NewNopLogger(), prometheus.NewPedanticRegistry())
186 require.NoError(t, err)

Callers

nothing calls this directly

Calls 8

initLeaderMethod · 0.95
runningMethod · 0.95
NewBackendFunction · 0.92
NewReporterFunction · 0.85
DecodeMethod · 0.65
CloseMethod · 0.65
WriteHeaderMethod · 0.45
EqualMethod · 0.45

Tested by

no test coverage detected