@@ -82,8 +82,8 @@ func TestRunEntryFailsWhenSpammerDoesNotStart(t *testing.T) {
8282func TestWaitForSpamoorDoneUsesDeltas (t * testing.T ) {
8383 client := & fakeClient {
8484 metricsSeq : []metricSnapshot {
85- {sent : 105 , failed : 3 },
86- {sent : 108 , failed : 4 },
85+ {sent : 105 , failed : 3 , running : 1 },
86+ {sent : 108 , failed : 4 , running : 0 },
8787 },
8888 spammerNames : []string {"bench-test-0" },
8989 }
@@ -94,6 +94,22 @@ func TestWaitForSpamoorDoneUsesDeltas(t *testing.T) {
9494 require .Equal (t , 2.0 , failed )
9595}
9696
97+ func TestWaitForSpamoorDoneWaitsForSpammersToStop (t * testing.T ) {
98+ client := & fakeClient {
99+ metricsSeq : []metricSnapshot {
100+ {sent : 110 , failed : 0 , running : 1 },
101+ {sent : 110 , failed : 0 , running : 1 },
102+ {sent : 110 , failed : 0 , running : 0 },
103+ },
104+ spammerNames : []string {"bench-test-0" },
105+ }
106+
107+ sent , _ , err := waitForSpamoorDoneWithInterval (context .Background (), client , 10 , 100 , 0 , "" , time .Millisecond )
108+ require .NoError (t , err )
109+ require .Equal (t , 10.0 , sent )
110+ require .Equal (t , 3 , client .metricsIndex )
111+ }
112+
97113func TestWaitForSpamoorDoneHonorsContext (t * testing.T ) {
98114 client := & fakeClient {
99115 metricsSeq : []metricSnapshot {
@@ -167,8 +183,9 @@ func TestSumCounterWithPrefixFiltersCorrectly(t *testing.T) {
167183}
168184
169185type metricSnapshot struct {
170- sent float64
171- failed float64
186+ sent float64
187+ failed float64
188+ running float64
172189}
173190
174191type fakeClient struct {
@@ -234,16 +251,38 @@ func (f *fakeClient) GetMetrics() (map[string]*dto.MetricFamily, error) {
234251 perFailed := snapshot .failed / float64 (len (f .spammerNames ))
235252 sentMap := make (map [string ]float64 , len (f .spammerNames ))
236253 failedMap := make (map [string ]float64 , len (f .spammerNames ))
254+ runningMap := make (map [string ]float64 , len (f .spammerNames ))
237255 for _ , name := range f .spammerNames {
238256 sentMap [name ] = perSpammer
239257 failedMap [name ] = perFailed
258+ runningMap [name ] = snapshot .running
240259 }
241260 return map [string ]* dto.MetricFamily {
242- "spamoor_transactions_sent_total" : labeledCounterFamily ("spamoor_transactions_sent_total" , sentMap ),
243- "spamoor_transactions_failed_total" : labeledCounterFamily ("spamoor_transactions_failed_total" , failedMap ),
261+ "spamoor_transactions_sent_total" : labeledCounterFamily ("spamoor_transactions_sent_total" , sentMap ),
262+ "spamoor_transaction_failures_total" : labeledCounterFamily ("spamoor_transaction_failures_total" , failedMap ),
263+ "spamoor_spammer_running" : labeledGaugeFamily ("spamoor_spammer_running" , runningMap ),
244264 }, nil
245265}
246266
267+ func labeledGaugeFamily (name string , spammerValues map [string ]float64 ) * dto.MetricFamily {
268+ gaugeType := dto .MetricType_GAUGE
269+ labelName := "spammer_name"
270+ var metrics []* dto.Metric
271+ for spammerName , value := range spammerValues {
272+ metrics = append (metrics , & dto.Metric {
273+ Label : []* dto.LabelPair {
274+ {Name : & labelName , Value : & spammerName },
275+ },
276+ Gauge : & dto.Gauge {Value : & value },
277+ })
278+ }
279+ return & dto.MetricFamily {
280+ Name : & name ,
281+ Type : & gaugeType ,
282+ Metric : metrics ,
283+ }
284+ }
285+
247286func (f * fakeClient ) GetClients () ([]spamoorapi.Client , error ) {
248287 if f .getClientsErr != nil {
249288 return nil , f .getClientsErr
0 commit comments