From d6b792375c5368bdf7a396b59b8e0feaf3ed05ff Mon Sep 17 00:00:00 2001 From: Thorsten de Buhr Date: Thu, 31 Aug 2023 16:06:03 +0200 Subject: [PATCH] testing concorrency --- cmd/installer/root.go | 46 +++++++++++++++++--------------------- cmd/installer/root_test.go | 5 +++++ 2 files changed, 25 insertions(+), 26 deletions(-) diff --git a/cmd/installer/root.go b/cmd/installer/root.go index 625eb46..301fea3 100644 --- a/cmd/installer/root.go +++ b/cmd/installer/root.go @@ -291,6 +291,20 @@ func massDownloadPdscFiles(pdscTag xml.PdscTag, skipInstalledPdscFiles bool, tim } } +func CheckConcurrency(concurrency int) int { + maxWorkers := runtime.GOMAXPROCS(0) + + if concurrency > 1 { + if concurrency > maxWorkers { + concurrency = maxWorkers + } + } else { + concurrency = 0 + } + + return concurrency +} + func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int) error { log.Info("Downloading all PDSC files available on the public index") if err := Installation.PublicIndexXML.Read(); err != nil { @@ -309,18 +323,8 @@ func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int } ctx := context.TODO() - maxWorkers := runtime.GOMAXPROCS(0) - - if concurrency > 1 { - if maxWorkers > concurrency { - maxWorkers = concurrency - } - if maxWorkers == 0 { - concurrency = 0 - } - } - - sem := semaphore.NewWeighted(int64(maxWorkers)) + concurrency = CheckConcurrency(concurrency) + sem := semaphore.NewWeighted(int64(concurrency)) for _, pdscTag := range pdscTags { if concurrency == 0 { @@ -338,7 +342,7 @@ func DownloadPDSCFiles(skipInstalledPdscFiles bool, concurrency int, timeout int } } if concurrency > 1 { - if err := sem.Acquire(ctx, int64(maxWorkers)); err != nil { + if err := sem.Acquire(ctx, int64(concurrency)); err != nil { log.Errorf("Failed to acquire semaphore: %v", err) } } @@ -354,18 +358,8 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int } ctx := context.TODO() - maxWorkers := runtime.GOMAXPROCS(0) - - if concurrency > 1 { - if maxWorkers > concurrency { - maxWorkers = concurrency - } - if maxWorkers == 0 { - concurrency = 0 - } - } - - sem := semaphore.NewWeighted(int64(maxWorkers)) + concurrency = CheckConcurrency(concurrency) + sem := semaphore.NewWeighted(int64(concurrency)) for _, pdscFile := range pdscFiles { log.Debugf("Checking if \"%s\" needs updating", pdscFile) @@ -415,7 +409,7 @@ func UpdateInstalledPDSCFiles(pidxXML *xml.PidxXML, concurrency int, timeout int } if concurrency > 1 { - if err := sem.Acquire(ctx, int64(maxWorkers)); err != nil { + if err := sem.Acquire(ctx, int64(concurrency)); err != nil { log.Errorf("Failed to acquire semaphore: %v", err) } } diff --git a/cmd/installer/root_test.go b/cmd/installer/root_test.go index b663aa9..28dacea 100644 --- a/cmd/installer/root_test.go +++ b/cmd/installer/root_test.go @@ -585,6 +585,11 @@ func TestUpdatePublicIndex(t *testing.T) { assert.Equal(copied, indexContent) }) + t.Run("test check concurrency function call", func(t *testing.T) { + assert.Equal(installer.CheckConcurrency(0), 0) + assert.Equal(installer.CheckConcurrency(5), 5) + }) + t.Run("test add remote index.pidx and dowload pdsc files", func(t *testing.T) { localTestingDir := "test-add-remote-index-download-pdsc" assert.Nil(installer.SetPackRoot(localTestingDir, CreatePackRoot))