Skip to content

Commit

Permalink
Replaced coreHandler hardcoded status with state machine constant
Browse files Browse the repository at this point in the history
  • Loading branch information
petruki committed Sep 7, 2024
1 parent 1cf104b commit c2e2a79
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 17 deletions.
17 changes: 15 additions & 2 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ import (
"github.com/switcherapi/switcher-gitops/src/utils"
)

const (
CoreHandlerStatusInit = 0
CoreHandlerStatusRunning = 1
)

type CoreHandler struct {
AccountRepository repository.AccountRepository
ApiService IAPIService
Expand All @@ -24,6 +29,14 @@ func NewCoreHandler(repo repository.AccountRepository, apiService IAPIService, c
}

func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) {
// Check if core handler is already running
if c.status == CoreHandlerStatusRunning {
return c.status, nil
}

// Update core handler status
c.status = CoreHandlerStatusInit

// Load all accounts
accounts, _ := c.AccountRepository.FetchAllActiveAccounts()

Expand All @@ -37,13 +50,13 @@ func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) {
}

// Update core handler status
c.status = 1
c.status = CoreHandlerStatusRunning
return c.status, nil
}

func (c *CoreHandler) StartAccountHandler(accountId string, gitService IGitService) {
for {
// Fetch account
// Refresh account settings
account, _ := c.AccountRepository.FetchByAccountId(accountId)

if account == nil {
Expand Down
55 changes: 40 additions & 15 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,26 +13,51 @@ import (
)

func TestInitCoreHandlerCoroutine(t *testing.T) {
// Given
fakeApiService := NewFakeApiService()
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeApiService, NewComparatorService())
t.Run("Should start account handlers for all active accounts", func(t *testing.T) {
// Given
fakeApiService := NewFakeApiService()
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeApiService, NewComparatorService())

account := givenAccount()
account.Domain.ID = "123-init-core-handler"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
status, err := coreHandler.InitCoreHandlerCoroutine()

// Terminate the goroutine
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
time.Sleep(1 * time.Second)

// Assert
assert.Nil(t, err)
assert.Equal(t, CoreHandlerStatusRunning, status)

tearDown()
})

account := givenAccount()
account.Domain.ID = "123-init-core-handler"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)
t.Run("Should not start account handlers when core handler is already running", func(t *testing.T) {
// Given
fakeApiService := NewFakeApiService()
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeApiService, NewComparatorService())

// Test
status, err := coreHandler.InitCoreHandlerCoroutine()
account := givenAccount()
account.Domain.ID = "123-not-init-core-handler"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Terminate the goroutine
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
time.Sleep(1 * time.Second)
// Test
coreHandler.InitCoreHandlerCoroutine()
status, _ := coreHandler.InitCoreHandlerCoroutine()

// Assert
assert.Nil(t, err)
assert.Equal(t, 1, status)
// Terminate the goroutine
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
time.Sleep(1 * time.Second)

tearDown()
// Assert
assert.Equal(t, CoreHandlerStatusRunning, status)

tearDown()
})
}

func TestStartAccountHandler(t *testing.T) {
Expand Down

0 comments on commit c2e2a79

Please sign in to comment.