-
Notifications
You must be signed in to change notification settings - Fork 72
perf: return pre-goroutine value to avoid lock reacquisition in getLatestVersionCached #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
mudrii
merged 3 commits into
mudrii:main
from
SweetSophia:fix/concurrent-version-fetch-v2
Apr 13, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,161 @@ | ||
| package appsystem | ||
|
|
||
| import ( | ||
| "context" | ||
| "sync" | ||
| "sync/atomic" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/mudrii/openclaw-dashboard/internal/appconfig" | ||
| ) | ||
|
|
||
| func TestGetLatestVersionCached_ConcurrentCalls_NoRace(t *testing.T) { | ||
| cfg := appconfig.SystemConfig{ | ||
| Enabled: true, | ||
| VersionsTTLSeconds: 1, | ||
| GatewayTimeoutMs: 100, | ||
| MetricsTTLSeconds: 10, | ||
| PollSeconds: 10, | ||
| } | ||
|
|
||
| var fetchCount atomic.Int32 | ||
| original := fetchLatestVersion | ||
| t.Cleanup(func() { fetchLatestVersion = original }) | ||
|
|
||
| fetchLatestVersion = func(_ context.Context, _ int) string { | ||
| fetchCount.Add(1) | ||
| return "2026.4.11" | ||
| } | ||
|
|
||
| svc := NewSystemService(cfg, "test", context.Background()) | ||
|
|
||
| const goroutines = 20 | ||
| var wg sync.WaitGroup | ||
| wg.Add(goroutines) | ||
|
|
||
| for i := 0; i < goroutines; i++ { | ||
| go func() { | ||
| defer wg.Done() | ||
| svc.getLatestVersionCached() | ||
| }() | ||
| } | ||
| wg.Wait() | ||
|
|
||
| // Poll until the background goroutine finishes | ||
| waitForLatestRefreshDone(t, svc) | ||
|
|
||
| if got := fetchCount.Load(); got != 1 { | ||
| t.Errorf("expected exactly 1 fetch, got %d", got) | ||
| } | ||
|
|
||
| // Second batch: expire cache and fire again | ||
| svc.latestMu.Lock() | ||
| svc.latestAt = time.Time{} | ||
| svc.latestMu.Unlock() | ||
| fetchCount.Store(0) | ||
|
|
||
| wg.Add(goroutines) | ||
| for i := 0; i < goroutines; i++ { | ||
| go func() { | ||
| defer wg.Done() | ||
| svc.getLatestVersionCached() | ||
| }() | ||
| } | ||
| wg.Wait() | ||
| waitForLatestRefreshDone(t, svc) | ||
|
|
||
| if got := fetchCount.Load(); got != 1 { | ||
| t.Errorf("expected exactly 1 fetch after cache expiry, got %d", got) | ||
| } | ||
| } | ||
|
|
||
| func TestGetLatestVersionCached_ReturnsCachedValueWhileRefreshing(t *testing.T) { | ||
| cfg := appconfig.SystemConfig{ | ||
| Enabled: true, | ||
| VersionsTTLSeconds: 300, | ||
| GatewayTimeoutMs: 100, | ||
| MetricsTTLSeconds: 10, | ||
| PollSeconds: 10, | ||
| } | ||
|
|
||
| original := fetchLatestVersion | ||
| t.Cleanup(func() { fetchLatestVersion = original }) | ||
|
|
||
| fetched := make(chan struct{}) | ||
| fetchLatestVersion = func(_ context.Context, _ int) string { | ||
| <-fetched | ||
| return "2026.4.11-new" | ||
| } | ||
|
|
||
| svc := NewSystemService(cfg, "test", context.Background()) | ||
|
|
||
| // Pre-seed expired cache | ||
| svc.latestMu.Lock() | ||
| svc.latestVer = "2026.4.10-old" | ||
| svc.latestAt = time.Now().Add(-time.Hour) | ||
| svc.latestMu.Unlock() | ||
|
|
||
| v := svc.getLatestVersionCached() | ||
| if v != "2026.4.10-old" { | ||
| t.Errorf("expected stale cached value '2026.4.10-old', got %q", v) | ||
| } | ||
|
|
||
| close(fetched) | ||
| waitForLatestRefreshDone(t, svc) | ||
|
|
||
| svc.latestMu.RLock() | ||
| v = svc.latestVer | ||
| svc.latestMu.RUnlock() | ||
| if v != "2026.4.11-new" { | ||
| t.Errorf("expected updated value '2026.4.11-new', got %q", v) | ||
| } | ||
| } | ||
|
|
||
| func TestGetLatestVersionCached_NegativeCaching(t *testing.T) { | ||
| cfg := appconfig.SystemConfig{ | ||
| Enabled: true, | ||
| VersionsTTLSeconds: 1, | ||
| GatewayTimeoutMs: 100, | ||
| MetricsTTLSeconds: 10, | ||
| PollSeconds: 10, | ||
| } | ||
|
|
||
| original := fetchLatestVersion | ||
| t.Cleanup(func() { fetchLatestVersion = original }) | ||
|
|
||
| fetchLatestVersion = func(_ context.Context, _ int) string { | ||
| return "" // simulate failure | ||
| } | ||
|
|
||
| svc := NewSystemService(cfg, "test", context.Background()) | ||
|
|
||
| svc.getLatestVersionCached() | ||
| waitForLatestRefreshDone(t, svc) | ||
|
|
||
| svc.latestMu.RLock() | ||
| at := svc.latestAt | ||
| svc.latestMu.RUnlock() | ||
|
|
||
| if at.IsZero() { | ||
| t.Error("expected latestAt to be set even on fetch failure (negative caching)") | ||
| } | ||
| } | ||
|
|
||
| // waitForLatestRefreshDone polls until the background goroutine finishes. | ||
| func waitForLatestRefreshDone(t *testing.T, svc *SystemService) { | ||
| t.Helper() | ||
| deadline := time.Now().Add(3 * time.Second) | ||
| for { | ||
| svc.latestMu.RLock() | ||
| running := svc.latestRefresh | ||
| svc.latestMu.RUnlock() | ||
| if !running { | ||
| return | ||
| } | ||
| if time.Now().After(deadline) { | ||
| t.Fatal("timed out waiting for background refresh to complete") | ||
| } | ||
| time.Sleep(5 * time.Millisecond) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using
t.Cleanupto restore global state is more robust than manual restoration at the end of the test. It ensures the state is restored even if the test panics or fails early, and it keeps the setup and teardown logic together. This pattern should be applied to all tests in this file that overridefetchLatestVersion.