Add Service.Key() method to generate the unique service key

This commit is contained in:
TwinProduction
2021-07-11 22:22:21 -04:00
committed by Chris
parent 7aed826d65
commit 1498b6d8a2
5 changed files with 16 additions and 14 deletions

View File

@ -63,7 +63,7 @@ func (s *Store) GetServiceStatusByKey(key string) *core.ServiceStatus {
// Insert adds the observed result for the specified service into the store
func (s *Store) Insert(service *core.Service, result *core.Result) {
key := util.ConvertGroupAndServiceToKey(service.Group, service.Name)
key := service.Key()
serviceStatus, exists := s.cache.Get(key)
if !exists {
serviceStatus = core.NewServiceStatus(service)

View File

@ -6,7 +6,6 @@ import (
"time"
"github.com/TwinProduction/gatus/core"
"github.com/TwinProduction/gatus/util"
)
var (
@ -181,7 +180,7 @@ func TestStore_GetServiceStatusByKey(t *testing.T) {
store.Insert(&testService, &testSuccessfulResult)
store.Insert(&testService, &testUnsuccessfulResult)
serviceStatus := store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(testService.Group, testService.Name))
serviceStatus := store.GetServiceStatusByKey(testService.Key())
if serviceStatus == nil {
t.Fatalf("serviceStatus shouldn't have been nil")
}
@ -212,7 +211,7 @@ func TestStore_GetAllServiceStatusesWithResultPagination(t *testing.T) {
if len(serviceStatuses) != 1 {
t.Fatal("expected 1 service status")
}
actual, exists := serviceStatuses[util.ConvertGroupAndServiceToKey(testService.Group, testService.Name)]
actual, exists := serviceStatuses[testService.Key()]
if !exists {
t.Fatal("expected service status to exist")
}
@ -234,20 +233,20 @@ func TestStore_DeleteAllServiceStatusesNotInKeys(t *testing.T) {
if store.cache.Count() != 2 {
t.Errorf("expected cache to have 2 keys, got %d", store.cache.Count())
}
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)) == nil {
if store.GetServiceStatusByKey(firstService.Key()) == nil {
t.Fatal("firstService should exist")
}
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(secondService.Group, secondService.Name)) == nil {
if store.GetServiceStatusByKey(secondService.Key()) == nil {
t.Fatal("secondService should exist")
}
store.DeleteAllServiceStatusesNotInKeys([]string{util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)})
store.DeleteAllServiceStatusesNotInKeys([]string{firstService.Key()})
if store.cache.Count() != 1 {
t.Fatalf("expected cache to have 1 keys, got %d", store.cache.Count())
}
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(firstService.Group, firstService.Name)) == nil {
if store.GetServiceStatusByKey(firstService.Key()) == nil {
t.Error("secondService should've been deleted")
}
if store.GetServiceStatusByKey(util.ConvertGroupAndServiceToKey(secondService.Group, secondService.Name)) != nil {
if store.GetServiceStatusByKey(secondService.Key()) != nil {
t.Error("firstService should still exist")
}
}