mirror of
https://github.com/akvorado/akvorado.git
synced 2025-12-12 06:24:10 +01:00
common/helpers: cache skip decision when requiring external services
This commit is contained in:
@@ -15,10 +15,16 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sync"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
|
||||
var (
|
||||
externalServiceSkipped = make(map[string]bool)
|
||||
externalServiceMu sync.RWMutex
|
||||
)
|
||||
|
||||
// CheckExternalService checks an external service, available either
|
||||
// as a named service or on a specific port on localhost. This applies
|
||||
// for example for Kafka and ClickHouse. The timeouts are quite short,
|
||||
@@ -30,8 +36,20 @@ func CheckExternalService(t *testing.T, name string, candidates []string) string
|
||||
if testing.Short() {
|
||||
t.Skipf("Skip test with real %s in short mode", name)
|
||||
}
|
||||
|
||||
mandatory := os.Getenv("CI_AKVORADO_FUNCTIONAL_TESTS") != ""
|
||||
|
||||
externalServiceMu.RLock()
|
||||
if skipped, cached := externalServiceSkipped[name]; cached && skipped {
|
||||
externalServiceMu.RUnlock()
|
||||
if mandatory {
|
||||
t.Fatalf("%s is not running (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name)
|
||||
} else {
|
||||
t.Skipf("%s is not running (CI_AKVORADO_FUNCTIONAL_TESTS is not set)", name)
|
||||
}
|
||||
}
|
||||
externalServiceMu.RUnlock()
|
||||
|
||||
server := ""
|
||||
for _, candidate := range candidates {
|
||||
resolv := net.Resolver{PreferGo: true}
|
||||
@@ -48,6 +66,9 @@ func CheckExternalService(t *testing.T, name string, candidates []string) string
|
||||
}
|
||||
}
|
||||
if server == "" {
|
||||
externalServiceMu.Lock()
|
||||
externalServiceSkipped[name] = true
|
||||
externalServiceMu.Unlock()
|
||||
if mandatory {
|
||||
t.Fatalf("%s cannot be resolved (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name)
|
||||
}
|
||||
@@ -65,6 +86,9 @@ func CheckExternalService(t *testing.T, name string, candidates []string) string
|
||||
t.Logf("DialContext() error:\n%+v", err)
|
||||
}
|
||||
if errors.Is(ctx.Err(), context.DeadlineExceeded) {
|
||||
externalServiceMu.Lock()
|
||||
externalServiceSkipped[name] = true
|
||||
externalServiceMu.Unlock()
|
||||
if mandatory {
|
||||
t.Fatalf("%s is not running (CI_AKVORADO_FUNCTIONAL_TESTS is set)", name)
|
||||
} else {
|
||||
|
||||
Reference in New Issue
Block a user