From cfe4a95a11d9d111f4dcbab0c568d8a20668dcf5 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sat, 22 Jun 2024 22:52:22 -0700 Subject: [PATCH 1/2] Added goroutine StartAccountHandler skeleton exec plan --- src/core/core_test.go | 8 ++--- src/core/handler.go | 42 ++++++++++++++++++++++--- src/core/handler_test.go | 67 ++++++++++++++++++++++++++++++++++++++-- 3 files changed, 107 insertions(+), 10 deletions(-) diff --git a/src/core/core_test.go b/src/core/core_test.go index 3b13276..1b19b13 100644 --- a/src/core/core_test.go +++ b/src/core/core_test.go @@ -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, diff --git a/src/core/handler.go b/src/core/handler.go index 6936b63..6294759 100644 --- a/src/core/handler.go +++ b/src/core/handler.go @@ -1,6 +1,9 @@ package core import ( + "sync" + "time" + "github.com/switcherapi/switcher-gitops/src/model" "github.com/switcherapi/switcher-gitops/src/repository" ) @@ -20,9 +23,9 @@ 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 @@ -30,6 +33,37 @@ func (c *CoreHandler) InitCoreHandlerCoroutine() (int, error) { 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) } diff --git a/src/core/handler_test.go b/src/core/handler_test.go index 2e42044..efc8336 100644 --- a/src/core/handler_test.go +++ b/src/core/handler_test.go @@ -1,15 +1,18 @@ 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() @@ -17,4 +20,64 @@ func TestInitCoreHandlerCoroutine(t *testing.T) { // 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()) } From a8455e5f38cebe8dbb6a37306790cf2b017733c8 Mon Sep 17 00:00:00 2001 From: petruki <31597636+petruki@users.noreply.github.com> Date: Sat, 22 Jun 2024 22:58:25 -0700 Subject: [PATCH 2/2] fix: CI workflow to run sync tests --- .github/workflows/master.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/master.yml b/.github/workflows/master.yml index 64925bf..03b1206 100644 --- a/.github/workflows/master.yml +++ b/.github/workflows/master.yml @@ -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