Add tests for GetAverageResponseTimeByKey

This commit is contained in:
TwinProduction 2021-08-21 11:17:39 -04:00 committed by Chris
parent 75d8b40327
commit f28d1b61f0
3 changed files with 69 additions and 0 deletions

View File

@ -99,6 +99,12 @@ func TestStore_SanityCheck(t *testing.T) {
} else if len(hourlyAverageResponseTime) != 1 { } else if len(hourlyAverageResponseTime) != 1 {
t.Errorf("expected 1 hour to have had a result in the past 24 hours, got %d", len(hourlyAverageResponseTime)) t.Errorf("expected 1 hour to have had a result in the past 24 hours, got %d", len(hourlyAverageResponseTime))
} }
if uptime, _ := store.GetUptimeByKey(testService.Key(), time.Now().Add(-24*time.Hour), time.Now()); uptime != 0.5 {
t.Errorf("expected uptime of last 24h to be 0.5, got %f", uptime)
}
if averageResponseTime, _ := store.GetAverageResponseTimeByKey(testService.Key(), time.Now().Add(-24*time.Hour), time.Now()); averageResponseTime != 450 {
t.Errorf("expected average response time of last 24h to be 450, got %d", averageResponseTime)
}
ss := store.GetServiceStatus(testService.Group, testService.Name, paging.NewServiceStatusParams().WithResults(1, 20).WithEvents(1, 20)) ss := store.GetServiceStatus(testService.Group, testService.Name, paging.NewServiceStatusParams().WithResults(1, 20).WithEvents(1, 20))
if ss == nil { if ss == nil {
t.Fatalf("Store should've had key '%s', but didn't", testService.Key()) t.Fatalf("Store should've had key '%s', but didn't", testService.Key())

View File

@ -279,6 +279,12 @@ func TestStore_SanityCheck(t *testing.T) {
} else if len(hourlyAverageResponseTime) != 1 { } else if len(hourlyAverageResponseTime) != 1 {
t.Errorf("expected 1 hour to have had a result in the past 24 hours, got %d", len(hourlyAverageResponseTime)) t.Errorf("expected 1 hour to have had a result in the past 24 hours, got %d", len(hourlyAverageResponseTime))
} }
if uptime, _ := store.GetUptimeByKey(testService.Key(), time.Now().Add(-24*time.Hour), time.Now()); uptime != 0.5 {
t.Errorf("expected uptime of last 24h to be 0.5, got %f", uptime)
}
if averageResponseTime, _ := store.GetAverageResponseTimeByKey(testService.Key(), time.Now().Add(-24*time.Hour), time.Now()); averageResponseTime != 450 {
t.Errorf("expected average response time of last 24h to be 450, got %d", averageResponseTime)
}
ss := store.GetServiceStatus(testService.Group, testService.Name, paging.NewServiceStatusParams().WithResults(1, 20).WithEvents(1, 20)) ss := store.GetServiceStatus(testService.Group, testService.Name, paging.NewServiceStatusParams().WithResults(1, 20).WithEvents(1, 20))
if ss == nil { if ss == nil {
t.Fatalf("Store should've had key '%s', but didn't", testService.Key()) t.Fatalf("Store should've had key '%s', but didn't", testService.Key())

View File

@ -292,6 +292,63 @@ func TestStore_GetUptimeByKey(t *testing.T) {
} }
} }
func TestStore_GetAverageResponseTimeByKey(t *testing.T) {
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetAverageResponseTimeByKey")
defer cleanUp(scenarios)
firstResult := testSuccessfulResult
firstResult.Timestamp = now.Add(-(2 * time.Hour))
firstResult.Duration = 300 * time.Millisecond
secondResult := testSuccessfulResult
secondResult.Duration = 150 * time.Millisecond
secondResult.Timestamp = now.Add(-(1*time.Hour + 30*time.Minute))
thirdResult := testUnsuccessfulResult
thirdResult.Duration = 200 * time.Millisecond
thirdResult.Timestamp = now.Add(-(1 * time.Hour))
fourthResult := testSuccessfulResult
fourthResult.Duration = 500 * time.Millisecond
fourthResult.Timestamp = now
for _, scenario := range scenarios {
t.Run(scenario.Name, func(t *testing.T) {
scenario.Store.Insert(&testService, &firstResult)
scenario.Store.Insert(&testService, &secondResult)
scenario.Store.Insert(&testService, &thirdResult)
scenario.Store.Insert(&testService, &fourthResult)
if averageResponseTime, err := scenario.Store.GetAverageResponseTimeByKey(testService.Key(), now.Add(-48*time.Hour), now.Add(-24*time.Hour)); err == nil {
if averageResponseTime != 0 {
t.Errorf("expected average response time to be 0ms, got %v", averageResponseTime)
}
} else {
t.Error("shouldn't have returned an error, got", err)
}
if averageResponseTime, err := scenario.Store.GetAverageResponseTimeByKey(testService.Key(), now.Add(-24*time.Hour), now); err == nil {
if averageResponseTime != 287 {
t.Errorf("expected average response time to be 287ms, got %v", averageResponseTime)
}
} else {
t.Error("shouldn't have returned an error, got", err)
}
if averageResponseTime, err := scenario.Store.GetAverageResponseTimeByKey(testService.Key(), now.Add(-time.Hour), now); err == nil {
if averageResponseTime != 350 {
t.Errorf("expected average response time to be 350ms, got %v", averageResponseTime)
}
} else {
t.Error("shouldn't have returned an error, got", err)
}
if averageResponseTime, err := scenario.Store.GetAverageResponseTimeByKey(testService.Key(), now.Add(-2*time.Hour), now.Add(-time.Hour)); err == nil {
if averageResponseTime != 216 {
t.Errorf("expected average response time to be 216ms, got %v", averageResponseTime)
}
} else {
t.Error("shouldn't have returned an error, got", err)
}
if _, err := scenario.Store.GetAverageResponseTimeByKey(testService.Key(), now, now.Add(-2*time.Hour)); err == nil {
t.Error("expected an error because from > to, got nil")
}
scenario.Store.Clear()
})
}
}
func TestStore_GetHourlyAverageResponseTimeByKey(t *testing.T) { func TestStore_GetHourlyAverageResponseTimeByKey(t *testing.T) {
scenarios := initStoresAndBaseScenarios(t, "TestStore_GetHourlyAverageResponseTimeByKey") scenarios := initStoresAndBaseScenarios(t, "TestStore_GetHourlyAverageResponseTimeByKey")
defer cleanUp(scenarios) defer cleanUp(scenarios)