Skip to content

Commit

Permalink
Fix failing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewvc committed Sep 6, 2023
1 parent 3c5b677 commit 04076e6
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 229 deletions.
7 changes: 7 additions & 0 deletions heartbeat/hbtest/hbtestutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,7 @@ func BaseChecks(ip string, status string, typ string) validator.Validator {
}

return lookslike.Compose(
HasEventType,
lookslike.MustCompile(map[string]interface{}{
"monitor": map[string]interface{}{
"ip": ipCheck,
Expand All @@ -187,6 +188,12 @@ func BaseChecks(ip string, status string, typ string) validator.Validator {
)
}

var HasEventType = lookslike.MustCompile(map[string]interface{}{
"event": map[string]interface{}{
"type": isdef.Optional(isdef.IsNonEmptyString),
},
})

// SummaryStateChecks validates the "summary" + "state" fields
func SummaryStateChecks(up uint16, down uint16) validator.Validator {
return lookslike.Compose(
Expand Down
1 change: 1 addition & 0 deletions heartbeat/monitors/mocks.go
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,7 @@ func baseMockEventMonitorValidator(id string, name string, status string) valida

func mockEventMonitorValidator(id string, name string) validator.Validator {
return lookslike.Strict(lookslike.Compose(
hbtest.HasEventType,
baseMockEventMonitorValidator(id, name, "up"),
hbtestllext.MonitorTimespanValidator,
hbtest.SummaryStateChecks(1, 0),
Expand Down
14 changes: 12 additions & 2 deletions heartbeat/monitors/wrappers/summarizer/summarizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ type JobSummary struct {
RetryGroup string `json:"retry_group"`
}

func (js *JobSummary) String() string {
return fmt.Sprintf("<JobSummary status=%s attempt=%d/%d, final=%t, up=%d/%d retryGroup=%s>", js.Status, js.Attempt, js.MaxAttempts, js.FinalAttempt, js.Up, js.Down, js.RetryGroup)
}

func NewSummarizer(rootJob jobs.Job, sf stdfields.StdMonitorFields, mst *monitorstate.Tracker) *Summarizer {
plugins := make([]SumPlugin, 0, 2)
if sf.Type == "browser" {
Expand Down Expand Up @@ -209,10 +213,16 @@ func (ssp *StateStatusPlugin) OnSummary(event *beat.Event) (retry bool) {
// dereference the pointer since the pointer is pointed at the next step
// after this
jsCopy := *ssp.js
eventext.MergeEventFields(event, mapstr.M{

fields := mapstr.M{
"event": mapstr.M{"type": "heartbeat/summary"},
"summary": &jsCopy,
"state": ms,
})
}
if ssp.sf.Type == "browser" {
fields["synthetics"] = mapstr.M{"type": "heartbeat/summary"}
}
eventext.MergeEventFields(event, fields)

if retry {
// mutate the js into the state for the next attempt
Expand Down
26 changes: 25 additions & 1 deletion heartbeat/monitors/wrappers/summarizer/summarizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,56 +51,64 @@ func TestSummarizer(t *testing.T) {
// The expected states on each event
expectedStates string
// the attempt number of the given event
expectedAttempts string
expectedAttempts string
expectedSummaries int
}{
{
"start down, transition to up",
2,
"du",
"du",
"12",
2,
},
{
"start up, stay up",
2,
"uuuuuuuu",
"uuuuuuuu",
"11111111",
8,
},
{
"start down, stay down",
2,
"dddddddd",
"dddddddd",
"12121212",
8,
},
{
"start up - go down with one retry - thenrecover",
2,
"udddduuu",
"uuddduuu",
"11212111",
8,
},
{
"start up, transient down, recover",
2,
"uuuduuuu",
"uuuuuuuu",
"11112111",
8,
},
{
"start up, multiple transient down, recover",
2,
"uuudududu",
"uuuuuuuuu",
"111121212",
9,
},
{
"no retries, single down",
1,
"uuuduuuu",
"uuuduuuu",
"11111111",
8,
},
}

Expand Down Expand Up @@ -135,6 +143,8 @@ func TestSummarizer(t *testing.T) {
rcvdStatuses := ""
rcvdStates := ""
rcvdAttempts := ""
rcvdEvents := []*beat.Event{}
rcvdSummaries := []*JobSummary{}
i := 0
var lastSummary *JobSummary
for {
Expand All @@ -144,6 +154,7 @@ func TestSummarizer(t *testing.T) {
wrapped := s.Wrap(job)
events, _ := jobs.ExecJobAndConts(t, wrapped)
for _, event := range events {
rcvdEvents = append(rcvdEvents, event)
eventStatus, _ := event.GetValue("monitor.status")
eventStatusStr := eventStatus.(string)
rcvdStatuses += eventStatusStr[:1]
Expand All @@ -155,8 +166,18 @@ func TestSummarizer(t *testing.T) {
}
summaryIface, _ := event.GetValue("summary")
summary := summaryIface.(*JobSummary)
duration, _ := event.GetValue("monitor.duration.us")

// Ensure that only summaries have a duration
if summary != nil {
rcvdSummaries = append(rcvdSummaries, summary)
require.GreaterOrEqual(t, duration, int64(0))
} else {
require.Nil(t, duration)
}

if summary == nil {
// note missing summaries
rcvdAttempts += "!"
} else if lastSummary != nil {
if summary.Attempt > 1 {
Expand All @@ -165,6 +186,7 @@ func TestSummarizer(t *testing.T) {
require.NotEqual(t, lastSummary.RetryGroup, summary.RetryGroup)
}
}

rcvdAttempts += fmt.Sprintf("%d", summary.Attempt)
lastSummary = summary
}
Expand All @@ -176,6 +198,8 @@ func TestSummarizer(t *testing.T) {
require.Equal(t, tt.statusSequence, rcvdStatuses)
require.Equal(t, tt.expectedStates, rcvdStates)
require.Equal(t, tt.expectedAttempts, rcvdAttempts)
require.Len(t, rcvdEvents, len(tt.statusSequence))
require.Len(t, rcvdSummaries, tt.expectedSummaries)
})
}
}
Loading

0 comments on commit 04076e6

Please sign in to comment.