refactor(cache): add completion channel to InvalidateAndRefresh
- commit
- a5ed4a4372b6297c65b7e8c10689d2daff072096
- author
- bnema · · 2025-11-30 05:00 UTC
- parent
- a35624f
- signature
- unsigned
2 files · +9 -6
M internal/cache/fuzzy_cache.go +6 -1
| @@ -209,8 +209,12 @@ func (cm *CacheManager) buildCacheFromDB(ctx context.Context) (*DmenuFuzzyCache, | |||
| 209 | 209 | ||
| 210 | 210 | // InvalidateAndRefresh forces cache invalidation and refresh. | |
| 211 | 211 | // This should be called when we know the DB has changed (e.g., after adding history). | |
| 212 | - | func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context) { | |
| 212 | + | // Returns a channel that is closed when the refresh is complete (can be ignored by callers). | |
| 213 | + | func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context) <-chan struct{} { | |
| 214 | + | done := make(chan struct{}) | |
| 213 | 215 | go func() { | |
| 216 | + | defer close(done) | |
| 217 | + | ||
| 214 | 218 | cm.mu.Lock() | |
| 215 | 219 | defer cm.mu.Unlock() | |
| 216 | 220 | ||
| @@ -229,6 +233,7 @@ func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context) { | |||
| 229 | 233 | logging.Warn(fmt.Sprintf("failed to refresh cache: %v", err)) | |
| 230 | 234 | } | |
| 231 | 235 | }() | |
| 236 | + | return done | |
| 232 | 237 | } | |
| 233 | 238 | ||
| 234 | 239 | // OnApplicationExit should be called when the application is about to exit. | |
| @@ -209,8 +209,12 @@ func (cm *CacheManager) buildCacheFromDB(ctx context.Context) (*DmenuFuzzyCache, | |||||
| 209 | 209 | ||||
| 210 | // InvalidateAndRefresh forces cache invalidation and refresh. | 210 | // InvalidateAndRefresh forces cache invalidation and refresh. | ||
| 211 | // This should be called when we know the DB has changed (e.g., after adding history). | 211 | // This should be called when we know the DB has changed (e.g., after adding history). | ||
| 212 | - | func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context) { | 212 | + | // Returns a channel that is closed when the refresh is complete (can be ignored by callers). |
| 213 | + | func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context) <-chan struct{} { | |||
| 214 | + | done := make(chan struct{}) | |||
| 213 | go func() { | 215 | go func() { | ||
| 216 | + | defer close(done) | |||
| 217 | + | ||||
| 214 | cm.mu.Lock() | 218 | cm.mu.Lock() | ||
| 215 | defer cm.mu.Unlock() | 219 | defer cm.mu.Unlock() | ||
| 216 | 220 | ||||
| @@ -229,6 +233,7 @@ func (cm *CacheManager) InvalidateAndRefresh(ctx context.Context) { | |||||
| 229 | logging.Warn(fmt.Sprintf("failed to refresh cache: %v", err)) | 233 | logging.Warn(fmt.Sprintf("failed to refresh cache: %v", err)) | ||
| 230 | } | 234 | } | ||
| 231 | }() | 235 | }() | ||
| 236 | + | return done | |||
| 232 | } | 237 | } | ||
| 233 | 238 | ||||
| 234 | // OnApplicationExit should be called when the application is about to exit. | 239 | // OnApplicationExit should be called when the application is about to exit. | ||
M internal/cache/fuzzy_cache_test.go +3 -5
| @@ -521,11 +521,9 @@ func TestCacheInvalidation(t *testing.T) { | |||
| 521 | 521 | t.Errorf("Expected 3 entries in initial cache, got %d", cache1.entryCount) | |
| 522 | 522 | } | |
| 523 | 523 | ||
| 524 | - | // Invalidate and refresh | |
| 525 | - | manager.InvalidateAndRefresh(ctx) | |
| 526 | - | ||
| 527 | - | // Wait for background refresh | |
| 528 | - | time.Sleep(100 * time.Millisecond) | |
| 524 | + | // Invalidate and refresh - wait for completion to prevent mock access after test ends | |
| 525 | + | done := manager.InvalidateAndRefresh(ctx) | |
| 526 | + | <-done | |
| 529 | 527 | ||
| 530 | 528 | // Get updated cache | |
| 531 | 529 | cache2, err := manager.GetCache(ctx) | |
| @@ -521,11 +521,9 @@ func TestCacheInvalidation(t *testing.T) { | |||||
| 521 | t.Errorf("Expected 3 entries in initial cache, got %d", cache1.entryCount) | 521 | t.Errorf("Expected 3 entries in initial cache, got %d", cache1.entryCount) | ||
| 522 | } | 522 | } | ||
| 523 | 523 | ||||
| 524 | - | // Invalidate and refresh | 524 | + | // Invalidate and refresh - wait for completion to prevent mock access after test ends |
| 525 | - | manager.InvalidateAndRefresh(ctx) | 525 | + | done := manager.InvalidateAndRefresh(ctx) |
| 526 | - | 526 | + | <-done | |
| 527 | - | // Wait for background refresh | |||
| 528 | - | time.Sleep(100 * time.Millisecond) | |||
| 529 | 527 | ||||
| 530 | // Get updated cache | 528 | // Get updated cache | ||
| 531 | cache2, err := manager.GetCache(ctx) | 529 | cache2, err := manager.GetCache(ctx) | ||