Skip to content

Added goroutine StartAccountHandler skeleton exec plan #13

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
Jun 23, 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
2 changes: 1 addition & 1 deletion .github/workflows/master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
run: go build -v ./...

- name: Test
run: go test -v ./... -coverprofile="coverage.out"
run: go test -p 1 -v ./... -coverprofile="coverage.out"
env:
GO_ENV: test
MONGODB_URI: mongodb://127.0.0.1:27017
Expand Down
8 changes: 4 additions & 4 deletions src/core/core_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@ func givenAccount() model.Account {
Domain: model.DomainDetails{
ID: "123",
Name: "Switcher GitOps",
Version: "123",
LastCommit: "123",
Status: "active",
Message: "Synced successfully",
Version: "",
LastCommit: "",
Status: "",
Message: "",
},
Settings: model.Settings{
Active: true,
Expand Down
42 changes: 38 additions & 4 deletions src/core/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package core

import (
"sync"
"time"

"github.com/switcherapi/switcher-gitops/src/model"
"github.com/switcherapi/switcher-gitops/src/repository"
)
Expand All @@ -20,16 +23,47 @@ func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) {
// Load all accounts
accounts, _ := c.AccountRepository.FetchAllActiveAccounts()

// Iterate over accounts and start a new account handler
// Iterate over accounts and start account handlers
for _, account := range accounts {
c.startNewAccountHandler(account)
go c.StartAccountHandler(account, make(chan bool), &sync.WaitGroup{})
}

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

func (c *CoreHandler) startNewAccountHandler(account model.Account) {
c.AccountRepository.FetchByDomainId(account.Domain.ID)
func (c *CoreHandler) StartAccountHandler(account model.Account, quit chan bool, wg *sync.WaitGroup) {
defer wg.Done()

// Check if account is not active
if !account.Settings.Active {
return
}

// Reads Window setting
sleep := 1

for {
select {
case <-quit:
return
default:
lastCommit := "123"
date := time.Now().Format("2006-01-02 15:04:05")

if account.Domain.LastCommit == "" || account.Domain.LastCommit != lastCommit {
c.CheckForChanges(account, lastCommit, date)
}
}

time.Sleep(time.Duration(sleep) * time.Second)
}
}

func (c *CoreHandler) CheckForChanges(account model.Account, lastCommit string, date string) {
account.Domain.LastCommit = lastCommit
account.Domain.Status = "Synced"
account.Domain.Message = "Synced successfully"
c.AccountRepository.Update(&account)
}
67 changes: 65 additions & 2 deletions src/core/handler_test.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,83 @@
package core

import (
"context"
"sync"
"testing"
"time"

"github.com/stretchr/testify/assert"
)

func TestInitCoreHandlerCoroutine(t *testing.T) {
// Given
account1 := givenAccount()
coreHandler.AccountRepository.Create(&account1)
account := givenAccount()
coreHandler.AccountRepository.Create(&account)

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

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

tearDown()
}

func TestStartAccountHandlerInactiveAccount(t *testing.T) {
// Given
account := givenAccount()
account.Settings.Active = false
coreHandler.AccountRepository.Create(&account)

// Prepare goroutine signals
var wg sync.WaitGroup
wg.Add(1)

// Test
go coreHandler.StartAccountHandler(account, make(chan bool), &wg)

// Wait for the goroutine to run and terminate
time.Sleep(1 * time.Second)
wg.Wait()

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
assert.Equal(t, "", accountFromDb.Domain.Message)
assert.Equal(t, "", accountFromDb.Domain.LastCommit)

tearDown()
}

func TestStartAccountHandler(t *testing.T) {
// Given
account := givenAccount()
coreHandler.AccountRepository.Create(&account)

// Prepare goroutine signals
var wg sync.WaitGroup
quit := make(chan bool)
wg.Add(1)

// Test
go coreHandler.StartAccountHandler(account, quit, &wg)

// Wait for the goroutine to run and terminate
time.Sleep(1 * time.Second)
quit <- true
wg.Wait()

// Assert
accountFromDb, _ := coreHandler.AccountRepository.FetchByDomainId(account.Domain.ID)
assert.Equal(t, "Synced successfully", accountFromDb.Domain.Message)
assert.Equal(t, "123", accountFromDb.Domain.LastCommit)

tearDown()
}

// Helpers

func tearDown() {
collection := mongoDb.Collection("accounts")
collection.Drop(context.Background())
}