func TestPoll(t *testing.T) { setNowMock("2011-09-09T23:36:13Z") defer resetNowMock() intAgg := health.NewIntervalAggregation(now()) data := &health.HealthAggregationsResponse{ InstanceId: "web22.12345", IntervalDuration: time.Minute, IntervalAggregations: []*health.IntervalAggregation{intAgg}, } stop := serveJson(":5050", data) defer func() { stop() }() responses := make(chan *pollResponse, 2) poll(health.NewStream(), ":5050", responses) response := <-responses assert.NotNil(t, response) assert.Equal(t, response.HostPort, ":5050") assert.Equal(t, response.Timestamp, now()) assert.Nil(t, response.Err) assert.Equal(t, response.Code, 200) assert.True(t, response.Nanos > 0 && response.Nanos < int64(time.Second)) assert.Equal(t, response.InstanceId, "web22.12345") // we'll just "trust" that the other stuff gets unmarshalled correctly. We didn't really put anything in there anyway in this test. }
func combineAggregations(aggregations []*health.IntervalAggregation) *health.IntervalAggregation { if len(aggregations) == 0 { return nil } overallAgg := health.NewIntervalAggregation(aggregations[0].IntervalStart) for _, ia := range aggregations { overallAgg.Merge(ia) } return overallAgg }
func (hd *HealthD) recalculateIntervals() { job := hd.stream.NewJob("recalculate") for k, _ := range hd.intervalsNeedingRecalculation { intAggsAtTime := []*health.IntervalAggregation{} for key, intAgg := range hd.hostAggregations { if key.Time == k { intAggsAtTime = append(intAggsAtTime, intAgg) } } overallAgg := health.NewIntervalAggregation(k) for _, ia := range intAggsAtTime { overallAgg.Merge(ia) } hd.setAggregation(overallAgg) } // Reset everything: hd.intervalsNeedingRecalculation = make(map[time.Time]struct{}) job.Complete(health.Success) }