mirror of
https://github.com/larksuite/cli.git
synced 2026-07-09 10:29:48 +08:00
fix(registry): wait for background meta refresh before test reset (#894)
* fix(registry): wait for background meta refresh before test reset TestComputeMinimumScopeSet can start doBackgroundRefresh via Init() while the next test's resetInit() mutates package-level globals the goroutine still reads (e.g. remoteMetaURL / configuredBrand), causing data races under -race in the coverage job. Track the refresh goroutine with a WaitGroup and drain it at the start of resetInit() in tests.
This commit is contained in:
@@ -255,11 +255,18 @@ func doSyncFetch() {
|
||||
|
||||
// --- background refresh ---
|
||||
|
||||
var refreshOnce sync.Once
|
||||
var (
|
||||
refreshOnce sync.Once
|
||||
bgRefreshInFlight sync.WaitGroup // tracks doBackgroundRefresh goroutines for test teardown (resetInit)
|
||||
)
|
||||
|
||||
func triggerBackgroundRefresh() {
|
||||
refreshOnce.Do(func() {
|
||||
go doBackgroundRefresh()
|
||||
bgRefreshInFlight.Add(1)
|
||||
go func() {
|
||||
defer bgRefreshInFlight.Done()
|
||||
doBackgroundRefresh()
|
||||
}()
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
@@ -17,8 +17,18 @@ import (
|
||||
"github.com/larksuite/cli/internal/core"
|
||||
)
|
||||
|
||||
// waitBackgroundRefresh blocks until any in-flight background refresh started by
|
||||
// triggerBackgroundRefresh has finished. Lives in this _test file so production
|
||||
// binaries cannot call it and accidentally block on test teardown state.
|
||||
func waitBackgroundRefresh() {
|
||||
bgRefreshInFlight.Wait()
|
||||
}
|
||||
|
||||
// resetInit resets the package-level state so each test starts fresh.
|
||||
func resetInit() {
|
||||
// Must wait: a prior test's Init() may have started doBackgroundRefresh which
|
||||
// reads globals this function mutates (see CI race: TestComputeMinimumScopeSet → Tenant).
|
||||
waitBackgroundRefresh()
|
||||
initOnce = sync.Once{}
|
||||
mergedServices = make(map[string]map[string]interface{})
|
||||
mergedProjectList = nil
|
||||
|
||||
Reference in New Issue
Block a user