From 9a37defed34d2a4bac11b428d6942660fcadb126 Mon Sep 17 00:00:00 2001 From: lyd123qw2008 <326643467@qq.com> Date: Sun, 1 Mar 2026 13:54:03 +0800 Subject: [PATCH] test(watcher): restore main test names and max-retry callback coverage --- internal/watcher/watcher_test.go | 71 +++++++++++++++++++++++++++++--- 1 file changed, 66 insertions(+), 5 deletions(-) diff --git a/internal/watcher/watcher_test.go b/internal/watcher/watcher_test.go index 32354e2f0..b4d758ddf 100644 --- a/internal/watcher/watcher_test.go +++ b/internal/watcher/watcher_test.go @@ -387,7 +387,7 @@ func TestAddOrUpdateClientSkipsUnchanged(t *testing.T) { } } -func TestAddOrUpdateClientUpdatesHashWithoutReload(t *testing.T) { +func TestAddOrUpdateClientTriggersReloadAndHash(t *testing.T) { tmpDir := t.TempDir() authFile := filepath.Join(tmpDir, "sample.json") if err := os.WriteFile(authFile, []byte(`{"type":"demo","api_key":"k"}`), 0o644); err != nil { @@ -416,7 +416,7 @@ func TestAddOrUpdateClientUpdatesHashWithoutReload(t *testing.T) { } } -func TestRemoveClientRemovesHashWithoutReload(t *testing.T) { +func TestRemoveClientRemovesHash(t *testing.T) { tmpDir := t.TempDir() authFile := filepath.Join(tmpDir, "sample.json") var reloads int32 @@ -662,7 +662,7 @@ func TestStopConfigReloadTimerSafeWhenNil(t *testing.T) { w.stopConfigReloadTimer() } -func TestHandleEventRemovesAuthFileWithoutReload(t *testing.T) { +func TestHandleEventRemovesAuthFile(t *testing.T) { tmpDir := t.TempDir() authFile := filepath.Join(tmpDir, "remove.json") if err := os.WriteFile(authFile, []byte(`{"type":"demo"}`), 0o644); err != nil { @@ -952,7 +952,7 @@ func TestHandleEventAtomicReplaceUnchangedSkips(t *testing.T) { } } -func TestHandleEventAtomicReplaceChangedTriggersIncrementalUpdateOnly(t *testing.T) { +func TestHandleEventAtomicReplaceChangedTriggersUpdate(t *testing.T) { tmpDir := t.TempDir() authDir := filepath.Join(tmpDir, "auth") if err := os.MkdirAll(authDir, 0o755); err != nil { @@ -1013,7 +1013,7 @@ func TestHandleEventRemoveUnknownFileIgnored(t *testing.T) { } } -func TestHandleEventRemoveKnownFileDeletesWithoutReload(t *testing.T) { +func TestHandleEventRemoveKnownFileDeletes(t *testing.T) { tmpDir := t.TempDir() authDir := filepath.Join(tmpDir, "auth") if err := os.MkdirAll(authDir, 0o755); err != nil { @@ -1270,6 +1270,67 @@ func TestReloadConfigFiltersAffectedOAuthProviders(t *testing.T) { } } +func TestReloadConfigTriggersCallbackForMaxRetryCredentialsChange(t *testing.T) { + tmpDir := t.TempDir() + authDir := filepath.Join(tmpDir, "auth") + if err := os.MkdirAll(authDir, 0o755); err != nil { + t.Fatalf("failed to create auth dir: %v", err) + } + configPath := filepath.Join(tmpDir, "config.yaml") + + oldCfg := &config.Config{ + AuthDir: authDir, + MaxRetryCredentials: 0, + RequestRetry: 1, + MaxRetryInterval: 5, + } + newCfg := &config.Config{ + AuthDir: authDir, + MaxRetryCredentials: 2, + RequestRetry: 1, + MaxRetryInterval: 5, + } + data, errMarshal := yaml.Marshal(newCfg) + if errMarshal != nil { + t.Fatalf("failed to marshal config: %v", errMarshal) + } + if errWrite := os.WriteFile(configPath, data, 0o644); errWrite != nil { + t.Fatalf("failed to write config: %v", errWrite) + } + + callbackCalls := 0 + callbackMaxRetryCredentials := -1 + w := &Watcher{ + configPath: configPath, + authDir: authDir, + lastAuthHashes: make(map[string]string), + reloadCallback: func(cfg *config.Config) { + callbackCalls++ + if cfg != nil { + callbackMaxRetryCredentials = cfg.MaxRetryCredentials + } + }, + } + w.SetConfig(oldCfg) + + if ok := w.reloadConfig(); !ok { + t.Fatal("expected reloadConfig to succeed") + } + + if callbackCalls != 1 { + t.Fatalf("expected reload callback to be called once, got %d", callbackCalls) + } + if callbackMaxRetryCredentials != 2 { + t.Fatalf("expected callback MaxRetryCredentials=2, got %d", callbackMaxRetryCredentials) + } + + w.clientsMutex.RLock() + defer w.clientsMutex.RUnlock() + if w.config == nil || w.config.MaxRetryCredentials != 2 { + t.Fatalf("expected watcher config MaxRetryCredentials=2, got %+v", w.config) + } +} + func TestStartFailsWhenAuthDirMissing(t *testing.T) { tmpDir := t.TempDir() configPath := filepath.Join(tmpDir, "config.yaml")