Skip to content

Refactored StartAccountHandler to better manage Goroutines liveness #25

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Aug 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/core/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,29 +31,41 @@ func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) {

// Iterate over accounts and start account handlers
for _, account := range accounts {
go c.StartAccountHandler(account)
go c.StartAccountHandler(account.ID.Hex())
}

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

func (c *CoreHandler) StartAccountHandler(account model.Account) {
func (c *CoreHandler) StartAccountHandler(accountId string) {
for {
if !account.Settings.Active {
// Fetch account
account, _ := c.AccountRepository.FetchByAccountId(accountId)

if account == nil {
// Terminate the goroutine (account was deleted)
return
}

account, _ := c.AccountRepository.FetchByDomainId(account.Domain.ID)
timeWindow, unitWindow := getTimeWindow(account.Settings.Window)
// Wait for account to be active
if !account.Settings.Active {
c.updateDomainStatus(*account, model.StatusPending, "Account was deactivated")
time.Sleep(1 * time.Minute)
continue
}

// Fetch repository data
repositoryData, err := c.GitService.GetRepositoryData(account.Environment)

// Check if repository is out of sync
if err == nil && isRepositoryOutSync(*account, repositoryData.CommitHash) {
c.syncUp(*account, repositoryData)
}

// Wait for the next cycle
timeWindow, unitWindow := getTimeWindow(account.Settings.Window)
time.Sleep(time.Duration(timeWindow) * unitWindow)
}
}
Expand Down
85 changes: 57 additions & 28 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"encoding/json"
"errors"
"runtime"
"testing"
"time"

Expand All @@ -18,14 +19,14 @@ func TestInitCoreHandlerCoroutine(t *testing.T) {
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

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

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

// Terminate the goroutine
account.Settings.Active = false
coreHandler.AccountRepository.Update(&account)
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
time.Sleep(1 * time.Second)

// Assert
Expand All @@ -39,43 +40,68 @@ func TestStartAccountHandler(t *testing.T) {
t.Run("Should not sync when account is not active", func(t *testing.T) {
// Given
account := givenAccount()
account.Domain.ID = "123-not-active"
account.Settings.Active = false
coreHandler.AccountRepository.Create(&account)
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
go coreHandler.StartAccountHandler(account)
go coreHandler.StartAccountHandler(accountCreated.ID.Hex())

time.Sleep(1 * time.Second)

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
assert.Equal(t, model.StatusOutSync, accountFromDb.Domain.Status)
assert.Equal(t, "", accountFromDb.Domain.Message)
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
assert.Equal(t, model.StatusPending, accountFromDb.Domain.Status)
assert.Equal(t, "Account was deactivated", accountFromDb.Domain.Message)
assert.Equal(t, "", accountFromDb.Domain.LastCommit)

tearDown()
})

t.Run("Should not sync after account is deleted", func(t *testing.T) {
// Given
account := givenAccount()
account.Domain.ID = "123-deleted"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
go coreHandler.StartAccountHandler(accountCreated.ID.Hex())
numGoroutinesBefore := runtime.NumGoroutine()

// Terminate the goroutine
coreHandler.AccountRepository.DeleteByDomainId(accountCreated.Domain.ID)
time.Sleep(1 * time.Second)
numGoroutinesAfter := runtime.NumGoroutine()

// Assert
_, err := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
assert.Less(t, numGoroutinesAfter, numGoroutinesBefore)
assert.NotNil(t, err)

tearDown()
})

t.Run("Should sync successfully when repository is out of sync", func(t *testing.T) {
// Given
fakeGitService := NewFakeGitService()
fakeApiService := NewFakeApiService()
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

account := givenAccount()
account.Domain.ID = "123-out-sync"
account.Domain.Version = "1"
coreHandler.AccountRepository.Create(&account)
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
go coreHandler.StartAccountHandler(account)
go coreHandler.StartAccountHandler(accountCreated.ID.Hex())

// Terminate the goroutine
account.Settings.Active = false
coreHandler.AccountRepository.Update(&account)
accountCreated.Settings.Active = false
coreHandler.AccountRepository.Update(accountCreated)
time.Sleep(1 * time.Second)

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
assert.Contains(t, accountFromDb.Domain.Message, "Synced successfully")
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
Expand All @@ -94,19 +120,20 @@ func TestStartAccountHandler(t *testing.T) {
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

account := givenAccount()
account.Domain.ID = "123-newer-version"
account.Domain.Version = "0"
coreHandler.AccountRepository.Create(&account)
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
go coreHandler.StartAccountHandler(account)
go coreHandler.StartAccountHandler(accountCreated.ID.Hex())

// Terminate the goroutine
account.Settings.Active = false
coreHandler.AccountRepository.Update(&account)
accountCreated.Settings.Active = false
coreHandler.AccountRepository.Update(accountCreated)
time.Sleep(1 * time.Second)

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
assert.Equal(t, model.StatusSynced, accountFromDb.Domain.Status)
assert.Contains(t, accountFromDb.Domain.Message, "Synced successfully")
assert.Equal(t, "111", accountFromDb.Domain.LastCommit)
Expand All @@ -125,18 +152,19 @@ func TestStartAccountHandler(t *testing.T) {
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

account := givenAccount()
coreHandler.AccountRepository.Create(&account)
account.Domain.ID = "123-api-error"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
go coreHandler.StartAccountHandler(account)
go coreHandler.StartAccountHandler(accountCreated.ID.Hex())

// Terminate the goroutine
account.Settings.Active = false
coreHandler.AccountRepository.Update(&account)
accountCreated.Settings.Active = false
coreHandler.AccountRepository.Update(accountCreated)
time.Sleep(1 * time.Second)

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
assert.Equal(t, model.StatusError, accountFromDb.Domain.Status)
assert.Contains(t, accountFromDb.Domain.Message, "Failed to check for changes")
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)
Expand All @@ -154,18 +182,19 @@ func TestStartAccountHandler(t *testing.T) {
coreHandler = NewCoreHandler(coreHandler.AccountRepository, fakeGitService, fakeApiService, NewComparatorService())

account := givenAccount()
coreHandler.AccountRepository.Create(&account)
account.Domain.ID = "123-no-permission"
accountCreated, _ := coreHandler.AccountRepository.Create(&account)

// Test
go coreHandler.StartAccountHandler(account)
go coreHandler.StartAccountHandler(accountCreated.ID.Hex())

// Terminate the goroutine
account.Settings.Active = false
coreHandler.AccountRepository.Update(&account)
accountCreated.Settings.Active = false
coreHandler.AccountRepository.Update(accountCreated)
time.Sleep(1 * time.Second)

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(accountCreated.Domain.ID)
assert.Equal(t, model.StatusError, accountFromDb.Domain.Status)
assert.Contains(t, accountFromDb.Domain.Message, "authorization failed")
assert.Contains(t, accountFromDb.Domain.Message, "Failed to apply changes [Repository]")
Expand Down
1 change: 1 addition & 0 deletions src/model/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const (
)

const (
StatusPending = "Pending"
StatusSynced = "Synced"
StatusOutSync = "OutSync"
StatusError = "Error"
Expand Down