2021-07-15 04:26:51 +02:00
|
|
|
package memory
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-21 03:25:14 +02:00
|
|
|
"github.com/TwiN/gatus/v4/core"
|
|
|
|
"github.com/TwiN/gatus/v4/storage/store/common"
|
|
|
|
"github.com/TwiN/gatus/v4/storage/store/common/paging"
|
2021-07-15 04:26:51 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAddResult(t *testing.T) {
|
2021-10-23 22:47:12 +02:00
|
|
|
endpoint := &core.Endpoint{Name: "name", Group: "group"}
|
|
|
|
endpointStatus := core.NewEndpointStatus(endpoint.Group, endpoint.Name)
|
2021-08-13 03:54:23 +02:00
|
|
|
for i := 0; i < (common.MaximumNumberOfResults+common.MaximumNumberOfEvents)*2; i++ {
|
2021-10-23 22:47:12 +02:00
|
|
|
AddResult(endpointStatus, &core.Result{Success: i%2 == 0, Timestamp: time.Now()})
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(endpointStatus.Results) != common.MaximumNumberOfResults {
|
|
|
|
t.Errorf("expected endpointStatus.Results to not exceed a length of %d", common.MaximumNumberOfResults)
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(endpointStatus.Events) != common.MaximumNumberOfEvents {
|
|
|
|
t.Errorf("expected endpointStatus.Events to not exceed a length of %d", common.MaximumNumberOfEvents)
|
2021-07-16 04:07:30 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
// Try to add nil endpointStatus
|
2021-07-16 04:07:30 +02:00
|
|
|
AddResult(nil, &core.Result{Timestamp: time.Now()})
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
|
|
|
|
2021-10-23 22:47:12 +02:00
|
|
|
func TestShallowCopyEndpointStatus(t *testing.T) {
|
|
|
|
endpoint := &core.Endpoint{Name: "name", Group: "group"}
|
|
|
|
endpointStatus := core.NewEndpointStatus(endpoint.Group, endpoint.Name)
|
2021-07-16 04:07:30 +02:00
|
|
|
ts := time.Now().Add(-25 * time.Hour)
|
2021-07-15 04:26:51 +02:00
|
|
|
for i := 0; i < 25; i++ {
|
2021-10-23 22:47:12 +02:00
|
|
|
AddResult(endpointStatus, &core.Result{Success: i%2 == 0, Timestamp: ts})
|
2021-07-16 04:07:30 +02:00
|
|
|
ts = ts.Add(time.Hour)
|
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(-1, -1)).Results) != 0 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 0 result")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(1, 1)).Results) != 1 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 1 result")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(5, 0)).Results) != 0 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 0 results")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(-1, 20)).Results) != 0 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 0 result, because the page was invalid")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(1, -1)).Results) != 0 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 0 result, because the page size was invalid")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(1, 10)).Results) != 10 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 10 results, because given a page size of 10, page 1 should have 10 elements")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(2, 10)).Results) != 10 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 10 results, because given a page size of 10, page 2 should have 10 elements")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(3, 10)).Results) != 5 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 5 results, because given a page size of 10, page 3 should have 5 elements")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(4, 10)).Results) != 0 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 0 results, because given a page size of 10, page 4 should have 0 elements")
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|
2021-10-23 22:47:12 +02:00
|
|
|
if len(ShallowCopyEndpointStatus(endpointStatus, paging.NewEndpointStatusParams().WithResults(1, 50)).Results) != 25 {
|
2021-07-16 04:07:30 +02:00
|
|
|
t.Error("expected to have 25 results, because there's only 25 results")
|
|
|
|
}
|
2021-07-15 04:26:51 +02:00
|
|
|
}
|