2020-11-27 05:45:17 +01:00
|
|
|
package core
|
|
|
|
|
2020-12-30 02:22:17 +01:00
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
)
|
2020-11-27 05:45:17 +01:00
|
|
|
|
|
|
|
func TestNewServiceStatus(t *testing.T) {
|
2020-11-30 14:44:58 +01:00
|
|
|
service := &Service{Name: "name", Group: "group"}
|
2020-11-27 05:45:17 +01:00
|
|
|
serviceStatus := NewServiceStatus(service)
|
2020-11-30 14:44:58 +01:00
|
|
|
if serviceStatus.Name != service.Name {
|
|
|
|
t.Errorf("expected %s, got %s", service.Name, serviceStatus.Name)
|
|
|
|
}
|
2020-11-27 05:45:17 +01:00
|
|
|
if serviceStatus.Group != service.Group {
|
|
|
|
t.Errorf("expected %s, got %s", service.Group, serviceStatus.Group)
|
|
|
|
}
|
2021-01-28 00:25:37 +01:00
|
|
|
if serviceStatus.Key != "group_name" {
|
|
|
|
t.Errorf("expected %s, got %s", "group_name", serviceStatus.Key)
|
|
|
|
}
|
2020-11-27 05:45:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestServiceStatus_AddResult(t *testing.T) {
|
2020-11-30 14:44:58 +01:00
|
|
|
service := &Service{Name: "name", Group: "group"}
|
2020-11-27 05:45:17 +01:00
|
|
|
serviceStatus := NewServiceStatus(service)
|
2021-02-25 04:41:36 +01:00
|
|
|
for i := 0; i < MaximumNumberOfResults+10; i++ {
|
2020-12-30 02:22:17 +01:00
|
|
|
serviceStatus.AddResult(&Result{Timestamp: time.Now()})
|
2020-11-27 05:45:17 +01:00
|
|
|
}
|
2021-02-25 04:41:36 +01:00
|
|
|
if len(serviceStatus.Results) != MaximumNumberOfResults {
|
2021-03-05 05:00:30 +01:00
|
|
|
t.Errorf("expected serviceStatus.Results to not exceed a length of %d", MaximumNumberOfResults)
|
2020-11-27 05:45:17 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-25 04:41:36 +01:00
|
|
|
|
|
|
|
func TestServiceStatus_WithResultPagination(t *testing.T) {
|
|
|
|
service := &Service{Name: "name", Group: "group"}
|
|
|
|
serviceStatus := NewServiceStatus(service)
|
|
|
|
for i := 0; i < 25; i++ {
|
|
|
|
serviceStatus.AddResult(&Result{Timestamp: time.Now()})
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(1, 1).Results) != 1 {
|
|
|
|
t.Errorf("expected to have 1 result")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(5, 0).Results) != 0 {
|
|
|
|
t.Errorf("expected to have 0 results")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(-1, 20).Results) != 0 {
|
|
|
|
t.Errorf("expected to have 0 result, because the page was invalid")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(1, -1).Results) != 0 {
|
|
|
|
t.Errorf("expected to have 0 result, because the page size was invalid")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(1, 10).Results) != 10 {
|
|
|
|
t.Errorf("expected to have 10 results, because given a page size of 10, page 1 should have 10 elements")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(2, 10).Results) != 10 {
|
|
|
|
t.Errorf("expected to have 10 results, because given a page size of 10, page 2 should have 10 elements")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(3, 10).Results) != 5 {
|
|
|
|
t.Errorf("expected to have 5 results, because given a page size of 10, page 3 should have 5 elements")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(4, 10).Results) != 0 {
|
|
|
|
t.Errorf("expected to have 0 results, because given a page size of 10, page 4 should have 0 elements")
|
|
|
|
}
|
|
|
|
if len(serviceStatus.WithResultPagination(1, 50).Results) != 25 {
|
|
|
|
t.Errorf("expected to have 25 results, because there's only 25 results")
|
|
|
|
}
|
|
|
|
}
|