2021-07-15 04:26:51 +02:00
package memory
2020-12-30 02:22:17 +01:00
import (
"testing"
"time"
2021-07-15 04:26:51 +02:00
2024-05-10 04:56:16 +02:00
"github.com/TwiN/gatus/v5/config/endpoint"
2020-12-30 02:22:17 +01:00
)
2021-07-15 04:26:51 +02:00
func TestProcessUptimeAfterResult ( t * testing . T ) {
2024-05-10 04:56:16 +02:00
ep := & endpoint . Endpoint { Name : "name" , Group : "group" }
status := endpoint . NewStatus ( ep . Group , ep . Name )
2021-10-23 22:47:12 +02:00
uptime := status . Uptime
2020-12-30 02:22:17 +01:00
2021-04-18 06:51:47 +02:00
now := time . Now ( )
now = time . Date ( now . Year ( ) , now . Month ( ) , now . Day ( ) , now . Hour ( ) , 0 , 0 , 0 , now . Location ( ) )
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 7 * 24 * time . Hour ) , Success : true } )
2020-12-30 02:22:17 +01:00
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 6 * 24 * time . Hour ) , Success : false } )
2020-12-30 02:22:17 +01:00
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 8 * 24 * time . Hour ) , Success : true } )
2020-12-30 02:22:17 +01:00
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 24 * time . Hour ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 12 * time . Hour ) , Success : true } )
2020-12-30 02:22:17 +01:00
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 1 * time . Hour ) , Success : true , Duration : 10 * time . Millisecond } )
2021-04-18 06:51:47 +02:00
checkHourlyStatistics ( t , uptime . HourlyStatistics [ now . Unix ( ) - now . Unix ( ) % 3600 - 3600 ] , 10 , 1 , 1 )
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 30 * time . Minute ) , Success : false , Duration : 500 * time . Millisecond } )
2021-04-18 06:51:47 +02:00
checkHourlyStatistics ( t , uptime . HourlyStatistics [ now . Unix ( ) - now . Unix ( ) % 3600 - 3600 ] , 510 , 2 , 1 )
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 15 * time . Minute ) , Success : false , Duration : 25 * time . Millisecond } )
2021-04-18 06:51:47 +02:00
checkHourlyStatistics ( t , uptime . HourlyStatistics [ now . Unix ( ) - now . Unix ( ) % 3600 - 3600 ] , 535 , 3 , 1 )
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 10 * time . Minute ) , Success : false } )
2020-12-30 02:22:17 +01:00
2024-05-10 04:56:16 +02:00
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 120 * time . Hour ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 119 * time . Hour ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 118 * time . Hour ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 117 * time . Hour ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 10 * time . Hour ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 8 * time . Hour ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 30 * time . Minute ) , Success : true } )
processUptimeAfterResult ( uptime , & endpoint . Result { Timestamp : now . Add ( - 25 * time . Minute ) , Success : true } )
2020-12-30 02:22:17 +01:00
}
2021-07-15 04:26:51 +02:00
func TestAddResultUptimeIsCleaningUpAfterItself ( t * testing . T ) {
2024-05-10 04:56:16 +02:00
ep := & endpoint . Endpoint { Name : "name" , Group : "group" }
status := endpoint . NewStatus ( ep . Group , ep . Name )
2020-12-30 02:22:17 +01:00
now := time . Now ( )
now = time . Date ( now . Year ( ) , now . Month ( ) , now . Day ( ) , now . Hour ( ) , 0 , 0 , 0 , now . Location ( ) )
// Start 12 days ago
timestamp := now . Add ( - 12 * 24 * time . Hour )
for timestamp . Unix ( ) <= now . Unix ( ) {
2024-05-10 04:56:16 +02:00
AddResult ( status , & endpoint . Result { Timestamp : timestamp , Success : true } )
2024-08-12 04:40:19 +02:00
if len ( status . Uptime . HourlyStatistics ) > uptimeCleanUpThreshold {
t . Errorf ( "At no point in time should there be more than %d entries in status.SuccessfulExecutionsPerHour, but there are %d" , uptimeCleanUpThreshold , len ( status . Uptime . HourlyStatistics ) )
2020-12-30 02:22:17 +01:00
}
2021-10-23 22:47:12 +02:00
// Simulate endpoint with an interval of 3 minutes
2020-12-30 02:22:17 +01:00
timestamp = timestamp . Add ( 3 * time . Minute )
}
}
2024-05-10 04:56:16 +02:00
func checkHourlyStatistics ( t * testing . T , hourlyUptimeStatistics * endpoint . HourlyUptimeStatistics , expectedTotalExecutionsResponseTime uint64 , expectedTotalExecutions uint64 , expectedSuccessfulExecutions uint64 ) {
2021-04-18 06:51:47 +02:00
if hourlyUptimeStatistics . TotalExecutionsResponseTime != expectedTotalExecutionsResponseTime {
t . Error ( "TotalExecutionsResponseTime should've been" , expectedTotalExecutionsResponseTime , "got" , hourlyUptimeStatistics . TotalExecutionsResponseTime )
}
if hourlyUptimeStatistics . TotalExecutions != expectedTotalExecutions {
t . Error ( "TotalExecutions should've been" , expectedTotalExecutions , "got" , hourlyUptimeStatistics . TotalExecutions )
}
if hourlyUptimeStatistics . SuccessfulExecutions != expectedSuccessfulExecutions {
t . Error ( "SuccessfulExecutions should've been" , expectedSuccessfulExecutions , "got" , hourlyUptimeStatistics . SuccessfulExecutions )
}
}