Add proper checks whether full batches were received

This commit is contained in:
Fabian Reinartz 2015-09-30 18:45:49 +02:00
parent d40bb99cee
commit 1fcc5d6717
3 changed files with 50 additions and 17 deletions

View File

@ -104,8 +104,8 @@ func (t *AcceptanceTest) Collector(name string) *Collector {
t: t.T,
name: name,
opts: t.opts,
collected: map[float64][]*types.Alert{},
exepected: map[Interval][]*types.Alert{},
collected: map[float64][][]*types.Alert{},
exepected: map[Interval][][]*types.Alert{},
}
t.collectors = append(t.collectors, co)

View File

@ -108,10 +108,12 @@ func TestBatching(t *testing.T) {
// until the 5s repeat interval has ended.
co.Want(Between(7, 8.5),
Alert("alertname", "test1").Active(1),
Alert("alertname", "test5").Active(1),
)
co.Want(Between(7, 8.5),
Alert("alertname", "test2").Active(1.5),
Alert("alertname", "test3").Active(1.5),
Alert("alertname", "test4").Active(1.6),
Alert("alertname", "test5").Active(1),
)
at.Run()

View File

@ -2,6 +2,7 @@ package test
import (
"fmt"
"sort"
"testing"
"time"
@ -15,14 +16,30 @@ type Collector struct {
name string
opts *AcceptanceOpts
collected map[float64][]*types.Alert
exepected map[Interval][]*types.Alert
collected map[float64][][]*types.Alert
exepected map[Interval][][]*types.Alert
}
func (c *Collector) String() string {
return c.name
}
func batchesEqual(as, bs []*types.Alert, opts *AcceptanceOpts) bool {
if len(as) != len(bs) {
return false
}
sort.Sort(types.AlertTimeline(as))
sort.Sort(types.AlertTimeline(bs))
for i, a := range as {
if !equalAlerts(a, bs[i], opts) {
return false
}
}
return true
}
// latest returns the latest relative point in time where a notification is
// expected.
func (c *Collector) latest() float64 {
@ -43,32 +60,36 @@ func (c *Collector) Want(iv Interval, alerts ...*TestAlert) {
nas = append(nas, a.nativeAlert(c.opts))
}
c.exepected[iv] = append(c.exepected[iv], nas...)
c.exepected[iv] = append(c.exepected[iv], nas)
}
// add the given alerts to the collected alerts.
func (c *Collector) add(alerts ...*types.Alert) {
arrival := c.opts.relativeTime(time.Now())
c.collected[arrival] = append(c.collected[arrival], alerts...)
c.collected[arrival] = append(c.collected[arrival], alerts)
}
func (c *Collector) check() string {
report := fmt.Sprintf("\nCollector %q:\n\n", c)
report := fmt.Sprintf("\ncollector %q:\n\n", c)
for iv, expected := range c.exepected {
report += fmt.Sprintf("interval %v\n", iv)
for _, exp := range expected {
var found *types.Alert
report += fmt.Sprintf("- %v ", exp)
var found []*types.Alert
report += fmt.Sprintf("---\n")
for _, e := range exp {
report += fmt.Sprintf("- %v\n", e)
}
for at, got := range c.collected {
if !iv.contains(at) {
continue
}
for _, a := range got {
if equalAlerts(exp, a, c.opts) {
if batchesEqual(exp, a, c.opts) {
found = a
break
}
@ -79,10 +100,10 @@ func (c *Collector) check() string {
}
if found != nil {
report += fmt.Sprintf("✓\n")
report += fmt.Sprintf(" [ ]\n")
} else {
c.t.Fail()
report += fmt.Sprintf("✗\n")
report += fmt.Sprintf(" [ ]\n")
}
}
}
@ -90,10 +111,17 @@ func (c *Collector) check() string {
// Detect unexpected notifications.
var totalExp, totalAct int
for _, exp := range c.exepected {
totalExp += len(exp)
for _, e := range exp {
totalExp += len(e)
}
}
for _, act := range c.collected {
totalAct += len(act)
for _, a := range act {
if len(a) == 0 {
c.t.Error("received empty notifications")
}
totalAct += len(a)
}
}
if totalExp != totalAct {
c.t.Fail()
@ -104,8 +132,11 @@ func (c *Collector) check() string {
report += "\nreceived:\n"
for at, col := range c.collected {
for _, a := range col {
report += fmt.Sprintf("- %v @ %v\n", a.String(), at)
for _, alerts := range col {
report += fmt.Sprintf("@ %v\n", at)
for _, a := range alerts {
report += fmt.Sprintf("- %v\n", a)
}
}
}
}