Skip to content

Commit 9a37def

Browse files
committed
test(watcher): restore main test names and max-retry callback coverage
1 parent c83a057 commit 9a37def

File tree

1 file changed

+66
-5
lines changed

1 file changed

+66
-5
lines changed

internal/watcher/watcher_test.go

Lines changed: 66 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ func TestAddOrUpdateClientSkipsUnchanged(t *testing.T) {
387387
}
388388
}
389389

390-
func TestAddOrUpdateClientUpdatesHashWithoutReload(t *testing.T) {
390+
func TestAddOrUpdateClientTriggersReloadAndHash(t *testing.T) {
391391
tmpDir := t.TempDir()
392392
authFile := filepath.Join(tmpDir, "sample.json")
393393
if err := os.WriteFile(authFile, []byte(`{"type":"demo","api_key":"k"}`), 0o644); err != nil {
@@ -416,7 +416,7 @@ func TestAddOrUpdateClientUpdatesHashWithoutReload(t *testing.T) {
416416
}
417417
}
418418

419-
func TestRemoveClientRemovesHashWithoutReload(t *testing.T) {
419+
func TestRemoveClientRemovesHash(t *testing.T) {
420420
tmpDir := t.TempDir()
421421
authFile := filepath.Join(tmpDir, "sample.json")
422422
var reloads int32
@@ -662,7 +662,7 @@ func TestStopConfigReloadTimerSafeWhenNil(t *testing.T) {
662662
w.stopConfigReloadTimer()
663663
}
664664

665-
func TestHandleEventRemovesAuthFileWithoutReload(t *testing.T) {
665+
func TestHandleEventRemovesAuthFile(t *testing.T) {
666666
tmpDir := t.TempDir()
667667
authFile := filepath.Join(tmpDir, "remove.json")
668668
if err := os.WriteFile(authFile, []byte(`{"type":"demo"}`), 0o644); err != nil {
@@ -952,7 +952,7 @@ func TestHandleEventAtomicReplaceUnchangedSkips(t *testing.T) {
952952
}
953953
}
954954

955-
func TestHandleEventAtomicReplaceChangedTriggersIncrementalUpdateOnly(t *testing.T) {
955+
func TestHandleEventAtomicReplaceChangedTriggersUpdate(t *testing.T) {
956956
tmpDir := t.TempDir()
957957
authDir := filepath.Join(tmpDir, "auth")
958958
if err := os.MkdirAll(authDir, 0o755); err != nil {
@@ -1013,7 +1013,7 @@ func TestHandleEventRemoveUnknownFileIgnored(t *testing.T) {
10131013
}
10141014
}
10151015

1016-
func TestHandleEventRemoveKnownFileDeletesWithoutReload(t *testing.T) {
1016+
func TestHandleEventRemoveKnownFileDeletes(t *testing.T) {
10171017
tmpDir := t.TempDir()
10181018
authDir := filepath.Join(tmpDir, "auth")
10191019
if err := os.MkdirAll(authDir, 0o755); err != nil {
@@ -1270,6 +1270,67 @@ func TestReloadConfigFiltersAffectedOAuthProviders(t *testing.T) {
12701270
}
12711271
}
12721272

1273+
func TestReloadConfigTriggersCallbackForMaxRetryCredentialsChange(t *testing.T) {
1274+
tmpDir := t.TempDir()
1275+
authDir := filepath.Join(tmpDir, "auth")
1276+
if err := os.MkdirAll(authDir, 0o755); err != nil {
1277+
t.Fatalf("failed to create auth dir: %v", err)
1278+
}
1279+
configPath := filepath.Join(tmpDir, "config.yaml")
1280+
1281+
oldCfg := &config.Config{
1282+
AuthDir: authDir,
1283+
MaxRetryCredentials: 0,
1284+
RequestRetry: 1,
1285+
MaxRetryInterval: 5,
1286+
}
1287+
newCfg := &config.Config{
1288+
AuthDir: authDir,
1289+
MaxRetryCredentials: 2,
1290+
RequestRetry: 1,
1291+
MaxRetryInterval: 5,
1292+
}
1293+
data, errMarshal := yaml.Marshal(newCfg)
1294+
if errMarshal != nil {
1295+
t.Fatalf("failed to marshal config: %v", errMarshal)
1296+
}
1297+
if errWrite := os.WriteFile(configPath, data, 0o644); errWrite != nil {
1298+
t.Fatalf("failed to write config: %v", errWrite)
1299+
}
1300+
1301+
callbackCalls := 0
1302+
callbackMaxRetryCredentials := -1
1303+
w := &Watcher{
1304+
configPath: configPath,
1305+
authDir: authDir,
1306+
lastAuthHashes: make(map[string]string),
1307+
reloadCallback: func(cfg *config.Config) {
1308+
callbackCalls++
1309+
if cfg != nil {
1310+
callbackMaxRetryCredentials = cfg.MaxRetryCredentials
1311+
}
1312+
},
1313+
}
1314+
w.SetConfig(oldCfg)
1315+
1316+
if ok := w.reloadConfig(); !ok {
1317+
t.Fatal("expected reloadConfig to succeed")
1318+
}
1319+
1320+
if callbackCalls != 1 {
1321+
t.Fatalf("expected reload callback to be called once, got %d", callbackCalls)
1322+
}
1323+
if callbackMaxRetryCredentials != 2 {
1324+
t.Fatalf("expected callback MaxRetryCredentials=2, got %d", callbackMaxRetryCredentials)
1325+
}
1326+
1327+
w.clientsMutex.RLock()
1328+
defer w.clientsMutex.RUnlock()
1329+
if w.config == nil || w.config.MaxRetryCredentials != 2 {
1330+
t.Fatalf("expected watcher config MaxRetryCredentials=2, got %+v", w.config)
1331+
}
1332+
}
1333+
12731334
func TestStartFailsWhenAuthDirMissing(t *testing.T) {
12741335
tmpDir := t.TempDir()
12751336
configPath := filepath.Join(tmpDir, "config.yaml")

0 commit comments

Comments
 (0)