add concurrency test on write & load index file

Signed-off-by: Artem Vdovin <arte.vdovin@gmail.com>
pull/30984/head
Artem Vdovin 2 months ago
parent 314bd19d11
commit 118d0eb697

@ -22,8 +22,10 @@ import (
"net/http" "net/http"
"net/http/httptest" "net/http/httptest"
"os" "os"
"path/filepath"
"runtime" "runtime"
"strings" "strings"
"sync"
"testing" "testing"
"time" "time"
@ -31,6 +33,7 @@ import (
"helm.sh/helm/v4/pkg/cli" "helm.sh/helm/v4/pkg/cli"
"helm.sh/helm/v4/pkg/getter" "helm.sh/helm/v4/pkg/getter"
"helm.sh/helm/v4/pkg/helmpath"
) )
type CustomGetter struct { type CustomGetter struct {
@ -91,6 +94,60 @@ func TestIndexCustomSchemeDownload(t *testing.T) {
} }
} }
func TestConcurrenyDownloadIndex(t *testing.T) {
srv, err := startLocalServerForTests(nil)
if err != nil {
t.Fatal(err)
}
defer srv.Close()
repo, err := NewChartRepository(&Entry{
Name: "nginx",
URL: srv.URL,
}, getter.All(&cli.EnvSettings{}))
if err != nil {
t.Fatalf("Problem loading chart repository from %s: %v", srv.URL, err)
}
repo.CachePath = t.TempDir()
// initial download index
idx, err := repo.DownloadIndexFile()
if err != nil {
t.Fatalf("Failed to download index file to %s: %v", idx, err)
}
indexFName := filepath.Join(repo.CachePath, helmpath.CacheIndexFile(repo.Config.Name))
var wg sync.WaitGroup
// Simultaneously start multiple goroutines that:
// 1) download index.yaml via DownloadIndexFile (write operation),
// 2) read index.yaml via LoadIndexFile (read operation).
// This checks for race conditions and ensures correct behavior under concurrent read/write access.
for range 150 {
wg.Add(1)
go func() {
defer wg.Done()
idx, err := repo.DownloadIndexFile()
if err != nil {
t.Fatalf("Failed to download index file to %s: %v", idx, err)
}
}()
wg.Add(1)
go func() {
defer wg.Done()
_, err := LoadIndexFile(indexFName)
if err != nil {
t.Fatalf("Failed to load index file: %v", err)
}
}()
}
wg.Wait()
}
// startLocalServerForTests Start the local helm server // startLocalServerForTests Start the local helm server
func startLocalServerForTests(handler http.Handler) (*httptest.Server, error) { func startLocalServerForTests(handler http.Handler) (*httptest.Server, error) {
if handler == nil { if handler == nil {

Loading…
Cancel
Save